导入:
- 本地文件导入到Hive表;
- Hive表导入到Hive表;
- HDFS文件导入到Hive表;
- 创建表的过程中从其他表导入;
- 通过sqoop将mysql库导入到Hive表
导出:
- Hive表导出到本地文件系统;
- Hive表导出到HDFS;
- 通过sqoop将Hive表导出到mysql库;
- .Hadoop命令导出到本地
- Export导出到HDFS上
Hive数据导出的几种方式:
1。Insert导出
1).将查询的结果导出到本地
hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
select * from student;
指定导出格式(此种方式导出的数据不一定可用,可能还是textfile格式):
insert overwrite directory '/aaa/bb' stored as xxx select ...
2).将查询的结果格式化导出到本地
hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' collection items terminated by '\n' select * from student;
3).将查询的结果导出到HDFS上(没有local)
hive (default)> insert overwrite directory '/user/atguigu/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
2. Hadoop命令导出到本地
hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0
/opt/module/datas/export/student3.txt;
3. Hive Shell 命令导出
基本语法:(hive -f/-e 执行语句或者脚本 > file)
[root@sparkproject1 hive]$ bin/hive -e 'select * from default.student;' >
/opt/module/datas/export/student4.txt;
4. Export导出到HDFS上
(defahiveult)> export table default.student to
'/user/hive/warehouse/export/student';
export和import主要用于两个Hadoop平台集群之间Hive表迁移。
5. Sqoop导出
实例:
1.hive shell
1) 导出到本地目录
1.登录hive shell
hive (default)>
2.use 数据库;
3.指定本地目录并导出
INSERT OVERWRITE LOCAL DIRECTORY '/user/local/hive/table-daochu' ROW FORMAT DELIMITED FIELDS TERMINATED by ',' select * from o.aim_base_employees;
查看数据结果:
-rw-r--r-- 1 root root 11132487 Feb 10 17:03 000000_0 //产生的结果存放在文件名为:000000_0。
[root@pf-bigdata1 table-daochu]#
可以使用editplus打开查看
2)导出到HDFS
导入到HDFS和导入本地文件类似,去掉HQL语句的LOCAL就可以了
INSERT OVERWRITE DIRECTORY '/user/local/hive/table-daochu' ROW FORMAT DELIMITED FIELDS TERMINATED by ',' select * from o.aim_base_employees;
查看hfds输出文件:
[hadoop@hadoopcluster78 bin]$ ./hadoop fs -cat /home/hadoop/output/000000_0
2.采用hive的-e和-f参数来导出数据。
参数为: -e 的使用方式,后面接SQL语句。>>后面为输出文件路径
hive -e "select * from o.aim_base_employees" >> /user/local/hive/table-daochu/test.txt
参数为: -f 的使用方式,后面接存放sql语句的文件。>>后面为输出文件路径
testsql0.sql文件内容:
select * from o.aim_base_employees limit 1000;
执行:
hi