您的位置:首页 > 其它

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在数据加载时不做检查,查询时检查

外部分区表需要添加分区才能看到数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: