Hive的数据加载与导出
普通表的加載
1.load方式
load data [local] inpath [源文件路徑] into table 目標(biāo)表名;
?
從HDFS上加載數(shù)據(jù),本質(zhì)上是移動(dòng)文件所在的路徑
load data inpath '/user/student.txt' into table student;
?
從本地加載數(shù)據(jù),本質(zhì)上是復(fù)制本地的文件到HDFS上
load data local inpath '/user/student.txt' into table student;
?
?
2.insert方式
插入一條數(shù)據(jù)(單重,先生成臨時(shí)表再拷貝到student中,效率低)
insert into table student values(00011,黃海霞,18);
?
插入多條數(shù)據(jù)(單重,查詢(xún)結(jié)果導(dǎo)入student中,效率低)
insert into table student select * from stu where age >=18;
?
多重插入(只掃描一次源表,將結(jié)果插入到多個(gè)新表中,效率高,常用)
from stu insert into table student01 select * where age >=18 insert into table student02 select * where age <18;
?
?
分區(qū)表的加載(常用)
- 一般不使用load方式,因?yàn)檫@種方式不會(huì)自動(dòng)檢驗(yàn)原表與目標(biāo)表的列是否對(duì)應(yīng),數(shù)據(jù)易出錯(cuò);
- 一般也不建議使用insert方式單條插入;
靜態(tài)分區(qū),即分區(qū)個(gè)數(shù)較少,可列舉:
1)手動(dòng)添加分區(qū)
ALTER TABLE student ADD if not exists PARTITION(city='beijing');
ALTER TABLE student ADD if not exists PARTITION(city='shanghai');
?
2)添加數(shù)據(jù)
from stu insert into table student01 partition(city='beijing') select id, sname, age where city='beijing' insert into table student02 partition(city='shanghai') select id, sname, age where city='shanghai';
?
動(dòng)態(tài)分區(qū),即分區(qū)個(gè)數(shù)較多,比如日期、年齡:
1)修改分區(qū)模式為非嚴(yán)格模式
set hive.exec.dynamic.partition.mode = nonstrict;#hive2版本,默認(rèn)是strict
set hive.exec.dynamic.partition = true;#hive1版本,先開(kāi)啟動(dòng)態(tài)分區(qū)
set hive.exec.dynamic.partition.mode = nonstrict;#hive1版本,再開(kāi)啟非嚴(yán)格模式
?
2)添加數(shù)據(jù)
- 若city是指定的自動(dòng)分區(qū)字段,則select中必須包含city,且在最后一個(gè);
- 若分區(qū)字段是兩個(gè),city和age,則partition(city,age),city為主,age為次,select中city,age在最后且順序不能變;
from stu
insert into table student01 partition(city) select id, sname, age, city
insert into table student02 partition(city) select id, sname, age, city ;
?
?
分桶表的加載
- 不允許使用load方式;
1)先建一個(gè)分桶表student;
CREATE TABLE if not exists student(id int ,sname string ,age int, city string) clustered BY (age) sorted BY (city) INTO 3 buckets ROW FORMAT delimited FIELDS terminated BY ','
?
2)添加數(shù)據(jù)
insert into table student select * from stu;
- 添加數(shù)據(jù)的時(shí)候,reducetask 實(shí)際運(yùn)行個(gè)數(shù),默認(rèn)值是1,但是因?yàn)榉?個(gè)桶,因此此時(shí)reducetask 實(shí)際運(yùn)行個(gè)數(shù)=3;
- reducetask 最大運(yùn)行個(gè)數(shù)是1009;
- 每一個(gè)reduce的吞吐量是256M;
?
=================================================================
數(shù)據(jù)的導(dǎo)出
單重
insert overwrite directory '/user/stu01.txt' select * from student where age >=18; #到HDFS
insert overwrite local directory '/user/stu01.txt' select * from student where age >=18; #到本地
?
多重
from student insert overwrite local directory '/user/stu01.txt' select * where age >=18 insert overwrite local directory '/user/stu01.txt' select * where age >=18;
到HDFS的話,去掉local即可
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Hive的数据加载与导出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python模块time_Python模
- 下一篇: 如何并行运行程序