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在資料載入時不做檢查,查詢時檢查。
外部分割槽表需要新增分割槽才能看到資料。