1 數(shù)據(jù)導(dǎo)入
1.1 向表中裝載數(shù)據(jù)(Load)
1)語(yǔ)法
hive>
load data [local] inpath '數(shù)據(jù)的path'
[overwrite] into table table_name[partition (partcol1=val1,…)];
(1)load data:表示加載數(shù)據(jù)。
(2)local:表示從本地加載數(shù)據(jù)到Hive表;否則從HDFS加載數(shù)據(jù)到Hive表。
(3)inpath:表示加載數(shù)據(jù)的路徑。
(4)overwrite:表示覆蓋表中已有數(shù)據(jù),否則表示追加。
(5)intotable:表示加載到哪張表。
(6)student:表示具體的表。
(7)partition:表示上傳到指定分區(qū)。
2)實(shí)操案例
(0)創(chuàng)建一張表
hive (default)>
create table student(
id int,
name string
)
row format delimited fields terminated by'\t';
(1)加載本地文件到hive
hive (default)> load data local inpath'/opt/module/hive/datas/student.txt' into table student;
(2)加載HDFS文件到hive中
①上傳文件到HDFS
hive (default)> dfs -put /opt/module/hive/datas/student.txt/user/atguigu;
②加載HDFS上數(shù)據(jù),導(dǎo)入完成后去HDFS上查看文件是否還存在
hive (default)>
load data inpath'/user/atguigu/student.txt'
into table student;
(3)加載數(shù)據(jù)覆蓋表中已有的數(shù)據(jù)
①上傳文件到HDFS
hive (default)> dfs -put/opt/module/hive/datas/student.txt /user/atguigu;
②加載數(shù)據(jù)覆蓋表中已有的數(shù)據(jù)
hive (default)>
load data inpath'/user/atguigu/student.txt'
overwrite into table student;
1.2 通過(guò)查詢(xún)語(yǔ)句向表中插入數(shù)據(jù)(Insert)
1)創(chuàng)建一張表
hive (default)>
create table student3(
id int,
name string
)
row format delimited fields terminated by'\t';
2)基本模式插入數(shù)據(jù)
hive (default)> insert into table student3 values(1,'wangwu'),(2,'zhaoliu');
3)根據(jù)查詢(xún)結(jié)果插入數(shù)據(jù)
hive (default)> insert overwrite tablestudent3
select
id,
name
from student
where id < 1006;
insert into:以追加數(shù)據(jù)的方式插入到表或分區(qū),原有數(shù)據(jù)不會(huì)刪除。
insert overwrite:會(huì)覆蓋表中已存在的數(shù)據(jù)。
注:insert不支持插入部分字段,并且后邊跟select語(yǔ)句時(shí),select之前不能加as,加了as會(huì)報(bào)錯(cuò),一定要跟下面的as select區(qū)分開(kāi)。
1.3 查詢(xún)語(yǔ)句中創(chuàng)建表并加載數(shù)據(jù)(As Select)
根據(jù)查詢(xún)結(jié)果創(chuàng)建表(查詢(xún)的結(jié)果會(huì)添加到新創(chuàng)建的表中)。
hive (default)>
create table if not exists student4
as select id, name from student;
1.4 創(chuàng)建表時(shí)通過(guò)Location指定加載數(shù)據(jù)路徑
1)上傳數(shù)據(jù)到HDFS上
[lily@hadoop102 datas]$ hadoop fs-mkdir -p /student5;
[lily@hadoop102 datas]$ hadoop fs -putstudent.txt /student5
2)創(chuàng)建表,并指定在HDFS上的位置
hive (default)>
create external table if notexists student5(
id int,
name string
)
row format delimited fields terminated by'\t'
location '/student5';
3)查詢(xún)數(shù)據(jù)
hive (default)> select * from student5;
1.5 Import數(shù)據(jù)到指定Hive表中
注:先用export導(dǎo)出后,再將數(shù)據(jù)導(dǎo)入。并且因?yàn)閑xport導(dǎo)出的數(shù)據(jù)里面包含了元數(shù)據(jù),因此import要導(dǎo)入的表不可以存在,否則報(bào)錯(cuò)。
hive (default)>
import table student2 from '/user/hive/warehouse/export/student';
2 數(shù)據(jù)導(dǎo)出
2.1 Insert導(dǎo)出
1)將查詢(xún)的結(jié)果導(dǎo)出到本地
hive (default)>
insert overwrite local directory'/opt/module/hive/datas/export/student'
select * from student;
2)將查詢(xún)的結(jié)果格式化導(dǎo)出到本地
hive(default)>
insert overwrite local directory'/opt/module/hive/datas/export/student'
row format delimited fields terminated by'\t'
select * from student;
3)將查詢(xún)的結(jié)果導(dǎo)出到HDFS上(沒(méi)有l(wèi)ocal)
hive (default)> insert overwrite directory'/user/atguigu/student2'
row format delimited fields terminated by'\t'
select * from student;
注:insert導(dǎo)出,導(dǎo)出的目錄不用自己提前創(chuàng)建,Hive會(huì)幫我們自動(dòng)創(chuàng)建,但是由于是overwrite,所以導(dǎo)出路徑一定要寫(xiě)具體,否則很可能會(huì)誤刪數(shù)據(jù)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-635320.html
2.2 Export導(dǎo)出到HDFS
hive (default)>
export table default.student to
'/user/hive/warehouse/export/student';
注:Export和Import主要用于兩個(gè)Hadoop平臺(tái)集群之間Hive表遷移,不能直接導(dǎo)出到本地。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-635320.html
到了這里,關(guān)于hive的數(shù)據(jù)導(dǎo)入的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!