From 5105c20a1c892e3a4f6027685da2246669478e95 Mon Sep 17 00:00:00 2001 From: Antonios Chalkiopoulos Date: Sun, 22 Feb 2015 21:49:43 +0000 Subject: [PATCH] Providing a ParquetAvroScheme for reading/writing Parquet-Avro files from Cascading/Scalding. Support predicates and filters --- parquet-cascading/pom.xml | 10 + .../parquet/cascading/ParquetAvroScheme.java | 187 ++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java diff --git a/parquet-cascading/pom.xml b/parquet-cascading/pom.xml index c4ca385e83..bf3fd7f13f 100644 --- a/parquet-cascading/pom.xml +++ b/parquet-cascading/pom.xml @@ -55,6 +55,16 @@ parquet-thrift ${project.version} + + com.twitter + parquet-avro + ${project.version} + + + cascading.avro + avro-scheme + 2.5.0 + org.apache.hadoop hadoop-client diff --git a/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java b/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java new file mode 100644 index 0000000000..77ccd2bcc9 --- /dev/null +++ b/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package parquet.cascading; + +import java.io.IOException; +import java.io.Serializable; + +import org.apache.hadoop.mapred.JobConf; +import org.apache.hadoop.mapred.OutputCollector; +import org.apache.hadoop.mapred.RecordReader; + +import cascading.flow.FlowProcess; +import cascading.scheme.Scheme; +import cascading.scheme.SinkCall; +import cascading.scheme.SourceCall; +import cascading.tap.Tap; +import cascading.tuple.Tuple; +import cascading.tuple.TupleEntry; +import parquet.filter2.predicate.FilterPredicate; +import parquet.hadoop.ParquetInputFormat; +import parquet.hadoop.mapred.Container; + +import static parquet.Preconditions.checkNotNull; + +import org.apache.avro.Schema; +import org.apache.avro.generic.IndexedRecord; +import org.apache.avro.generic.GenericData.Record; +import parquet.avro.AvroReadSupport; +import parquet.avro.AvroWriteSupport; +import parquet.hadoop.mapred.DeprecatedParquetInputFormat; +import parquet.hadoop.mapred.DeprecatedParquetOutputFormat; +import cascading.avro.CascadingToAvro; + +/** + * A Cascading Scheme that returns a simple Tuple with a single value, the "value" object + * coming out of the underlying Avro InputFormat. + */ +public class ParquetAvroScheme extends Scheme { + + public static final class Config implements Serializable { + private final FilterPredicate filterPredicate; + private final String projectionString; + private final Class klass; + + private Config(Class klass, FilterPredicate filterPredicate, String projectionString) { + this.filterPredicate = filterPredicate; + this.projectionString = projectionString; + this.klass = klass; + } + + public Config() { + filterPredicate = null; + projectionString = null; + klass = null; + } + + public FilterPredicate getFilterPredicate() { + return filterPredicate; + } + + public String getProjectionString() { + return projectionString; + } + + public Class getKlass() { + return klass; + } + + public Config withFilterPredicate(FilterPredicate f) { + return new Config(this.klass, checkNotNull(f, "filterPredicate"), this.projectionString); + } + + public Config withProjectionString(String p) { + return new Config(this.klass, this.filterPredicate, checkNotNull(p, "projectionFilter")); + } + + public Config withRecordClass(Class klass) { + return new Config(checkNotNull(klass, "recordClass"), this.filterPredicate, this.projectionString); + } + } + + private static final long serialVersionUID = 157560846420730044L; + protected final Config config; + protected transient Schema schema; + protected String schemaStr; + + public ParquetAvroScheme() { + this(new Config()); + } + + public ParquetAvroScheme(FilterPredicate filterPredicate) { + this(new Config().withFilterPredicate(filterPredicate)); + } + + public ParquetAvroScheme(Config config) { + this.config = config; + } + + public ParquetAvroScheme(String avroSchema) { + this(new Config()); + schemaStr = avroSchema; + } + + private void setProjectionPushdown(JobConf jobConf) { + if (this.config.projectionString!= null) { + AvroReadSupport.setRequestedProjection(jobConf, new Schema.Parser().parse(this.config.projectionString)); + } + } + + private void setPredicatePushdown(JobConf jobConf) { + if (this.config.filterPredicate != null) { + ParquetInputFormat.setFilterPredicate(jobConf, this.config.filterPredicate); + } + } + + @Override + public void sourceConfInit(FlowProcess jobConfFlowProcess, Tap jobConfRecordReaderOutputCollectorTap, final JobConf jobConf) { + setPredicatePushdown(jobConf); + setProjectionPushdown(jobConf); + setRecordClass(jobConf); + } + + private void setRecordClass(JobConf jobConf) { + jobConf.setInputFormat(DeprecatedParquetInputFormat.class); + } + + @SuppressWarnings("unchecked") + @Override + public boolean source(FlowProcess fp, SourceCall sc) + throws IOException { + Container value = (Container) sc.getInput().createValue(); + boolean hasNext = sc.getInput().next(null, value); + if (!hasNext) { return false; } + + // Skip nulls + if (value == null) { return true; } + + sc.getIncomingEntry().setTuple(new Tuple(value.get())); + return true; + } + + @SuppressWarnings("unchecked") + @Override + public void sink(FlowProcess fp, SinkCall sc) + throws IOException { + TupleEntry tuple = sc.getOutgoingEntry(); + + IndexedRecord record = new Record(schema); + Object[] objectArray = CascadingToAvro.parseTupleEntry(tuple, schema); + for (int i = 0; i < objectArray.length; i++) { + record.put(i, objectArray[i]); + } + // noinspection unchecked + sc.getOutput().collect(null, record); + + } + + @Override + // Method sinkInit initializes this instance as a sink. + public void sinkConfInit(FlowProcess flowProcess, Tap tap, JobConf conf) { + conf.setOutputFormat(DeprecatedParquetOutputFormat.class); + conf.set("parquet.avro.schema", schemaStr); + DeprecatedParquetOutputFormat.setWriteSupportClass(conf, AvroWriteSupport.class); + } + + @Override + public void sinkPrepare(FlowProcess flowProcess, SinkCall sinkCall) throws IOException { + schema = new Schema.Parser().parse(schemaStr); + } + +}