1. Insert 導(dǎo)出
1)將查詢的結(jié)果導(dǎo)出到本地
hive (default)> insert overwrite local directory '/opt/module/hive/data/export/student' select * from student5;
Automatically selecting local only mode for query
Query ID = atguigu_20211217153118_31119102-f06a-4313-a1c7-c99c89d5f549
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Job running in-process (local Hadoop)
2021-12-17 15:31:21,767 Stage-1 map = 100%, reduce = 0%
Ended Job = job_local2085445374_0004
Moving data to local directory /opt/module/hive/data/export/student
MapReduce Jobs Launched:
Stage-Stage-1: HDFS Read: 767 HDFS Write: 41412249 SUCCESS
Total MapReduce CPU Time Spent: 0 msec
OK
student5.id student5.name
Time taken: 2.922 seconds
2)將查詢的結(jié)果格式化導(dǎo)出到本地(加上一個(gè)以“,”隔開(kāi)數(shù)據(jù)的格式)
hive(default)>insert overwrite local directory '/opt/module/hive/data/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
select * from student;
3)將查詢的結(jié)果導(dǎo)出到 HDFS 上(沒(méi)有 local)
hive (default)> insert overwrite directory '/user/zzdq/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ''
select * from student;
2. Hadoop 命令導(dǎo)出到本地
hive (default)> dfs -get /user/hive/warehouse/student/student.txt /opt/module/data/export/student3.txt;
3. Hive Shell 命令導(dǎo)出
基本語(yǔ)法:(hive -f/-e 執(zhí)行語(yǔ)句或者腳本> file)
[zzdq@hadoop102 hive]$ bin/hive -e 'select * from default.student;' opt/module/hive/data/export/student4.txt;
4. Export 導(dǎo)出到 HDFS 上
hive (default)> export table default.student5 to '/student/export/student';
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.ExportTask. Cannot copy hdfs://hadoop100:8020/student/export to its subdirectory hdfs://hadoop100:8020/student/export/student/data/export
導(dǎo)出的數(shù)據(jù)中有兩個(gè)數(shù)據(jù)源,其中除了主信息之外,還包括記錄主數(shù)據(jù)信息的元數(shù)據(jù)
export 和 import 主要用于兩個(gè) Hadoop 平臺(tái)集群之間 Hive 表遷移。
我們嘗試使用import導(dǎo)入上面所產(chǎn)生的信息,但是導(dǎo)入已存在的表發(fā)現(xiàn)報(bào)錯(cuò)了
hive (default)> import table student5 from '/student/export/student';
FAILED: SemanticException [Error 10119]: Table exists and contains data files
我們創(chuàng)建一個(gè)新表來(lái)進(jìn)行導(dǎo)入
hive (default)> import table student6 from '/student/export/student';
Copying data from hdfs://hadoop100:8020/student/export/student/data
Copying file: hdfs://hadoop100:8020/student/export/student/data/student.txt
Loading data to table default.student6
OK
Time taken: 0.984 seconds
我們查詢數(shù)據(jù):
hive (default)> select * from student6;
OK
student6.id student6.name
1001 zzz
1002 ddd
1111 ccc
Time taken: 0.351 seconds, Fetched: 3 row(s)
結(jié)論:導(dǎo)入的表需要是一張沒(méi)收數(shù)據(jù)的表,也就是說(shuō)該表要么不存在,要么就是一張空表。
5. Sqoop 導(dǎo)出
后續(xù)課程專門講。主要作用就是將數(shù)據(jù)導(dǎo)入mysql文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-813768.html
6. 清除表中數(shù)據(jù)(Truncate)
注意:Truncate 只能刪除管理表,不能刪除外部表中數(shù)據(jù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-813768.html
hive (default)> truncate table student6; OK Time taken: 0.943 seconds
到了這里,關(guān)于Hive基礎(chǔ)知識(shí)(十一):Hive的數(shù)據(jù)導(dǎo)出方法示例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!