|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.wayang.apps.tpch; |
| 20 | + |
| 21 | +import org.apache.wayang.api.JavaPlanBuilder; |
| 22 | +import org.apache.wayang.apps.tpch.data.LineItemTuple; |
| 23 | +import org.apache.wayang.apps.tpch.data.q1.GroupKey; |
| 24 | +import org.apache.wayang.apps.tpch.data.q1.ReturnTuple; |
| 25 | +import org.apache.wayang.core.api.Configuration; |
| 26 | +import org.apache.wayang.core.api.WayangContext; |
| 27 | +import org.apache.wayang.java.Java; |
| 28 | +import org.apache.wayang.spark.Spark; |
| 29 | + |
| 30 | +import java.util.Collection; |
| 31 | + |
| 32 | +/** |
| 33 | + * TPC-H Query 1 implementation using JavaPlanBuilder API. |
| 34 | + * This is the modern, fluent API version. Compare with {@link TPCHQ1WithJavaNative} |
| 35 | + * to see the differences between the native operator API and the JavaPlanBuilder API. |
| 36 | + */ |
| 37 | +public class TPCHQ1WithPlanBuilder { |
| 38 | + |
| 39 | + /** |
| 40 | + * Executes TPC-H Query 1, which is as follows: |
| 41 | + * <pre> |
| 42 | + * select |
| 43 | + * l_returnflag, |
| 44 | + * l_linestatus, |
| 45 | + * sum(l_quantity) as sum_qty, |
| 46 | + * sum(l_extendedprice) as sum_base_price, |
| 47 | + * sum(l_extendedprice*(1-l_discount)) as sum_disc_price, |
| 48 | + * sum(l_extendedprice*(1-l_discount)*(1+l_tax)) as sum_charge, |
| 49 | + * avg(l_quantity) as avg_qty, |
| 50 | + * avg(l_extendedprice) as avg_price, |
| 51 | + * avg(l_discount) as avg_disc, |
| 52 | + * count(*) as count_order |
| 53 | + * from |
| 54 | + * lineitem |
| 55 | + * where |
| 56 | + * l_shipdate <= date '1998-12-01' - interval '[DELTA]' day (3) |
| 57 | + * group by |
| 58 | + * l_returnflag, |
| 59 | + * l_linestatus |
| 60 | + * order by |
| 61 | + * l_returnflag, |
| 62 | + * l_linestatus; |
| 63 | + * </pre> |
| 64 | + * |
| 65 | + * @param wayangContext the Wayang context |
| 66 | + * @param lineItemUrl URL to the lineitem CSV file |
| 67 | + * @param delta the {@code [DELTA]} parameter |
| 68 | + * @return Collection of query results |
| 69 | + */ |
| 70 | + private static Collection<ReturnTuple> executeQ1(WayangContext wayangContext, String lineItemUrl, final int delta) { |
| 71 | + final int maxShipdate = LineItemTuple.Parser.parseDate("1998-12-01") - delta; |
| 72 | + |
| 73 | + JavaPlanBuilder planBuilder = new JavaPlanBuilder(wayangContext) |
| 74 | + .withJobName("TPC-H Q1") |
| 75 | + .withUdfJarOf(TPCHQ1WithPlanBuilder.class); |
| 76 | + |
| 77 | + return planBuilder |
| 78 | + // Read the lineitem table |
| 79 | + .readTextFile(lineItemUrl).withName("Load lineitem") |
| 80 | + |
| 81 | + // Parse the rows |
| 82 | + .map(line -> new LineItemTuple.Parser().parse(line)) |
| 83 | + .withName("Parse lineitem") |
| 84 | + |
| 85 | + // Filter by shipdate |
| 86 | + .filter(tuple -> tuple.L_SHIPDATE <= maxShipdate) |
| 87 | + .withName("Filter by shipdate") |
| 88 | + |
| 89 | + // Project the queried attributes |
| 90 | + .map(lineItemTuple -> new ReturnTuple( |
| 91 | + lineItemTuple.L_RETURNFLAG, |
| 92 | + lineItemTuple.L_LINESTATUS, |
| 93 | + lineItemTuple.L_QUANTITY, |
| 94 | + lineItemTuple.L_EXTENDEDPRICE, |
| 95 | + lineItemTuple.L_EXTENDEDPRICE * (1 - lineItemTuple.L_DISCOUNT), |
| 96 | + lineItemTuple.L_EXTENDEDPRICE * (1 - lineItemTuple.L_DISCOUNT) * (1 + lineItemTuple.L_TAX), |
| 97 | + lineItemTuple.L_QUANTITY, |
| 98 | + lineItemTuple.L_EXTENDEDPRICE, |
| 99 | + lineItemTuple.L_DISCOUNT, |
| 100 | + 1)) |
| 101 | + .withName("Project attributes") |
| 102 | + |
| 103 | + // Aggregation: group by returnflag and linestatus |
| 104 | + .reduceByKey( |
| 105 | + returnTuple -> new GroupKey(returnTuple.L_RETURNFLAG, returnTuple.L_LINESTATUS), |
| 106 | + (t1, t2) -> { |
| 107 | + t1.SUM_QTY += t2.SUM_QTY; |
| 108 | + t1.SUM_BASE_PRICE += t2.SUM_BASE_PRICE; |
| 109 | + t1.SUM_DISC_PRICE += t2.SUM_DISC_PRICE; |
| 110 | + t1.SUM_CHARGE += t2.SUM_CHARGE; |
| 111 | + t1.AVG_QTY += t2.AVG_QTY; |
| 112 | + t1.AVG_PRICE += t2.AVG_PRICE; |
| 113 | + t1.AVG_DISC += t2.AVG_DISC; |
| 114 | + t1.COUNT_ORDER += t2.COUNT_ORDER; |
| 115 | + return t1; |
| 116 | + }) |
| 117 | + .withName("Aggregate") |
| 118 | + |
| 119 | + // Finalize AVG operations |
| 120 | + .map(t -> { |
| 121 | + t.AVG_QTY /= t.COUNT_ORDER; |
| 122 | + t.AVG_PRICE /= t.COUNT_ORDER; |
| 123 | + t.AVG_DISC /= t.COUNT_ORDER; |
| 124 | + return t; |
| 125 | + }) |
| 126 | + .withName("Finalize aggregation") |
| 127 | + |
| 128 | + // Execute and collect results |
| 129 | + .collect(); |
| 130 | + } |
| 131 | + |
| 132 | + public static void main(String[] args) { |
| 133 | + if (args.length == 0) { |
| 134 | + System.err.print("Usage: <platform1>[,<platform2>]* <query number> <query parameters>*"); |
| 135 | + System.exit(1); |
| 136 | + } |
| 137 | + |
| 138 | + WayangContext wayangContext = new WayangContext(new Configuration()); |
| 139 | + for (String platform : args[0].split(",")) { |
| 140 | + switch (platform) { |
| 141 | + case "java": |
| 142 | + wayangContext.register(Java.basicPlugin()); |
| 143 | + break; |
| 144 | + case "spark": |
| 145 | + wayangContext.register(Spark.basicPlugin()); |
| 146 | + break; |
| 147 | + default: |
| 148 | + System.err.format("Unknown platform: \"%s\"\n", platform); |
| 149 | + System.exit(3); |
| 150 | + return; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + Collection<ReturnTuple> results; |
| 155 | + switch (Integer.parseInt(args[1])) { |
| 156 | + case 1: |
| 157 | + results = executeQ1(wayangContext, args[2], Integer.parseInt(args[3])); |
| 158 | + break; |
| 159 | + default: |
| 160 | + System.err.println("Unsupported query number."); |
| 161 | + System.exit(2); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + // Print results |
| 166 | + results.forEach(System.out::println); |
| 167 | + } |
| 168 | +} |
0 commit comments