Hive数据加载(内部表,外部表,分区表)
2015-07-17 21:49
537 查看
内表数据加载
创建表时加载
create table newtable as select col1,col2 from oldtable
hive> create table testNew as select name,addr from testtable; hive> select * from testNew; OK liguodong cd aobama lsj liguodong cd aobama lsj
创建表时指定数据位置
create table tablename location '/**/**'
create table if not exists testNew2( name string comment 'name value', addr string comment 'addr value' ) row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile location '/liguodong/hivedata' ;
本地数据加载
load data local inpath 'localpath' [overwrite] into table tablename
追加 hive> load data local inpath '/liguodong/hivedata/datatest' into table testNew2; 覆盖 load data local inpath '/liguodong/hivedata/datatest' overwrite into table testNew2;
加载hdfs数据
load data inpath 'hdfspath' [overwrite] into table tablename
注:这个操作是移动数据,而不是复制数据。
load data inpath '/liguodong/datatest' into table testNew2; load data inpath '/liguodong/datatest' overwrite into table testNew2;
使用Hadoop命令拷贝数据到指定位置(hive的shell中执行和Linux的shell执行)
由查询语句加载数据
insert [overwrite | into] table tablename select col1,col2 from table where ... from table insert [overwrite | into ]table tablename select col1,col2 where ...
注意:字段对应不同于一些关系型数据库。
外表数据加载
创建表时指定数据位置
create external table tablename() location ''
create external table if not exists testExtNew( name string, addr string ) row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile location '/liguodong/exttable/'; select * from testExtNew;
查询插入,同内表
使用Hadoop命令拷贝数据到指定位置(hive的shell中执行和Linux的Shell执行),同内部表
Hive分区表数据加载
内部分区表和外部分区表数据加载内部分区表数据加载方式类似于内表
外部分区表数据加载方式类似于外表
注意:
数据存放的路径层次要和表的分区一致;
如果分区表没有新增分区,即使目标路径下己经有数据了,但依然查不到数据。
不同之处
加载数据指定目标表的同时,需要指定分区。
本地数据加载
Load data local inpath 'localpath' [overwrite] into table tablename partition(pn='')
内部表
create table if not exists testPar( name string, addr string ) partitioned by (dt string) row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile ; load data local inpath '/liguodong/dataext' into table testPar partition(dt='20150717'); 显示分区 show partitions testPar;
外部表
create external table if not exists testExtPar( name string, addr string ) partitioned by (dt string) row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile location '/liguodong/exttable/' ; select * from testExtPar; 没有结果,不满足分区表相应的目录格式。 dfs -copyFromLocal /liguodong/dataext /liguodong/exttable/dt=20150717 select * from testExtPar; 满足分区表相应的目录格式,仍然没有结果,因为,查不到分区相关信息。 //修改表,添加相应的分区 alter table testExtPar add partition(dt='20150717'); show partitions testExtPar; select * from testExtPar;
加载hdfs数据
Load data inpath 'hdfspath' [overwrite] into table tablename partition(pn='')
由查询语句加载数据
insert [overwrite] into table tablename partition(pn='') select col1,col2 from table where…
Hive数据加载注意问题
分隔符问题:分隔符默认只有单个字符。如果有多个字符,默认取第一个字符作为分隔符。数据类型对应问题:
Load数据数据,字段类型不能互相转化时,查询结果返回NULL。而实际的数据仍然存在。
Select查询插入,字段类型不能互相转化时,插入数据为NULL。而实际的数据也为NULL。
其他:
Select查询插入数据,字段值顺序要与表中字段顺序一致,名称可不一致。
Hive在数据加载时不做检查,查询时检查。
外部分区表需要添加分区才能看到数据。
相关文章推荐
- 支撑统计学的七大支柱!
- 喵哈哈的日常选数问题(数位DP)
- 关于Service中bindService注意的几个问题
- 1046. Shortest Distance (20) 坑我
- 关于Service中bindService注意的几个问题
- Tortoise SVN 提示 clean up 然而 clean up 没卵用的解决方法
- 线段树区间修改 懒惰标记 维护和、最大值、最小值
- LINUX之文件解压与压缩汇总
- 关于解决乱码问题的一点探索之一(涉及utf-8和GBK)
- OpenCV 数字验证码识别
- 理解dynamicsAX表里的各种key
- [LeetCode][Java] Set Matrix Zeroes
- 找规律 hdu4861 Couple doubi
- iOS动画中的物理知识应用之愤怒的小鸟-重力加速度
- poj 1840 Eqs(Hash)
- 一道树形dp的题目:个人赛第二场G题
- AndroidStudio插件GsonFormat快速实现JavaBean
- R12.2 克隆完成之后weblogic AdminSErver无法启动
- 设计模式之状态模式
- [POJ3982]序列[字符串][大整数]