|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.parquet.examples; |
| 21 | + |
| 22 | +import org.apache.hadoop.fs.Path; |
| 23 | +import org.apache.parquet.example.data.Group; |
| 24 | +import org.apache.parquet.example.data.simple.SimpleGroupFactory; |
| 25 | +import org.apache.parquet.filter2.compat.FilterCompat; |
| 26 | +import org.apache.parquet.filter2.predicate.FilterApi; |
| 27 | +import org.apache.parquet.filter2.predicate.FilterPredicate; |
| 28 | +import org.apache.parquet.filter2.predicate.Operators; |
| 29 | +import org.apache.parquet.hadoop.ParquetReader; |
| 30 | +import org.apache.parquet.hadoop.ParquetWriter; |
| 31 | +import org.apache.parquet.hadoop.example.ExampleParquetWriter; |
| 32 | +import org.apache.parquet.hadoop.example.GroupReadSupport; |
| 33 | +import org.apache.parquet.hadoop.example.GroupWriteSupport; |
| 34 | +import org.apache.parquet.hadoop.metadata.CompressionCodecName; |
| 35 | +import org.apache.parquet.schema.MessageType; |
| 36 | +import org.apache.parquet.schema.PrimitiveType; |
| 37 | +import org.apache.parquet.schema.Types; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.Random; |
| 41 | + |
| 42 | +public class AdvancedFeaturesExample { |
| 43 | + |
| 44 | + public static void main(String[] args) throws IOException { |
| 45 | + String filename = "data/sales.parquet"; |
| 46 | + |
| 47 | + MessageType schema = Types.buildMessage() |
| 48 | + .required(PrimitiveType.PrimitiveTypeName.INT32).named("id") |
| 49 | + .required(PrimitiveType.PrimitiveTypeName.BINARY).named("product") |
| 50 | + .required(PrimitiveType.PrimitiveTypeName.DOUBLE).named("amount") |
| 51 | + .required(PrimitiveType.PrimitiveTypeName.BINARY).named("region") |
| 52 | + .named("sale"); |
| 53 | + |
| 54 | + writeSalesData(filename, schema); |
| 55 | + |
| 56 | + testPredicatePushdown(filename); |
| 57 | + |
| 58 | + testPerformanceOptimization(filename); |
| 59 | + } |
| 60 | + |
| 61 | + private static void writeSalesData(String filename, MessageType schema) throws IOException { |
| 62 | + Path file = new Path(filename); |
| 63 | + |
| 64 | + try (ParquetWriter<Group> writer = ExampleParquetWriter.builder(file) |
| 65 | + .withCompressionCodec(CompressionCodecName.SNAPPY) |
| 66 | + .withRowGroupSize(64 * 1024 * 1024) |
| 67 | + .withPageSize(1024 * 1024) |
| 68 | + .withDictionaryEncoding(true) |
| 69 | + .config(GroupWriteSupport.PARQUET_EXAMPLE_SCHEMA, schema.toString()) |
| 70 | + .build()) { |
| 71 | + |
| 72 | + SimpleGroupFactory factory = new SimpleGroupFactory(schema); |
| 73 | + Random random = new Random(42); |
| 74 | + String[] products = {"Laptop", "Phone", "Tablet", "Monitor", "Keyboard"}; |
| 75 | + String[] regions = {"North", "South", "East", "West"}; |
| 76 | + |
| 77 | + for (int i = 0; i < 10000; i++) { |
| 78 | + Group group = factory.newGroup() |
| 79 | + .append("id", i) |
| 80 | + .append("product", products[random.nextInt(products.length)]) |
| 81 | + .append("amount", 100.0 + random.nextDouble() * 900.0) |
| 82 | + .append("region", regions[random.nextInt(regions.length)]); |
| 83 | + writer.write(group); |
| 84 | + } |
| 85 | + |
| 86 | + System.out.println("Wrote 10000 sales records"); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static void testPredicatePushdown(String filename) throws IOException { |
| 91 | + Path file = new Path(filename); |
| 92 | + |
| 93 | + Operators.DoubleColumn amountColumn = FilterApi.doubleColumn("amount"); |
| 94 | + FilterPredicate amountFilter = FilterApi.gt(amountColumn, 500.0); |
| 95 | + |
| 96 | + long startTime = System.currentTimeMillis(); |
| 97 | + int count = 0; |
| 98 | + try (ParquetReader<Group> reader = ParquetReader.builder(new GroupReadSupport(), file) |
| 99 | + .withFilter(FilterCompat.get(amountFilter)) |
| 100 | + .build()) { |
| 101 | + |
| 102 | + Group group; |
| 103 | + while ((group = reader.read()) != null) { |
| 104 | + count++; |
| 105 | + } |
| 106 | + } |
| 107 | + long filterTime = System.currentTimeMillis() - startTime; |
| 108 | + |
| 109 | + Operators.BinaryColumn regionColumn = FilterApi.binaryColumn("region"); |
| 110 | + FilterPredicate complexFilter = FilterApi.and( |
| 111 | + FilterApi.gt(amountColumn, 500.0), |
| 112 | + FilterApi.eq(regionColumn, org.apache.parquet.io.api.Binary.fromString("North")) |
| 113 | + ); |
| 114 | + |
| 115 | + startTime = System.currentTimeMillis(); |
| 116 | + count = 0; |
| 117 | + try (ParquetReader<Group> reader = ParquetReader.builder(new GroupReadSupport(), file) |
| 118 | + .withFilter(FilterCompat.get(complexFilter)) |
| 119 | + .build()) { |
| 120 | + |
| 121 | + Group group; |
| 122 | + while ((group = reader.read()) != null) { |
| 123 | + count++; |
| 124 | + } |
| 125 | + } |
| 126 | + filterTime = System.currentTimeMillis() - startTime; |
| 127 | + System.out.printf("Found %d records in %dms%n", count, filterTime); |
| 128 | + } |
| 129 | + |
| 130 | + private static void testPerformanceOptimization(String filename) throws IOException { |
| 131 | + Path file = new Path(filename); |
| 132 | + |
| 133 | + System.out.println("Testing default reading performance..."); |
| 134 | + long startTime = System.currentTimeMillis(); |
| 135 | + int count = 0; |
| 136 | + try (ParquetReader<Group> reader = ParquetReader.builder(new GroupReadSupport(), file).build()) { |
| 137 | + Group group; |
| 138 | + while ((group = reader.read()) != null) { |
| 139 | + count++; |
| 140 | + } |
| 141 | + } |
| 142 | + long defaultTime = System.currentTimeMillis() - startTime; |
| 143 | + System.out.printf("Default: Read %d records in %dms%n", count, defaultTime); |
| 144 | + |
| 145 | + System.out.println("Testing projection reading performance..."); |
| 146 | + MessageType projection = Types.buildMessage() |
| 147 | + .required(PrimitiveType.PrimitiveTypeName.INT32).named("id") |
| 148 | + .required(PrimitiveType.PrimitiveTypeName.DOUBLE).named("amount") |
| 149 | + .named("sale"); |
| 150 | + |
| 151 | + startTime = System.currentTimeMillis(); |
| 152 | + count = 0; |
| 153 | + // Note: withProjection is not available in this version, using configuration instead |
| 154 | + org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration(); |
| 155 | + conf.set("parquet.read.schema", projection.toString()); |
| 156 | + |
| 157 | + try (ParquetReader<Group> reader = ParquetReader.builder(new GroupReadSupport(), file) |
| 158 | + .withConf(conf) |
| 159 | + .build()) { |
| 160 | + |
| 161 | + Group group; |
| 162 | + while ((group = reader.read()) != null) { |
| 163 | + count++; |
| 164 | + } |
| 165 | + } |
| 166 | + long projectionTime = System.currentTimeMillis() - startTime; |
| 167 | + System.out.printf("Projection: Read %d records in %dms (%.1fx faster)%n", |
| 168 | + count, projectionTime, (double) defaultTime / projectionTime); |
| 169 | + } |
| 170 | +} |
0 commit comments