|
| 1 | +package org.ohnlp.backbone.io.local; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import org.apache.avro.generic.GenericRecord; |
| 5 | +import org.apache.beam.sdk.io.FileIO; |
| 6 | +import org.apache.beam.sdk.io.parquet.ParquetIO; |
| 7 | +import org.apache.beam.sdk.schemas.Schema; |
| 8 | +import org.apache.beam.sdk.schemas.utils.AvroUtils; |
| 9 | +import org.apache.beam.sdk.transforms.DoFn; |
| 10 | +import org.apache.beam.sdk.transforms.ParDo; |
| 11 | +import org.apache.beam.sdk.values.PCollection; |
| 12 | +import org.apache.beam.sdk.values.PDone; |
| 13 | +import org.apache.beam.sdk.values.Row; |
| 14 | +import org.ohnlp.backbone.api.Load; |
| 15 | +import org.ohnlp.backbone.api.exceptions.ComponentInitializationException; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | + |
| 19 | +/** |
| 20 | + * Writes Parquet Formatted Files to Directory |
| 21 | + * |
| 22 | + * Expected Configuration: |
| 23 | + * { |
| 24 | + * "fileSystemPath": "path/to/write/to", |
| 25 | + * "fields": ["optional", "list", "of", "columns", "to", "write"] |
| 26 | + * } |
| 27 | + */ |
| 28 | +public class ParquetLoad extends Load { |
| 29 | + private String dir; |
| 30 | + private ArrayList<String> fields; |
| 31 | + |
| 32 | + private transient Schema beamSchema; |
| 33 | + private transient org.apache.avro.Schema avroSchema; |
| 34 | + |
| 35 | + @Override |
| 36 | + public void initFromConfig(JsonNode config) throws ComponentInitializationException { |
| 37 | + this.dir = config.get("fileSystemPath").asText(); |
| 38 | + this.fields = new ArrayList<>(); |
| 39 | + if (config.has("fields")) { |
| 40 | + config.get("fields").forEach((f) -> { |
| 41 | + String field = f.asText(); |
| 42 | + fields.add(field); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public PDone expand(PCollection<Row> input) { |
| 50 | + input.apply("Subselect Columns and Convert to Avro Format", ParDo.of(new DoFn<Row, GenericRecord>() { |
| 51 | + @ProcessElement |
| 52 | + public void processElement(@Element Row input, OutputReceiver<GenericRecord> output) { |
| 53 | + if (beamSchema == null) { |
| 54 | + // Assume homogeneous schema |
| 55 | + Schema sourceSchema = input.getSchema(); |
| 56 | + if (!fields.isEmpty()) { |
| 57 | + Schema.Builder targetBeamSchemaBuilder = org.apache.beam.sdk.schemas.Schema.builder(); |
| 58 | + for (String f : fields) { |
| 59 | + targetBeamSchemaBuilder.addField(f, sourceSchema.getField(f).getType()); |
| 60 | + } |
| 61 | + beamSchema = targetBeamSchemaBuilder.build(); |
| 62 | + } else { |
| 63 | + beamSchema = sourceSchema; |
| 64 | + } |
| 65 | + avroSchema = AvroUtils.toAvroSchema(beamSchema); |
| 66 | + } |
| 67 | + output.output(AvroUtils.toGenericRecord(input, avroSchema)); |
| 68 | + } |
| 69 | + })) |
| 70 | + .apply("Write to Filesystem", FileIO |
| 71 | + .<GenericRecord>write() |
| 72 | + .via(ParquetIO.sink(avroSchema)) |
| 73 | + .to(this.dir) |
| 74 | + ); |
| 75 | + return PDone.in(input.getPipeline()); |
| 76 | + } |
| 77 | +} |
0 commit comments