buyingyi | c73348c | 2012-11-02 00:31:31 +0000 | [diff] [blame] | 1 | -- create tables and load data |
| 2 | create 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'; |
| 3 | create 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'; |
| 4 | create 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'; |
| 5 | create external table partsupp (PS_PARTKEY INT, PS_SUPPKEY INT, PS_AVAILQTY INT, PS_SUPPLYCOST DOUBLE, PS_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION'/tpch/100/partsupp'; |
| 6 | create external table part (P_PARTKEY INT, P_NAME STRING, P_MFGR STRING, P_BRAND STRING, P_TYPE STRING, P_SIZE INT, P_CONTAINER STRING, P_RETAILPRICE DOUBLE, P_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/part'; |
| 7 | |
| 8 | -- create the target table |
| 9 | create table q20_tmp1(p_partkey int); |
| 10 | create table q20_tmp2(l_partkey int, l_suppkey int, sum_quantity double); |
| 11 | create table q20_tmp3(ps_suppkey int, ps_availqty int, sum_quantity double); |
| 12 | create table q20_tmp4(ps_suppkey int); |
| 13 | create table q20_potential_part_promotion(s_name string, s_address string); |
| 14 | |
| 15 | set mapred.min.split.size=536870912; |
| 16 | |
| 17 | -- the query |
| 18 | insert overwrite table q20_tmp1 |
| 19 | select distinct p_partkey |
| 20 | from |
| 21 | part |
| 22 | where |
| 23 | p_name like 'forest%'; |
| 24 | |
| 25 | insert overwrite table q20_tmp2 |
| 26 | select |
| 27 | l_partkey, l_suppkey, 0.5 * sum(l_quantity) |
| 28 | from |
| 29 | lineitem |
| 30 | where |
| 31 | l_shipdate >= '1994-01-01' |
| 32 | and l_shipdate < '1995-01-01' |
| 33 | group by l_partkey, l_suppkey; |
| 34 | |
| 35 | insert overwrite table q20_tmp3 |
| 36 | select |
| 37 | ps_suppkey, ps_availqty, sum_quantity |
| 38 | from |
| 39 | partsupp ps join q20_tmp1 t1 |
| 40 | on |
| 41 | ps.ps_partkey = t1.p_partkey |
| 42 | join q20_tmp2 t2 |
| 43 | on |
| 44 | ps.ps_partkey = t2.l_partkey and ps.ps_suppkey = t2.l_suppkey; |
| 45 | |
| 46 | insert overwrite table q20_tmp4 |
| 47 | select |
| 48 | ps_suppkey |
| 49 | from |
| 50 | q20_tmp3 |
| 51 | where |
| 52 | ps_availqty > sum_quantity |
| 53 | group by ps_suppkey; |
| 54 | |
| 55 | insert overwrite table q20_potential_part_promotion |
| 56 | select |
| 57 | s_name, s_address |
| 58 | from |
| 59 | supplier s join nation n |
| 60 | on |
| 61 | s.s_nationkey = n.n_nationkey |
| 62 | and n.n_name = 'CANADA' |
| 63 | join q20_tmp4 t4 |
| 64 | on |
| 65 | s.s_suppkey = t4.ps_suppkey |
| 66 | order by s_name; |
| 67 | |
| 68 | DROP TABLE partsupp; |
| 69 | DROP TABLE lineitem; |
| 70 | DROP TABLE supplier; |
| 71 | DROP TABLE nation; |
| 72 | DROP TABLE q20_tmp1; |
| 73 | DROP TABLE q20_tmp2; |
| 74 | DROP TABLE q20_tmp3; |
| 75 | DROP TABLE q20_tmp4; |
| 76 | DROP TABLE q20_potential_part_promotion; |