Hive数据加载(内部表,外部表,分区表)

###内表数据加载

####创建表时加载

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

外部分区表需要添加分区才能看到数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃果冻不吐果冻皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值