Class ParquetIO
- java.lang.Object
-
- org.apache.beam.sdk.io.parquet.ParquetIO
-
public class ParquetIO extends java.lang.ObjectIO to read and write Parquet files.Reading Parquet files
ParquetIOsource returns aPCollectionfor Parquet files. The elements in thePCollectionare AvroGenericRecord.To configure the
ParquetIO.Read, you have to provide the file patterns (from) of the Parquet files and the schema.For example:
PCollection<GenericRecord> records = pipeline.apply(ParquetIO.read(SCHEMA).from("/foo/bar")); ...As
ParquetIO.Readis based onFileIO, it supports any filesystem (hdfs, ...).When using schemas created via reflection, it may be useful to generate
GenericRecordinstances rather than instances of the class associated with the schema.ParquetIO.ReadandParquetIO.ReadFilesprovideParquetIO.Read.withAvroDataModel(GenericData)allowing implementations to set the data model associated with theAvroParquetReaderFor more advanced use cases, like reading each file in a
PCollectionofFileIO.ReadableFile, use theParquetIO.ReadFilestransform.For example:
PCollection<FileIO.ReadableFile> files = pipeline .apply(FileIO.match().filepattern(options.getInputFilepattern()) .apply(FileIO.readMatches()); PCollection<GenericRecord> output = files.apply(ParquetIO.readFiles(SCHEMA));ParquetIO leverages splittable reading by using Splittable DoFn. It initially splits the files into the blocks of 64MB and may dynamically split further for higher read efficiency.
Reading with projection can be enabled with the projection schema as following. Splittable reading is enabled when reading with projection. The projection_schema contains only the column that we would like to read and encoder_schema contains the schema to encode the output with the unwanted columns changed to nullable. Partial reading provide decrease of reading time due to partial processing of the data and partial encoding. The decrease in the reading time depends on the relative position of the columns. Memory allocation is optimised depending on the encoding schema. Note that the improvement is not as significant comparing to the proportion of the data requested, since the processing time saved is only the time to read the unwanted columns, the reader will still go over the data set according to the encoding schema since data for each column in a row is stored interleaved.
PCollection<GenericRecord> records = pipeline .apply( ParquetIO.read(SCHEMA).from("/foo/bar").withProjection(Projection_schema,Encoder_Schema));Reading records of an unknown schema
To read records from files whose schema is unknown at pipeline construction time or differs between files, use
parseGenericRecords(SerializableFunction)- in this case, you will need to specify a parsing function for converting eachGenericRecordinto a value of your custom type.For example:
Pipeline p = ...; PCollection<Foo> records = p.apply( ParquetIO.parseGenericRecords( new SerializableFunction<GenericRecord, Foo>() { public Foo apply(GenericRecord record) { // If needed, access the schema of the record using record.getSchema() return ...; } }) .from(...)); // For reading from files PCollection<FileIO.ReadableFile> files = p.apply(...); PCollection<Foo> records = files .apply( ParquetIO.parseFilesGenericRecords( new SerializableFunction<GenericRecord, Foo>() { public Foo apply(GenericRecord record) { // If needed, access the schema of the record using record.getSchema() return ...; } }));Inferring Beam schemas from Parquet files
If you want to use SQL or schema based operations on an Parquet-based PCollection, you must configure the read transform to infer the Beam schema and automatically setup the Beam related coders by doing:
You can also use it when reading a list of filenams from aPCollection<GenericRecord> parquetRecords = p.apply(ParquetIO.read(...).from(...).withBeamSchemas(true));PCollection<String>:PCollection<String> filePatterns = p.apply(...); PCollection<GenericRecord> parquetRecords = filePatterns .apply(ParquetIO.readFiles(...).withBeamSchemas(true));Writing Parquet files
ParquetIO.Sinkallows you to write aPCollectionofGenericRecordinto a Parquet file. It can be used with the general-purposeFileIOtransforms with FileIO.write/writeDynamic specifically.By default,
ParquetIO.Sinkproduces output files that are compressed using theCompressionCodec.SNAPPY. This default can be changed or overridden usingParquetIO.Sink.withCompressionCodec(CompressionCodecName).For example:
pipeline .apply(...) // PCollection<GenericRecord> .apply(FileIO .<GenericRecord>write() .via(ParquetIO.sink(SCHEMA) .withCompressionCodec(CompressionCodecName.SNAPPY)) .to("destination/path") .withSuffix(".parquet"));- See Also:
- Beam ParquetIO documentation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classParquetIO.Parse<T>Implementation ofparseGenericRecords(SerializableFunction).static classParquetIO.ParseFiles<T>Implementation ofparseFilesGenericRecords(SerializableFunction).static classParquetIO.ReadImplementation ofread(Schema).static classParquetIO.ReadFilesImplementation ofreadFiles(Schema).static classParquetIO.SinkImplementation ofsink(org.apache.avro.Schema).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> ParquetIO.ParseFiles<T>parseFilesGenericRecords(org.apache.beam.sdk.transforms.SerializableFunction<org.apache.avro.generic.GenericRecord,T> parseFn)ReadsGenericRecordfrom Parquet files and converts to user defined type using providedparseFn.static <T> ParquetIO.Parse<T>parseGenericRecords(org.apache.beam.sdk.transforms.SerializableFunction<org.apache.avro.generic.GenericRecord,T> parseFn)ReadsGenericRecordfrom a Parquet file (or multiple Parquet files matching the pattern) and converts to user defined type using provided parseFn.static ParquetIO.Readread(org.apache.avro.Schema schema)ReadsGenericRecordfrom a Parquet file (or multiple Parquet files matching the pattern).static ParquetIO.ReadFilesreadFiles(org.apache.avro.Schema schema)Likeread(Schema), but reads each file in aPCollectionofFileIO.ReadableFile, which allows more flexible usage.static ParquetIO.Sinksink(org.apache.avro.Schema schema)Creates aParquetIO.Sinkthat, for use withFileIO.write().
-
-
-
Method Detail
-
read
public static ParquetIO.Read read(org.apache.avro.Schema schema)
ReadsGenericRecordfrom a Parquet file (or multiple Parquet files matching the pattern).
-
readFiles
public static ParquetIO.ReadFiles readFiles(org.apache.avro.Schema schema)
Likeread(Schema), but reads each file in aPCollectionofFileIO.ReadableFile, which allows more flexible usage.
-
parseGenericRecords
public static <T> ParquetIO.Parse<T> parseGenericRecords(org.apache.beam.sdk.transforms.SerializableFunction<org.apache.avro.generic.GenericRecord,T> parseFn)
ReadsGenericRecordfrom a Parquet file (or multiple Parquet files matching the pattern) and converts to user defined type using provided parseFn.
-
parseFilesGenericRecords
public static <T> ParquetIO.ParseFiles<T> parseFilesGenericRecords(org.apache.beam.sdk.transforms.SerializableFunction<org.apache.avro.generic.GenericRecord,T> parseFn)
ReadsGenericRecordfrom Parquet files and converts to user defined type using providedparseFn.
-
sink
public static ParquetIO.Sink sink(org.apache.avro.Schema schema)
Creates aParquetIO.Sinkthat, for use withFileIO.write().
-
-