blob: dd6b416d4e67dfbd70479068914e1ea3caa3934c [file] [log] [blame]
buyingyic73348c2012-11-02 00:31:31 +00001-- create tables and load data
2create external table customer (C_CUSTKEY INT, C_NAME STRING, C_ADDRESS STRING, C_NATIONKEY INT, C_PHONE STRING, C_ACCTBAL DOUBLE, C_MKTSEGMENT STRING, C_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/customer';
3Create external table lineitem (L_ORDERKEY INT, L_PARTKEY INT, L_SUPPKEY INT, L_LINENUMBER INT, L_QUANTITY DOUBLE, L_EXTENDEDPRICE DOUBLE, L_DISCOUNT DOUBLE, L_TAX DOUBLE, L_RETURNFLAG STRING, L_LINESTATUS STRING, L_SHIPDATE STRING, L_COMMITDATE STRING, L_RECEIPTDATE STRING, L_SHIPINSTRUCT STRING, L_SHIPMODE STRING, L_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/lineitem';
4create external table orders (O_ORDERKEY INT, O_CUSTKEY INT, O_ORDERSTATUS STRING, O_TOTALPRICE DOUBLE, O_ORDERDATE STRING, O_ORDERPRIORITY STRING, O_CLERK STRING, O_SHIPPRIORITY INT, O_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/orders';
5create external table supplier (S_SUPPKEY INT, S_NAME STRING, S_ADDRESS STRING, S_NATIONKEY INT, S_PHONE STRING, S_ACCTBAL DOUBLE, S_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/supplier';
6create external table nation (N_NATIONKEY INT, N_NAME STRING, N_REGIONKEY INT, N_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/nation';
7
8-- create the target table
9create table q7_volume_shipping (supp_nation string, cust_nation string, l_year int, revenue double);
10create table q7_volume_shipping_tmp(supp_nation string, cust_nation string, s_nationkey int, c_nationkey int);
11
12set mapred.min.split.size=536870912;
13set hive.exec.reducers.bytes.per.reducer=1225000000;
14
15-- the query
16insert overwrite table q7_volume_shipping_tmp
17select
18 *
19from
20 (
21 select
22 n1.n_name as supp_nation, n2.n_name as cust_nation, n1.n_nationkey as s_nationkey,
23 n2.n_nationkey as c_nationkey
24from
25 nation n1 join nation n2
26 on
27 n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY'
28 UNION ALL
29select
30 n1.n_name as supp_nation, n2.n_name as cust_nation, n1.n_nationkey as s_nationkey,
31 n2.n_nationkey as c_nationkey
32from
33 nation n1 join nation n2
34 on
35 n2.n_name = 'FRANCE' and n1.n_name = 'GERMANY'
36) a;
37
38insert overwrite table q7_volume_shipping
39select
40 supp_nation, cust_nation, l_year, sum(volume) as revenue
41from
42 (
43 select
44 supp_nation, cust_nation, year(l_shipdate) as l_year,
45 l_extendedprice * (1 - l_discount) as volume
46 from
47 q7_volume_shipping_tmp t join
48 (select l_shipdate, l_extendedprice, l_discount, c_nationkey, s_nationkey
49 from supplier s join
50 (select l_shipdate, l_extendedprice, l_discount, l_suppkey, c_nationkey
51 from customer c join
52 (select l_shipdate, l_extendedprice, l_discount, l_suppkey, o_custkey
53 from orders o join lineitem l
54 on
55 o.o_orderkey = l.l_orderkey and l.l_shipdate >= '1995-01-01'
56 and l.l_shipdate <= '1996-12-31'
57 ) l1 on c.c_custkey = l1.o_custkey
58 ) l2 on s.s_suppkey = l2.l_suppkey
59 ) l3 on l3.c_nationkey = t.c_nationkey and l3.s_nationkey = t.s_nationkey
60 ) shipping
61group by supp_nation, cust_nation, l_year
62order by supp_nation, cust_nation, l_year;
63
64DROP TABLE customer;
65DROP TABLE orders;
66DROP TABLE lineitem;
67DROP TABLE supplier;
68DROP TABLE nation;
69DROP TABLE q7_volume_shipping;
70DROP TABLE q7_volume_shipping_tmp;