Hive creates a partition table by date | dynamically inserts data into the date Partition

Source: Internet
Author: User

Hive creates a partition table based on the current day (""). hql is as follows:

CREATE EXTERNAL TABLE IF NOT EXISTS product_sell(category_id BIGINT,province_id BIGINT,product_id BIGINT,price DOUBLE,sell_num BIGINT)PARTITIONED BY (ds string)ROW FORMAT DELIMITEDFIELDS TERMINATED BY '\t'LINES TERMINATED BY '\n'STORED AS TEXTFILE;

Insert data based on the date as the partition. The shell script is as follows:

#!/bin/bashsource /etc/profile;today=$(date +%Y-%m-%d)/usr/local/cloud/hive/bin/hive<<EOF INSERT OVERWRITE TABLE product_sell PARTITION (ds='$today') select a.category_id, b.good_receiver_province_id as province_id, a.id as product_id, (b.sell_amount/b.sell_num) as price, b.sell_num from product a join (select si.product_id, s.good_receiver_province_id, sum(si.order_item_amount) sell_amount, sum(si.order_item_num) sell_num from so_item si join so s on (si.order_id=s.id) where si.is_gift=0 and datediff(date_sub(from_unixtime(unix_timestamp()),1), si.ds) between 0 and 6 group by s.good_receiver_province_id, si.product_id) b on (a.id=b.product_id);EOF

This is difficult because hive does not know how to create a date partition by calling a shell date variable. Mark it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.