Sqoop教程(二)使用Sqoop在MySQL与Hive之间进行数据导入导出操作

Sqoop主要作用是提供方便的Hive表和关系型数据库表的数据的导入导出操作。
运行Sqoop需要启动Hadoop、Hive服务
在hp1的MySQL中新建数据库test_sqoop,并建表student:
表结构:

Sqoop教程(二)使用Sqoop在MySQL与Hive之间进行数据导入导出操作
表数据:
Sqoop教程(二)使用Sqoop在MySQL与Hive之间进行数据导入导出操作

; 导入:

根据MySQL中student表结构,直接在Hive中建立一张表student_h:

sqoop create-hive-table -connect jdbc:mysql://hp1:3306/test_sqoop -table student -username root -password Hive123+ -hive-table student_h

在beeline中查看表结构:

desc student_h

Sqoop教程(二)使用Sqoop在MySQL与Hive之间进行数据导入导出操作
将MySQL中student表数据,导入到Hive的student_h表中:
sqoop import -connect "jdbc:mysql://hp1:3306/test_sqoop?characterEncoding=utf-8&useSSL=false" -table student -username root -password Hive123+ -hive-import -hive-table my_db.student_x -m 1

其中最后的-m 1,是指开一个任务,以为开启认为会随机在datanode结点上开启,所以要使用hp1指定数据库地址。

如果报异常:

ERROR tool.ImportTool: Import failed: org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://hp1:9000/user/root/student already exists

可以先删除掉hdfs上对应的目录:

hdfs dfs -rm -r /user/root/student

重新执行导入命令即可。

以上import语句,在发现没有表的情况下可以自动建表, my_db.student_x语法可以指定Hive的具体数据库。

导出:

在hive中新建表stu_m,导入数据,请参考:
Hive3详细教程(五)内部表、外部表与导入数据
在Mysql的test_sqoop库中新建表studet_e,表结构与hive中保持一致:

Sqoop教程(二)使用Sqoop在MySQL与Hive之间进行数据导入导出操作

导出语句:

sqoop export -connect  "jdbc:mysql://hp1:3306/test_sqoop?characterEncoding=utf-8&useSSL=false" -username root -password Hive123+ -table student_e -fields-terminated-by ',' --export-dir "/user/hive/warehouse/stu_m"

Original: https://blog.csdn.net/GodBlessYouAndMe/article/details/124691649
Author: Java朱老师
Title: Sqoop教程(二)使用Sqoop在MySQL与Hive之间进行数据导入导出操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/818258/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球