一直对hive 的分区有点模糊,看到一篇具体一点的文章,转载过来。
http://f.dataguru.cn/thread-526681-1-1.html
1 导入作业数据集total.csv到Hive中,用日期做为分区表的分区ID。要求有描述型文字,Hive脚本和截图。
回购表数据:
tradedate:交易日期
tradetime:交易时间
securityid:股票ID
bidpx1:买一
bidsize1:买一交易量
offerpx1:卖一
bidsize1:卖一交易量
数据存储格式:
tradedate,tradetime,securityid,bidpx1,bidsize1,offerpx1,offersize1
数据准备:
localhost:homework11 apple$ scptotal.csv hadoop@hadoop0:/home/hadoop/Downloads/Data/
[hadoop@hadoop0Data]$ head total.csv
#删除表
drop table if exists t_reverse_repurchase_tmp;
#创建临时表t_reverse_repurchase_tmp,分隔符','。
CREATE TABLE t_reverse_repurchase_tmp(tradedate STRING,tradetime STRING, securityid STRING, bidpx1 DOUBLE, bidsize1BIGINT,offerpx1 DOUBLE,offersize1 BIGINT) ROW FORMAT DELIMITED FIELDSTERMINATED BY ',';
#导入数据到t_reverse_repurchase_tmp。
LOAD DATA LOCAL INPATH'/home/hadoop/Downloads/Data/total.csv' OVERWRITE INTO TABLEt_reverse_repurchase_tmp;
#查看导入的数据:
hive> select * from t_reverse_repurchase_tmp limit 10;
#查看表描述
hive> DESCRIBE t_reverse_repurchase_tmp;
staticpartition常适用于使用处理时间作为partition key的例子。
但是这里需要向分区表中插入大量数据,并且插入前不清楚数据归属的partition,此时,我们需要dynamic partition。使用动态分区需要设置hive.exec.dynamic.partition参数值为true。
hive>use mydb
#删除表
hive>drop table if exists t_reverse_repurchase;
#建表
hive>CREATE TABLE t_reverse_repurchase (tradetime STRING, securityidSTRING, bidpx1 DOUBLE, bidsize1 BIGINT,offerpx1 DOUBLE,offersize1 BIGINT)PARTITIONED BY (tradedate STRING);
#设置参数开启dynamic partition
hive> sethive.exec.dynamic.partition.mode=nonstrict;
hive> sethive.exec.dynamic.partition=true;
#将数据从t_reverse_repurchase_tmp加载到t_reverse_repurchase中
INSERT OVERWRITE TABLEt_reverse_repurchase PARTITION (tradedate) SELECT a.tradetime astradetime,a.securityid as securityid, a.bidpx1 as bidpx1, a.bidsize1 asbidsize1,a.offerpx1 as fferpx1,a.offersize1 as offersize1, a.tradedate astradedate FROM t_reverse_repurchase_tmp a;
#查看导入的数据:
hive> select * from t_reverse_repurchase wheretradedate=='20130723' and securityid='204001' limit 10;
#查看表的描述信息
hive> DESCRIBE t_reverse_repurchase;
#查看表的分区存储情况
hive>show partitions t_reverse_repurchase;
参考连接:
Hive Getting Started
Hive静态动态分区
用RHive从历史数据中提取逆回购信息
Hive数据几种导出方式
R数据类型变换