From be8a08d85d7197a0a14d8f280d62e4c029a3b6c4 Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Mon, 23 Mar 2015 17:15:08 +0200 Subject: [PATCH 1/9] [PARQUET-198] Initial commit of ParquetAvroScheme (currently only supports code-generated classes) --- .../avro/projection/AvroCustomField.java | 38 ++++ .../avro/projection/AvroProjection.java | 157 ++++++++++++++++ parquet-cascading/pom.xml | 22 +++ .../parquet/cascading/ParquetAvroScheme.java | 98 ++++++++++ parquet-cascading/src/test/avro/test.avdl | 8 + .../cascading/TestParquetAvroScheme.java | 169 ++++++++++++++++++ 6 files changed, 492 insertions(+) create mode 100644 parquet-avro/src/main/java/parquet/avro/projection/AvroCustomField.java create mode 100644 parquet-avro/src/main/java/parquet/avro/projection/AvroProjection.java create mode 100644 parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java create mode 100644 parquet-cascading/src/test/avro/test.avdl create mode 100644 parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java diff --git a/parquet-avro/src/main/java/parquet/avro/projection/AvroCustomField.java b/parquet-avro/src/main/java/parquet/avro/projection/AvroCustomField.java new file mode 100644 index 0000000000..3267f4abe8 --- /dev/null +++ b/parquet-avro/src/main/java/parquet/avro/projection/AvroCustomField.java @@ -0,0 +1,38 @@ +/* + * 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.avro.projection; + +import org.apache.avro.Schema; + +//remember the original field position +public class AvroCustomField extends Schema.Field { + Schema schema; + Schema.Field field; + + public AvroCustomField(Schema schema, Schema.Field field) { + super(field.name(), schema, field.doc(), field.defaultValue()); + this.schema = schema; + this.field = field; + } + + public int pos() { + return field.pos(); + } + +} \ No newline at end of file diff --git a/parquet-avro/src/main/java/parquet/avro/projection/AvroProjection.java b/parquet-avro/src/main/java/parquet/avro/projection/AvroProjection.java new file mode 100644 index 0000000000..7e2230176b --- /dev/null +++ b/parquet-avro/src/main/java/parquet/avro/projection/AvroProjection.java @@ -0,0 +1,157 @@ +/* + * 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.avro.projection; + +import org.apache.avro.Schema; +import org.apache.avro.Schema.Type.*; + +import java.util.*; + +// originally based on https://raw.githubusercontent.com/epishkin/scalding/parquet_avro/scalding-parquet/src/main/scala/com/twitter/scalding/parquet/avro/Projection.scala + +/** + * Helps create projections for Avro schemas. + */ +public class AvroProjection { + /** + * Create a projection for {{schema}} that includes {{fields}}. + */ + public static Schema createProjection(Schema schema, String[] fields) { + return createProjection(schema, fields, null); + } + + + private static Schema createProjection(Schema schema, String[] fields, String parentFieldName) { + switch(schema.getType()) { + case RECORD: + return createRecordProjection(schema, fields, parentFieldName); + case UNION: + return createUnionProjection(schema, fields, parentFieldName); + case ARRAY: + return createArrayProjection(schema, fields, parentFieldName); + default: + String fieldInfo = schema.getType().toString(); + if (parentFieldName != null) { + fieldInfo = parentFieldName + fieldInfo; + } + String children = convertFieldNamesToString(fields); + throw new RuntimeException("Projection doesn't support schema type " + fieldInfo + " with fields: " + children); + } + } + + private static Schema createRecordProjection(Schema schema, String[] fields, String parentFieldName) { + // Take the head of any nested properties, "parent.fieldX" => "parent" + List nestedFields = new LinkedList(); + for (int i = 0; i < fields.length; i++) { + if (fields[i].contains(".")) { + nestedFields.add(fields[i].split("\\.")[0]); + } + } + List directFields = new LinkedList(); + Collections.copy(directFields, nestedFields); + directFields.addAll(Arrays.asList(fields)); + + List schemaFields = schema.getFields(); + validateRequestedFields(schema, schemaFields, directFields); + + List projectedFields = new LinkedList(); + + for (Schema.Field field : schemaFields) { + if (directFields.contains(field.name())) { + Schema thisSchema; + // Create projection for the nested field + if (nestedFields.contains(field.name())) { + String prefix = field.name() + "."; + // Find the nested fields and remove the prefix + List children = new LinkedList(); + for (String requestedField : fields) { + if (requestedField.startsWith(prefix)) { + children.add(requestedField.substring(0, prefix.length())); + } + } + thisSchema = createProjection(field.schema(), (String[])children.toArray(), fullFieldName(parentFieldName, field.name())); + } else { + thisSchema = field.schema(); + } + + projectedFields.add(copyField(thisSchema, field)); + } + } + + Schema projection = Schema.createRecord(schema.getName(), schema.getDoc(), schema.getNamespace(), false); + projection.setFields(projectedFields); + return projection; + } + + private static Schema createUnionProjection(Schema schema, String[] fields, String parentFieldName) { + List projectedSchemas = new LinkedList(); + for (Schema nestedSchema : schema.getTypes()) { + if (nestedSchema.getType() == Schema.Type.NULL) { + projectedSchemas.add(nestedSchema); + } else { + projectedSchemas.add(createProjection(nestedSchema, fields, parentFieldName)); + } + } + return Schema.createUnion(projectedSchemas); + } + + private static Schema createArrayProjection(Schema schema, String[] fields, String parentFieldName) { + return Schema.createArray( + createProjection(schema.getElementType(), fields, parentFieldName)); + } + + private static void validateRequestedFields(Schema schema, List schemaFields, List fieldNames) throws RuntimeException { + Set schemaFieldNames = new HashSet(); + for (Schema.Field field: schemaFields) { + schemaFieldNames.add(field.name()); + } + for (String field: fieldNames) { + if (!field.contains("\\.") && !schemaFieldNames.contains(field)) { + String full_name = schema.getFullName(); + final String supported_fields = Arrays.toString(schemaFieldNames.toArray()); + throw new RuntimeException( + "Field " + field + " not found in schema " + full_name + ". " + + "Supported fields are: " + supported_fields + ); + } + } + } + + private static Schema.Field copyField(Schema schema, Schema.Field field) { + return (new AvroCustomField(schema, field)); + } + + private static String fullFieldName(String parentFieldName, String fieldName) { + if (parentFieldName == null) { + return null; + } else { + return (parentFieldName + "." + fieldName); + } + } + + private static String convertFieldNamesToString(String[] fields) { + StringBuilder sb = new StringBuilder(); + for (String field : fields) { + if (sb.length() > 0) sb.append(", "); + sb.append(field); + } + return sb.toString(); + } + +} diff --git a/parquet-cascading/pom.xml b/parquet-cascading/pom.xml index 209818b50b..c44eab077b 100644 --- a/parquet-cascading/pom.xml +++ b/parquet-cascading/pom.xml @@ -55,6 +55,11 @@ parquet-thrift ${project.version} + + com.twitter + parquet-avro + ${project.version} + org.apache.hadoop hadoop-client @@ -114,6 +119,23 @@ + + org.apache.avro + avro-maven-plugin + 1.7.6 + + + generate-test-sources + + idl-protocol + + + ${project.basedir}/src/test/avro/ + + + + + 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..61424a434f --- /dev/null +++ b/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java @@ -0,0 +1,98 @@ +/* + * 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 org.apache.avro.generic.IndexedRecord; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hadoop.mapred.OutputCollector; +import org.apache.hadoop.mapred.RecordReader; + +import cascading.flow.FlowProcess; +import cascading.tap.Tap; +import parquet.avro.AvroReadSupport; +import parquet.avro.AvroWriteSupport; +import parquet.avro.projection.AvroProjection; +import parquet.filter2.predicate.FilterPredicate; +import parquet.hadoop.ParquetInputFormat; +import parquet.hadoop.mapred.DeprecatedParquetInputFormat; +import parquet.hadoop.mapred.DeprecatedParquetOutputFormat; +import parquet.hadoop.metadata.CompressionCodecName; + +public class ParquetAvroScheme extends ParquetValueScheme { + + // In the case of reads, we can read the thrift class from the file metadata + public ParquetAvroScheme() { + this(new parquet.cascading.ParquetValueScheme.Config()); + } + + public ParquetAvroScheme(Class avroClass) { + this(new parquet.cascading.ParquetValueScheme.Config().withRecordClass(avroClass)); + } + + public ParquetAvroScheme(FilterPredicate filterPredicate) { + this(new parquet.cascading.ParquetValueScheme.Config().withFilterPredicate(filterPredicate)); + } + + public ParquetAvroScheme(FilterPredicate filterPredicate, Class avroClass) { + this(new parquet.cascading.ParquetValueScheme.Config().withRecordClass(avroClass).withFilterPredicate(filterPredicate)); + } + + public ParquetAvroScheme(parquet.cascading.ParquetValueScheme.Config config) { + super(config); + } + + @SuppressWarnings("rawtypes") + @Override + public void sourceConfInit(FlowProcess fp, + Tap tap, JobConf jobConf) { + super.sourceConfInit(fp, tap, jobConf); + try { + jobConf.setInputFormat(DeprecatedParquetInputFormat.class); + IndexedRecord record = (IndexedRecord)config.getKlass().getConstructor(null).newInstance(null); + ParquetInputFormat.setReadSupportClass(jobConf, AvroReadSupport.class); + AvroReadSupport.setAvroReadSchema(jobConf, record.getSchema()); + AvroReadSupport.setRequestedProjection(jobConf, AvroProjection.createProjection(record.getSchema(), config.getProjectionString().split(";"))); + ParquetInputFormat.setFilterPredicate(jobConf, config.getFilterPredicate()); + } catch (Exception e) { + System.err.println(e.toString()); + } + } + + @SuppressWarnings("rawtypes") + @Override + public void sinkConfInit(FlowProcess fp, + Tap tap, JobConf jobConf) { + + if (this.config.getKlass() == null) { + throw new IllegalArgumentException("To use ParquetAvroScheme as a sink, you must specify an avro class in the constructor"); + } + + try { + jobConf.setOutputFormat(DeprecatedParquetOutputFormat.class); + DeprecatedParquetOutputFormat.setWriteSupportClass(jobConf, AvroWriteSupport.class); + DeprecatedParquetOutputFormat.setCompressOutput(jobConf, true); + DeprecatedParquetOutputFormat.setCompression(jobConf, CompressionCodecName.SNAPPY); + IndexedRecord record = (IndexedRecord)config.getKlass().getConstructor(null).newInstance(null); + AvroWriteSupport.setSchema(jobConf, record.getSchema()); + } catch (Exception e) { + System.err.println(e.toString()); + } + } +} + diff --git a/parquet-cascading/src/test/avro/test.avdl b/parquet-cascading/src/test/avro/test.avdl new file mode 100644 index 0000000000..a20baeb4f8 --- /dev/null +++ b/parquet-cascading/src/test/avro/test.avdl @@ -0,0 +1,8 @@ +@namespace("avrotestclasses") +protocol AvroTest { + record Name { + string first_name; + // optional last name + union { null, string } last_name = null; + } +} diff --git a/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java b/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java new file mode 100644 index 0000000000..26d93eb393 --- /dev/null +++ b/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java @@ -0,0 +1,169 @@ +/* + * 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 cascading.flow.Flow; +import cascading.flow.FlowProcess; +import cascading.flow.hadoop.HadoopFlowConnector; +import cascading.operation.BaseOperation; +import cascading.operation.Function; +import cascading.operation.FunctionCall; +import cascading.pipe.Each; +import cascading.pipe.Pipe; +import cascading.scheme.Scheme; +import cascading.scheme.hadoop.TextLine; +import cascading.tap.Tap; +import cascading.tap.hadoop.Hfs; +import cascading.tuple.Fields; +import cascading.tuple.Tuple; +import cascading.tuple.TupleEntry; +import org.apache.commons.io.FileUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.mapred.JobConf; +import org.junit.Test; +import parquet.avro.AvroParquetWriter; +import parquet.hadoop.metadata.CompressionCodecName; + +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import avrotestclasses.Name; + +/** + * Created by schuman on 23/03/15. + */ +public class TestParquetAvroScheme { + final String txtInputPath = "src/test/resources/names.txt"; + final String parquetInputPath = "target/test/ParquetAvroScheme/names-parquet-in"; + final String parquetOutputPath = "target/test/ParquetAvroScheme/names-parquet-out"; + final String txtOutputPath = "target/test/ParquetAvroScheme/names-txt-out"; + + @Test + public void testWrite() throws Exception { + Path path = new Path(parquetOutputPath); + JobConf jobConf = new JobConf(); + final FileSystem fs = path.getFileSystem(jobConf); + if (fs.exists(path)) fs.delete(path, true); + + Scheme sourceScheme = new TextLine( new Fields( "first", "last" ) ); + Tap source = new Hfs(sourceScheme, txtInputPath); + + Scheme sinkScheme = new ParquetAvroScheme(Name.class); + Tap sink = new Hfs(sinkScheme, parquetOutputPath); + + Pipe assembly = new Pipe( "namecp" ); + assembly = new Each(assembly, new PackAvroFunction()); + HadoopFlowConnector hadoopFlowConnector = new HadoopFlowConnector(); + Flow flow = hadoopFlowConnector.connect("namecp", source, sink, assembly); + + flow.complete(); + + assertTrue(fs.exists(new Path(parquetOutputPath))); + assertTrue(fs.exists(new Path(parquetOutputPath + "/_SUCCESS"))); + } + + @Test + public void testRead() throws Exception { + doRead(new ParquetAvroScheme(Name.class)); + } + + /*@Test + // TODO: make scheme work without requiring code generation + public void testReadWithoutClass() throws Exception { + doRead(new ParquetAvroScheme(GenericRecord.class)); + }*/ + + private void doRead(Scheme sourceScheme) throws Exception { + createFileForRead(); + + Path path = new Path(txtOutputPath); + final FileSystem fs = path.getFileSystem(new Configuration()); + if (fs.exists(path)) fs.delete(path, true); + + Tap source = new Hfs(sourceScheme, parquetInputPath); + + Scheme sinkScheme = new TextLine(new Fields("first", "last")); + Tap sink = new Hfs(sinkScheme, txtOutputPath); + + Pipe assembly = new Pipe( "namecp" ); + assembly = new Each(assembly, new UnpackAvroFunction()); + Flow flow = new HadoopFlowConnector().connect("namecp", source, sink, assembly); + + flow.complete(); + String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); + assertEquals("Alice\tPractice\nBob\tHope\nCharlie\tHorse\n", result); + } + + + private void createFileForRead() throws Exception { + final Path fileToCreate = new Path(parquetInputPath+"/names.parquet"); + + final Configuration conf = new Configuration(); + final FileSystem fs = fileToCreate.getFileSystem(conf); + if (fs.exists(fileToCreate)) fs.delete(fileToCreate, true); + + AvroParquetWriter writer = new AvroParquetWriter(fileToCreate, Name.getClassSchema(), CompressionCodecName.UNCOMPRESSED, 100, 100); + Name n1 = new Name(); + n1.setFirstName("Alice"); + n1.setLastName("Practice"); + Name n2 = new Name(); + n2.setFirstName("Bob"); + n2.setLastName("Hope"); + Name n3 = new Name(); + n3.setFirstName("Charlie"); + n3.setLastName("Horse"); + + writer.write(n1); + writer.write(n2); + writer.write(n3); + writer.close(); + } + + private static class PackAvroFunction extends BaseOperation implements Function { + @Override + public void operate(FlowProcess flowProcess, FunctionCall functionCall) { + TupleEntry arguments = functionCall.getArguments(); + Tuple result = new Tuple(); + + Name name = new Name(); + name.setFirstName(arguments.getString(0)); + name.setLastName(arguments.getString(1)); + + result.add(name); + functionCall.getOutputCollector().add(result); + } + } + + private static class UnpackAvroFunction extends BaseOperation implements Function { + @Override + public void operate(FlowProcess flowProcess, FunctionCall functionCall) { + TupleEntry arguments = functionCall.getArguments(); + Tuple result = new Tuple(); + + Name name = (Name) arguments.get(0); + result.add(name.getFirstName()); + result.add(name.getLastName()); + functionCall.getOutputCollector().add(result); + } + } +} From 4ba68d1541b0fb904f96d7e12a422bfe359774d8 Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Thu, 2 Apr 2015 13:04:46 +0300 Subject: [PATCH 2/9] Exclude Avro IDL files for Rat plugin --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 5efbe019e3..20556307da 100644 --- a/pom.xml +++ b/pom.xml @@ -381,6 +381,7 @@ **/*.parquet **/*.avro + **/*.avdl **/*.json **/names.txt **/*.avsc From 7ec2e20f4dbf3f16930f039dd39d3194421061a3 Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Fri, 3 Apr 2015 04:30:34 +0300 Subject: [PATCH 3/9] Added test for read with projection --- .../parquet/cascading/ParquetAvroScheme.java | 4 +-- .../cascading/TestParquetAvroScheme.java | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java b/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java index 61424a434f..81b23fde8b 100644 --- a/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java +++ b/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java @@ -64,7 +64,7 @@ public void sourceConfInit(FlowProcess fp, super.sourceConfInit(fp, tap, jobConf); try { jobConf.setInputFormat(DeprecatedParquetInputFormat.class); - IndexedRecord record = (IndexedRecord)config.getKlass().getConstructor(null).newInstance(null); + IndexedRecord record = config.getKlass().getConstructor(null).newInstance(null); ParquetInputFormat.setReadSupportClass(jobConf, AvroReadSupport.class); AvroReadSupport.setAvroReadSchema(jobConf, record.getSchema()); AvroReadSupport.setRequestedProjection(jobConf, AvroProjection.createProjection(record.getSchema(), config.getProjectionString().split(";"))); @@ -88,7 +88,7 @@ public void sinkConfInit(FlowProcess fp, DeprecatedParquetOutputFormat.setWriteSupportClass(jobConf, AvroWriteSupport.class); DeprecatedParquetOutputFormat.setCompressOutput(jobConf, true); DeprecatedParquetOutputFormat.setCompression(jobConf, CompressionCodecName.SNAPPY); - IndexedRecord record = (IndexedRecord)config.getKlass().getConstructor(null).newInstance(null); + IndexedRecord record = config.getKlass().getConstructor(null).newInstance(null); AvroWriteSupport.setSchema(jobConf, record.getSchema()); } catch (Exception e) { System.err.println(e.toString()); diff --git a/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java b/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java index 26d93eb393..56437dbbc2 100644 --- a/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java +++ b/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java @@ -87,13 +87,21 @@ public void testRead() throws Exception { doRead(new ParquetAvroScheme(Name.class)); } + @Test + public void testReadWithProjection() throws Exception { + ParquetValueScheme.Config config = new ParquetValueScheme.Config() + .withProjectionString("first") + .withRecordClass(Name.class); + doReadWithProjection(new ParquetAvroScheme(config)); + } + /*@Test // TODO: make scheme work without requiring code generation public void testReadWithoutClass() throws Exception { doRead(new ParquetAvroScheme(GenericRecord.class)); }*/ - private void doRead(Scheme sourceScheme) throws Exception { + private void prepareRead(Scheme sourceScheme, String[] fieldNames) throws Exception { createFileForRead(); Path path = new Path(txtOutputPath); @@ -102,18 +110,29 @@ private void doRead(Scheme sourceScheme) throws Exception { Tap source = new Hfs(sourceScheme, parquetInputPath); - Scheme sinkScheme = new TextLine(new Fields("first", "last")); + Scheme sinkScheme = new TextLine(new Fields(fieldNames)); Tap sink = new Hfs(sinkScheme, txtOutputPath); - Pipe assembly = new Pipe( "namecp" ); + Pipe assembly = new Pipe("namecp"); assembly = new Each(assembly, new UnpackAvroFunction()); - Flow flow = new HadoopFlowConnector().connect("namecp", source, sink, assembly); + Flow flow = new HadoopFlowConnector().connect("namecp", source, sink, assembly); flow.complete(); + } + + private void doRead(Scheme sourceScheme) throws Exception { + String[] fields = {"first", "last"}; + prepareRead(sourceScheme, fields); String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); assertEquals("Alice\tPractice\nBob\tHope\nCharlie\tHorse\n", result); } + private void doReadWithProjection(Scheme sourceScheme) throws Exception { + String[] fields = {"first"}; + prepareRead(sourceScheme, fields); + String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); + assertEquals("Alice\nBob\nCharlie\n", result); + } private void createFileForRead() throws Exception { final Path fileToCreate = new Path(parquetInputPath+"/names.parquet"); From 9c948a818530ec98d6d91a48e7143c5d3b018bf3 Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Fri, 3 Apr 2015 04:56:57 +0300 Subject: [PATCH 4/9] Moving ParquetAvroScheme to its own module --- parquet-cascading-avro/pom.xml | 118 ++++++++++++++++++ .../parquet/cascading/ParquetAvroScheme.java | 0 .../src/test/avro/test.avdl | 0 .../cascading/TestParquetAvroScheme.java | 0 .../src/test/resources/names.txt | 3 + 5 files changed, 121 insertions(+) create mode 100644 parquet-cascading-avro/pom.xml rename {parquet-cascading => parquet-cascading-avro}/src/main/java/parquet/cascading/ParquetAvroScheme.java (100%) rename {parquet-cascading => parquet-cascading-avro}/src/test/avro/test.avdl (100%) rename {parquet-cascading => parquet-cascading-avro}/src/test/java/parquet/cascading/TestParquetAvroScheme.java (100%) create mode 100644 parquet-cascading-avro/src/test/resources/names.txt diff --git a/parquet-cascading-avro/pom.xml b/parquet-cascading-avro/pom.xml new file mode 100644 index 0000000000..2c999d0502 --- /dev/null +++ b/parquet-cascading-avro/pom.xml @@ -0,0 +1,118 @@ + + + + com.twitter + parquet + ../pom.xml + 1.6.0rc8-SNAPSHOT + + + 4.0.0 + + parquet-cascading-avro + jar + + Apache Parquet Cascading (Incubating) + https://parquet.incubator.apache.org + + + + conjars.org + http://conjars.org/repo + + + + + + com.twitter + parquet-column + ${project.version} + + + com.twitter + parquet-hadoop + ${project.version} + + + com.twitter + parquet-avro + ${project.version} + + + org.apache.hadoop + hadoop-client + ${hadoop.version} + provided + + + log4j + log4j + ${log4j.version} + provided + + + com.twitter + parquet-column + ${project.version} + test-jar + test + + + org.mockito + mockito-all + 1.9.5 + test + + + cascading + cascading-hadoop + ${cascading.version} + provided + + + + + + + maven-enforcer-plugin + + + org.apache.maven.plugins + maven-jar-plugin + + + org.apache.avro + avro-maven-plugin + ${avro.version} + + + generate-test-sources + + idl-protocol + + + ${project.basedir}/src/test/avro/ + + + + + + + diff --git a/parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java b/parquet-cascading-avro/src/main/java/parquet/cascading/ParquetAvroScheme.java similarity index 100% rename from parquet-cascading/src/main/java/parquet/cascading/ParquetAvroScheme.java rename to parquet-cascading-avro/src/main/java/parquet/cascading/ParquetAvroScheme.java diff --git a/parquet-cascading/src/test/avro/test.avdl b/parquet-cascading-avro/src/test/avro/test.avdl similarity index 100% rename from parquet-cascading/src/test/avro/test.avdl rename to parquet-cascading-avro/src/test/avro/test.avdl diff --git a/parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java b/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java similarity index 100% rename from parquet-cascading/src/test/java/parquet/cascading/TestParquetAvroScheme.java rename to parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java diff --git a/parquet-cascading-avro/src/test/resources/names.txt b/parquet-cascading-avro/src/test/resources/names.txt new file mode 100644 index 0000000000..e2d0408c8f --- /dev/null +++ b/parquet-cascading-avro/src/test/resources/names.txt @@ -0,0 +1,3 @@ +Alice Practive +Bob Hope +Charlie Horse From 1513dc9ac3927bd08c2eb9f0b6bb592f4933b45d Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Fri, 3 Apr 2015 05:00:09 +0300 Subject: [PATCH 5/9] Removing now unnecessary avro dependencies from parquet-cascading --- parquet-avro/pom.xml | 4 ---- parquet-cascading/pom.xml | 22 ---------------------- pom.xml | 2 ++ 3 files changed, 2 insertions(+), 26 deletions(-) diff --git a/parquet-avro/pom.xml b/parquet-avro/pom.xml index 0d511a3a46..e182b4810b 100644 --- a/parquet-avro/pom.xml +++ b/parquet-avro/pom.xml @@ -32,10 +32,6 @@ Apache Parquet Avro (Incubating) https://parquet.incubator.apache.org - - 1.7.6 - - com.twitter diff --git a/parquet-cascading/pom.xml b/parquet-cascading/pom.xml index c44eab077b..209818b50b 100644 --- a/parquet-cascading/pom.xml +++ b/parquet-cascading/pom.xml @@ -55,11 +55,6 @@ parquet-thrift ${project.version} - - com.twitter - parquet-avro - ${project.version} - org.apache.hadoop hadoop-client @@ -119,23 +114,6 @@ - - org.apache.avro - avro-maven-plugin - 1.7.6 - - - generate-test-sources - - idl-protocol - - - ${project.basedir}/src/test/avro/ - - - - - diff --git a/pom.xml b/pom.xml index 20556307da..258c309381 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,7 @@ 1.2.17 1.6.0rc1 thrift + 1.7.6 2.10.4 2.10 @@ -119,6 +120,7 @@ parquet-avro parquet-cascading + parquet-cascading-avro parquet-column parquet-common parquet-encoding From 337136417b6c175ed26f173285675218f3fe6423 Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Fri, 3 Apr 2015 05:39:08 +0300 Subject: [PATCH 6/9] Adding test with filter predicate --- parquet-cascading-avro/pom.xml | 21 ++++++++++++++----- .../cascading/TestParquetAvroScheme.java | 19 +++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/parquet-cascading-avro/pom.xml b/parquet-cascading-avro/pom.xml index 2c999d0502..44b3a19209 100644 --- a/parquet-cascading-avro/pom.xml +++ b/parquet-cascading-avro/pom.xml @@ -50,11 +50,11 @@ parquet-hadoop ${project.version} - - com.twitter - parquet-avro - ${project.version} - + + com.twitter + parquet-avro + ${project.version} + org.apache.hadoop hadoop-client @@ -74,6 +74,17 @@ test-jar test + + com.twitter + parquet-cascading + ${project.version} + + + com.twitter + parquet-thrift + + + org.mockito mockito-all diff --git a/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java b/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java index 56437dbbc2..bd7858bf8e 100644 --- a/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java +++ b/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java @@ -40,7 +40,10 @@ import org.apache.hadoop.mapred.JobConf; import org.junit.Test; import parquet.avro.AvroParquetWriter; +import parquet.filter2.predicate.FilterPredicate; +import parquet.filter2.predicate.FilterApi; import parquet.hadoop.metadata.CompressionCodecName; +import parquet.io.api.Binary; import java.io.File; @@ -95,6 +98,15 @@ public void testReadWithProjection() throws Exception { doReadWithProjection(new ParquetAvroScheme(config)); } + @Test + public void testReadWithFilter() throws Exception { + FilterPredicate filter = FilterApi.eq(FilterApi.binaryColumn("first"), Binary.fromString("Bob")); + ParquetValueScheme.Config config = new ParquetValueScheme.Config() + .withFilterPredicate(filter) + .withRecordClass(Name.class); + doReadWithFilter(new ParquetAvroScheme(config)); + } + /*@Test // TODO: make scheme work without requiring code generation public void testReadWithoutClass() throws Exception { @@ -134,6 +146,13 @@ private void doReadWithProjection(Scheme sourceScheme) throws Exception { assertEquals("Alice\nBob\nCharlie\n", result); } + private void doReadWithFilter(Scheme sourceScheme) throws Exception { + String[] fields = {"first", "last"}; + prepareRead(sourceScheme, fields); + String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); + assertEquals("Bob\tHope\n", result); + } + private void createFileForRead() throws Exception { final Path fileToCreate = new Path(parquetInputPath+"/names.parquet"); From 76dbbc10297b67c5d15f4681610ab874feef9e13 Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Sun, 5 Apr 2015 11:21:52 +0300 Subject: [PATCH 7/9] Reformatting, reducing Thrift dependencies in ParquetValueScheme --- parquet-cascading-avro/pom.xml | 2 +- .../parquet/cascading/ParquetAvroScheme.java | 58 ++-- .../cascading/TestParquetAvroScheme.java | 286 +++++++++--------- .../parquet/cascading/ParquetTBaseScheme.java | 15 + .../parquet/cascading/ParquetValueScheme.java | 16 +- 5 files changed, 194 insertions(+), 183 deletions(-) diff --git a/parquet-cascading-avro/pom.xml b/parquet-cascading-avro/pom.xml index 44b3a19209..2bf3ed8abb 100644 --- a/parquet-cascading-avro/pom.xml +++ b/parquet-cascading-avro/pom.xml @@ -29,7 +29,7 @@ parquet-cascading-avro jar - Apache Parquet Cascading (Incubating) + Apache Parquet Cascading Avro (Incubating) https://parquet.incubator.apache.org diff --git a/parquet-cascading-avro/src/main/java/parquet/cascading/ParquetAvroScheme.java b/parquet-cascading-avro/src/main/java/parquet/cascading/ParquetAvroScheme.java index 81b23fde8b..80dfa97484 100644 --- a/parquet-cascading-avro/src/main/java/parquet/cascading/ParquetAvroScheme.java +++ b/parquet-cascading-avro/src/main/java/parquet/cascading/ParquetAvroScheme.java @@ -32,7 +32,6 @@ import parquet.hadoop.ParquetInputFormat; import parquet.hadoop.mapred.DeprecatedParquetInputFormat; import parquet.hadoop.mapred.DeprecatedParquetOutputFormat; -import parquet.hadoop.metadata.CompressionCodecName; public class ParquetAvroScheme extends ParquetValueScheme { @@ -60,39 +59,52 @@ public ParquetAvroScheme(parquet.cascading.ParquetValueScheme.Config config) @SuppressWarnings("rawtypes") @Override public void sourceConfInit(FlowProcess fp, - Tap tap, JobConf jobConf) { + Tap tap, JobConf jobConf) { super.sourceConfInit(fp, tap, jobConf); try { - jobConf.setInputFormat(DeprecatedParquetInputFormat.class); - IndexedRecord record = config.getKlass().getConstructor(null).newInstance(null); - ParquetInputFormat.setReadSupportClass(jobConf, AvroReadSupport.class); - AvroReadSupport.setAvroReadSchema(jobConf, record.getSchema()); - AvroReadSupport.setRequestedProjection(jobConf, AvroProjection.createProjection(record.getSchema(), config.getProjectionString().split(";"))); - ParquetInputFormat.setFilterPredicate(jobConf, config.getFilterPredicate()); + jobConf.setInputFormat(DeprecatedParquetInputFormat.class); + ParquetInputFormat.setReadSupportClass(jobConf, AvroReadSupport.class); } catch (Exception e) { - System.err.println(e.toString()); + System.err.println(e.toString()); } } @SuppressWarnings("rawtypes") @Override public void sinkConfInit(FlowProcess fp, - Tap tap, JobConf jobConf) { + Tap tap, JobConf jobConf) { + DeprecatedParquetOutputFormat.setAsOutputFormat(jobConf); + DeprecatedParquetOutputFormat.setWriteSupportClass(jobConf, AvroWriteSupport.class); + IndexedRecord record = createRecordInstance(); + AvroWriteSupport.setSchema(jobConf, record.getSchema()); + } - if (this.config.getKlass() == null) { - throw new IllegalArgumentException("To use ParquetAvroScheme as a sink, you must specify an avro class in the constructor"); - } + @Override + protected void setRecordClass(JobConf jobConf) { + IndexedRecord record = createRecordInstance(); + AvroReadSupport.setAvroReadSchema(jobConf, record.getSchema()); + } + + @Override + protected void setProjectionPushdown(JobConf jobConf) { + if (this.config.getProjectionString() != null) { + IndexedRecord record = createRecordInstance(); + AvroReadSupport.setRequestedProjection( + jobConf, + AvroProjection.createProjection(record.getSchema(), config.getProjectionString().split(";"))); + } + } - try { - jobConf.setOutputFormat(DeprecatedParquetOutputFormat.class); - DeprecatedParquetOutputFormat.setWriteSupportClass(jobConf, AvroWriteSupport.class); - DeprecatedParquetOutputFormat.setCompressOutput(jobConf, true); - DeprecatedParquetOutputFormat.setCompression(jobConf, CompressionCodecName.SNAPPY); - IndexedRecord record = config.getKlass().getConstructor(null).newInstance(null); - AvroWriteSupport.setSchema(jobConf, record.getSchema()); - } catch (Exception e) { - System.err.println(e.toString()); - } + private IndexedRecord createRecordInstance() { + if (config.getClass() == null) { + throw new IllegalArgumentException("Unable to create instance of unknown Avro record class"); + } + try { + IndexedRecord record = config.getKlass().getConstructor(null).newInstance(null); + return record; + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Unable to create Avro Record of class " + config.getKlass().getName(), e); + } } } diff --git a/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java b/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java index bd7858bf8e..37cca47d24 100644 --- a/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java +++ b/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java @@ -56,152 +56,146 @@ * Created by schuman on 23/03/15. */ public class TestParquetAvroScheme { - final String txtInputPath = "src/test/resources/names.txt"; - final String parquetInputPath = "target/test/ParquetAvroScheme/names-parquet-in"; - final String parquetOutputPath = "target/test/ParquetAvroScheme/names-parquet-out"; - final String txtOutputPath = "target/test/ParquetAvroScheme/names-txt-out"; - - @Test - public void testWrite() throws Exception { - Path path = new Path(parquetOutputPath); - JobConf jobConf = new JobConf(); - final FileSystem fs = path.getFileSystem(jobConf); - if (fs.exists(path)) fs.delete(path, true); - - Scheme sourceScheme = new TextLine( new Fields( "first", "last" ) ); - Tap source = new Hfs(sourceScheme, txtInputPath); - - Scheme sinkScheme = new ParquetAvroScheme(Name.class); - Tap sink = new Hfs(sinkScheme, parquetOutputPath); - - Pipe assembly = new Pipe( "namecp" ); - assembly = new Each(assembly, new PackAvroFunction()); - HadoopFlowConnector hadoopFlowConnector = new HadoopFlowConnector(); - Flow flow = hadoopFlowConnector.connect("namecp", source, sink, assembly); - - flow.complete(); - - assertTrue(fs.exists(new Path(parquetOutputPath))); - assertTrue(fs.exists(new Path(parquetOutputPath + "/_SUCCESS"))); - } - - @Test - public void testRead() throws Exception { - doRead(new ParquetAvroScheme(Name.class)); + final String txtInputPath = "src/test/resources/names.txt"; + final String parquetInputPath = "target/test/ParquetAvroScheme/names-parquet-in"; + final String parquetOutputPath = "target/test/ParquetAvroScheme/names-parquet-out"; + final String txtOutputPath = "target/test/ParquetAvroScheme/names-txt-out"; + + @Test + public void testWrite() throws Exception { + Path path = new Path(parquetOutputPath); + JobConf jobConf = new JobConf(); + final FileSystem fs = path.getFileSystem(jobConf); + if (fs.exists(path)) fs.delete(path, true); + + Scheme sourceScheme = new TextLine( new Fields( "first", "last" ) ); + Tap source = new Hfs(sourceScheme, txtInputPath); + + Scheme sinkScheme = new ParquetAvroScheme(Name.class); + Tap sink = new Hfs(sinkScheme, parquetOutputPath); + + Pipe assembly = new Pipe( "namecp" ); + assembly = new Each(assembly, new PackAvroFunction()); + HadoopFlowConnector hadoopFlowConnector = new HadoopFlowConnector(); + Flow flow = hadoopFlowConnector.connect("namecp", source, sink, assembly); + + flow.complete(); + + assertTrue(fs.exists(new Path(parquetOutputPath))); + assertTrue(fs.exists(new Path(parquetOutputPath + "/_SUCCESS"))); + } + + @Test + public void testRead() throws Exception { + doRead(new ParquetAvroScheme(Name.class)); + } + + @Test + public void testReadWithProjection() throws Exception { + ParquetValueScheme.Config config = new ParquetValueScheme.Config() + .withProjectionString("first") + .withRecordClass(Name.class); + doReadWithProjection(new ParquetAvroScheme(config)); + } + + @Test + public void testReadWithFilter() throws Exception { + FilterPredicate filter = FilterApi.eq(FilterApi.binaryColumn("first"), Binary.fromString("Bob")); + ParquetValueScheme.Config config = new ParquetValueScheme.Config() + .withFilterPredicate(filter) + .withRecordClass(Name.class); + doReadWithFilter(new ParquetAvroScheme(config)); + } + + private void prepareRead(Scheme sourceScheme, String[] fieldNames) throws Exception { + createFileForRead(); + + Path path = new Path(txtOutputPath); + final FileSystem fs = path.getFileSystem(new Configuration()); + if (fs.exists(path)) fs.delete(path, true); + + Tap source = new Hfs(sourceScheme, parquetInputPath); + + Scheme sinkScheme = new TextLine(new Fields(fieldNames)); + Tap sink = new Hfs(sinkScheme, txtOutputPath); + + Pipe assembly = new Pipe("namecp"); + assembly = new Each(assembly, new UnpackAvroFunction()); + Flow flow = new HadoopFlowConnector().connect("namecp", source, sink, assembly); + + flow.complete(); + } + + private void doRead(Scheme sourceScheme) throws Exception { + String[] fields = {"first", "last"}; + prepareRead(sourceScheme, fields); + String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); + assertEquals("Alice\tPractice\nBob\tHope\nCharlie\tHorse\n", result); + } + + private void doReadWithProjection(Scheme sourceScheme) throws Exception { + String[] fields = {"first"}; + prepareRead(sourceScheme, fields); + String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); + assertEquals("Alice\nBob\nCharlie\n", result); + } + + private void doReadWithFilter(Scheme sourceScheme) throws Exception { + String[] fields = {"first", "last"}; + prepareRead(sourceScheme, fields); + String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); + assertEquals("Bob\tHope\n", result); + } + + private void createFileForRead() throws Exception { + final Path fileToCreate = new Path(parquetInputPath+"/names.parquet"); + + final Configuration conf = new Configuration(); + final FileSystem fs = fileToCreate.getFileSystem(conf); + if (fs.exists(fileToCreate)) fs.delete(fileToCreate, true); + + AvroParquetWriter writer = new AvroParquetWriter(fileToCreate, Name.getClassSchema(), CompressionCodecName.UNCOMPRESSED, 100, 100); + Name n1 = new Name(); + n1.setFirstName("Alice"); + n1.setLastName("Practice"); + Name n2 = new Name(); + n2.setFirstName("Bob"); + n2.setLastName("Hope"); + Name n3 = new Name(); + n3.setFirstName("Charlie"); + n3.setLastName("Horse"); + + writer.write(n1); + writer.write(n2); + writer.write(n3); + writer.close(); + } + + private static class PackAvroFunction extends BaseOperation implements Function { + @Override + public void operate(FlowProcess flowProcess, FunctionCall functionCall) { + TupleEntry arguments = functionCall.getArguments(); + Tuple result = new Tuple(); + + Name name = new Name(); + name.setFirstName(arguments.getString(0)); + name.setLastName(arguments.getString(1)); + + result.add(name); + functionCall.getOutputCollector().add(result); } - - @Test - public void testReadWithProjection() throws Exception { - ParquetValueScheme.Config config = new ParquetValueScheme.Config() - .withProjectionString("first") - .withRecordClass(Name.class); - doReadWithProjection(new ParquetAvroScheme(config)); - } - - @Test - public void testReadWithFilter() throws Exception { - FilterPredicate filter = FilterApi.eq(FilterApi.binaryColumn("first"), Binary.fromString("Bob")); - ParquetValueScheme.Config config = new ParquetValueScheme.Config() - .withFilterPredicate(filter) - .withRecordClass(Name.class); - doReadWithFilter(new ParquetAvroScheme(config)); - } - - /*@Test - // TODO: make scheme work without requiring code generation - public void testReadWithoutClass() throws Exception { - doRead(new ParquetAvroScheme(GenericRecord.class)); - }*/ - - private void prepareRead(Scheme sourceScheme, String[] fieldNames) throws Exception { - createFileForRead(); - - Path path = new Path(txtOutputPath); - final FileSystem fs = path.getFileSystem(new Configuration()); - if (fs.exists(path)) fs.delete(path, true); - - Tap source = new Hfs(sourceScheme, parquetInputPath); - - Scheme sinkScheme = new TextLine(new Fields(fieldNames)); - Tap sink = new Hfs(sinkScheme, txtOutputPath); - - Pipe assembly = new Pipe("namecp"); - assembly = new Each(assembly, new UnpackAvroFunction()); - Flow flow = new HadoopFlowConnector().connect("namecp", source, sink, assembly); - - flow.complete(); - } - - private void doRead(Scheme sourceScheme) throws Exception { - String[] fields = {"first", "last"}; - prepareRead(sourceScheme, fields); - String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); - assertEquals("Alice\tPractice\nBob\tHope\nCharlie\tHorse\n", result); - } - - private void doReadWithProjection(Scheme sourceScheme) throws Exception { - String[] fields = {"first"}; - prepareRead(sourceScheme, fields); - String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); - assertEquals("Alice\nBob\nCharlie\n", result); - } - - private void doReadWithFilter(Scheme sourceScheme) throws Exception { - String[] fields = {"first", "last"}; - prepareRead(sourceScheme, fields); - String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); - assertEquals("Bob\tHope\n", result); - } - - private void createFileForRead() throws Exception { - final Path fileToCreate = new Path(parquetInputPath+"/names.parquet"); - - final Configuration conf = new Configuration(); - final FileSystem fs = fileToCreate.getFileSystem(conf); - if (fs.exists(fileToCreate)) fs.delete(fileToCreate, true); - - AvroParquetWriter writer = new AvroParquetWriter(fileToCreate, Name.getClassSchema(), CompressionCodecName.UNCOMPRESSED, 100, 100); - Name n1 = new Name(); - n1.setFirstName("Alice"); - n1.setLastName("Practice"); - Name n2 = new Name(); - n2.setFirstName("Bob"); - n2.setLastName("Hope"); - Name n3 = new Name(); - n3.setFirstName("Charlie"); - n3.setLastName("Horse"); - - writer.write(n1); - writer.write(n2); - writer.write(n3); - writer.close(); - } - - private static class PackAvroFunction extends BaseOperation implements Function { - @Override - public void operate(FlowProcess flowProcess, FunctionCall functionCall) { - TupleEntry arguments = functionCall.getArguments(); - Tuple result = new Tuple(); - - Name name = new Name(); - name.setFirstName(arguments.getString(0)); - name.setLastName(arguments.getString(1)); - - result.add(name); - functionCall.getOutputCollector().add(result); - } - } - - private static class UnpackAvroFunction extends BaseOperation implements Function { - @Override - public void operate(FlowProcess flowProcess, FunctionCall functionCall) { - TupleEntry arguments = functionCall.getArguments(); - Tuple result = new Tuple(); - - Name name = (Name) arguments.get(0); - result.add(name.getFirstName()); - result.add(name.getLastName()); - functionCall.getOutputCollector().add(result); - } + } + + private static class UnpackAvroFunction extends BaseOperation implements Function { + @Override + public void operate(FlowProcess flowProcess, FunctionCall functionCall) { + TupleEntry arguments = functionCall.getArguments(); + Tuple result = new Tuple(); + + Name name = (Name) arguments.get(0); + result.add(name.getFirstName()); + result.add(name.getLastName()); + functionCall.getOutputCollector().add(result); } + } } diff --git a/parquet-cascading/src/main/java/parquet/cascading/ParquetTBaseScheme.java b/parquet-cascading/src/main/java/parquet/cascading/ParquetTBaseScheme.java index ab84749907..17096402ca 100644 --- a/parquet-cascading/src/main/java/parquet/cascading/ParquetTBaseScheme.java +++ b/parquet-cascading/src/main/java/parquet/cascading/ParquetTBaseScheme.java @@ -29,6 +29,7 @@ import parquet.hadoop.ParquetInputFormat; import parquet.hadoop.mapred.DeprecatedParquetInputFormat; import parquet.hadoop.mapred.DeprecatedParquetOutputFormat; +import parquet.hadoop.thrift.ParquetThriftInputFormat; import parquet.hadoop.thrift.ThriftReadSupport; import parquet.hadoop.thrift.TBaseWriteSupport; import parquet.thrift.TBaseRecordConverter; @@ -77,4 +78,18 @@ public void sinkConfInit(FlowProcess fp, DeprecatedParquetOutputFormat.setWriteSupportClass(jobConf, TBaseWriteSupport.class); TBaseWriteSupport.setThriftClass(jobConf, this.config.getKlass()); } + + @Override + protected void setRecordClass(JobConf jobConf) { + if (config.getKlass() != null) { + ParquetThriftInputFormat.setThriftClass(jobConf, config.getKlass()); + } + } + + @Override + protected void setProjectionPushdown(JobConf jobConf) { + if (this.config.getProjectionString()!= null) { + ThriftReadSupport.setProjectionPushdown(jobConf, this.config.getProjectionString()); + } + } } diff --git a/parquet-cascading/src/main/java/parquet/cascading/ParquetValueScheme.java b/parquet-cascading/src/main/java/parquet/cascading/ParquetValueScheme.java index 0f75479961..331acc4779 100644 --- a/parquet-cascading/src/main/java/parquet/cascading/ParquetValueScheme.java +++ b/parquet-cascading/src/main/java/parquet/cascading/ParquetValueScheme.java @@ -35,8 +35,6 @@ import parquet.filter2.predicate.FilterPredicate; import parquet.hadoop.ParquetInputFormat; import parquet.hadoop.mapred.Container; -import parquet.hadoop.thrift.ParquetThriftInputFormat; -import parquet.hadoop.thrift.ThriftReadSupport; import static parquet.Preconditions.checkNotNull; @@ -105,13 +103,9 @@ public ParquetValueScheme(Config config) { this.config = config; } - private void setProjectionPushdown(JobConf jobConf) { - if (this.config.projectionString!= null) { - ThriftReadSupport.setProjectionPushdown(jobConf, this.config.projectionString); - } - } + protected abstract void setProjectionPushdown(JobConf jobConf); - private void setPredicatePushdown(JobConf jobConf) { + protected void setPredicatePushdown(JobConf jobConf) { if (this.config.filterPredicate != null) { ParquetInputFormat.setFilterPredicate(jobConf, this.config.filterPredicate); } @@ -123,11 +117,7 @@ public void sourceConfInit(FlowProcess jobConfFlowProcess, Tap Date: Sun, 5 Apr 2015 11:55:27 +0300 Subject: [PATCH 8/9] Fixing test --- .../parquet/cascading/TestParquetAvroScheme.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java b/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java index 37cca47d24..ad29ac47ee 100644 --- a/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java +++ b/parquet-cascading-avro/src/test/java/parquet/cascading/TestParquetAvroScheme.java @@ -68,7 +68,7 @@ public void testWrite() throws Exception { final FileSystem fs = path.getFileSystem(jobConf); if (fs.exists(path)) fs.delete(path, true); - Scheme sourceScheme = new TextLine( new Fields( "first", "last" ) ); + Scheme sourceScheme = new TextLine( new Fields( "first_name", "last_name" ) ); Tap source = new Hfs(sourceScheme, txtInputPath); Scheme sinkScheme = new ParquetAvroScheme(Name.class); @@ -93,14 +93,14 @@ public void testRead() throws Exception { @Test public void testReadWithProjection() throws Exception { ParquetValueScheme.Config config = new ParquetValueScheme.Config() - .withProjectionString("first") + .withProjectionString("first_name") .withRecordClass(Name.class); doReadWithProjection(new ParquetAvroScheme(config)); } @Test public void testReadWithFilter() throws Exception { - FilterPredicate filter = FilterApi.eq(FilterApi.binaryColumn("first"), Binary.fromString("Bob")); + FilterPredicate filter = FilterApi.eq(FilterApi.binaryColumn("first_name"), Binary.fromString("Bob")); ParquetValueScheme.Config config = new ParquetValueScheme.Config() .withFilterPredicate(filter) .withRecordClass(Name.class); @@ -127,21 +127,21 @@ private void prepareRead(Scheme sourceScheme, String[] fieldNames) throws Except } private void doRead(Scheme sourceScheme) throws Exception { - String[] fields = {"first", "last"}; + String[] fields = {"first_name", "last_name"}; prepareRead(sourceScheme, fields); String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); assertEquals("Alice\tPractice\nBob\tHope\nCharlie\tHorse\n", result); } private void doReadWithProjection(Scheme sourceScheme) throws Exception { - String[] fields = {"first"}; + String[] fields = {"first_name"}; prepareRead(sourceScheme, fields); String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); - assertEquals("Alice\nBob\nCharlie\n", result); + assertEquals("Alice\tnull\nBob\tnull\nCharlie\tnull\n", result); } private void doReadWithFilter(Scheme sourceScheme) throws Exception { - String[] fields = {"first", "last"}; + String[] fields = {"first_name", "last_name"}; prepareRead(sourceScheme, fields); String result = FileUtils.readFileToString(new File(txtOutputPath + "/part-00000")); assertEquals("Bob\tHope\n", result); From 8477e428e49a0efadb284eb620c9f30eb975222e Mon Sep 17 00:00:00 2001 From: Andre Schumacher Date: Sun, 5 Apr 2015 16:50:56 +0300 Subject: [PATCH 9/9] Propagating interface changes in ParquetValueScheme --- .../parquet/scrooge/ParquetScroogeScheme.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/parquet-scrooge/src/main/java/parquet/scrooge/ParquetScroogeScheme.java b/parquet-scrooge/src/main/java/parquet/scrooge/ParquetScroogeScheme.java index 2745307321..d8990bb3e2 100644 --- a/parquet-scrooge/src/main/java/parquet/scrooge/ParquetScroogeScheme.java +++ b/parquet-scrooge/src/main/java/parquet/scrooge/ParquetScroogeScheme.java @@ -32,6 +32,7 @@ import parquet.hadoop.ParquetOutputFormat; import parquet.hadoop.mapred.DeprecatedParquetInputFormat; import parquet.hadoop.mapred.DeprecatedParquetOutputFormat; +import parquet.hadoop.thrift.ParquetThriftInputFormat; import parquet.hadoop.thrift.ThriftReadSupport; public class ParquetScroogeScheme extends ParquetValueScheme { @@ -66,4 +67,18 @@ public void sourceConfInit(FlowProcess fp, ParquetInputFormat.setReadSupportClass(jobConf, ScroogeReadSupport.class); ThriftReadSupport.setRecordConverterClass(jobConf, ScroogeRecordConverter.class); } + + @Override + protected void setRecordClass(JobConf jobConf) { + if (config.getKlass() != null) { + ParquetThriftInputFormat.setThriftClass(jobConf, config.getKlass()); + } + } + + @Override + protected void setProjectionPushdown(JobConf jobConf) { + if (this.config.getProjectionString()!= null) { + ThriftReadSupport.setProjectionPushdown(jobConf, this.config.getProjectionString()); + } + } }