前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用Sqoop实现Hive的数据与MySQL数据的互导

利用Sqoop实现Hive的数据与MySQL数据的互导

作者头像
CoderJed
发布2018-09-13 10:36:39
2.7K0
发布2018-09-13 10:36:39
举报
文章被收录于专栏:Jed的技术阶梯Jed的技术阶梯

1. 配置概览

代码语言:javascript
复制
Hive arguments:
   --create-hive-table                         Fail if the target hive table exists
   --hive-database <database-name>             Sets the database name to use when importing to hive
   --hive-delims-replacement <arg>             Replace Hive record \0x01 and row delimiters (\n\r) from imported string fields with user-defined string
   --hive-drop-import-delims                   Drop Hive record \0x01 and row delimiters (\n\r) from imported string fields
   --hive-home <dir>                           Override $HIVE_HOME Import tables into Hive (Uses Hive's default delimiters if none are set.)
   --hive-import                               
   --hive-overwrite                            Overwrite existing data in the Hive table
   --hive-partition-key <partition-key>        Sets the partition key to use when importing to hive
   --hive-partition-value <partition-value>    Sets the partition value to use when importing to hive
   --hive-table <table-name>                   Sets the table name to use when importing to hive
   --map-column-hive <arg>                     Override mapping for specific column to hive types.

2. 把MySQL表中数据导入到hive表中

代码语言:javascript
复制
drop table if exists hive_users;

create table hive_users (id string,name string,age int)
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY '\t';

[root@repo bin]# ./sqoop import \
--connect jdbc:mysql://192.168.9.100:3306/test \
--username root \
--password 123456 \
--table users \
--fields-terminated-by '\t' \
--num-mappers 1 \
--hive-import \
--hive-database default \
--hive-table hive_users \
--delete-target-dir 

hive> select * from hive_users;
OK
1   Jed     15
2   Tom     16
3   Tony    17
4   Bob     18
5   Harry   19
6   Jack    20

3. 把hive表中数据导入到MySQL表中

代码语言:javascript
复制
mysql> create table users_from_hive (id int,name varchar(10),age int,primary key (`id`));

[root@repo bin]# ./sqoop export \
--connect jdbc:mysql://192.168.9.100:3306/test \
--username root \
--password 123456 \
--table users_from_hive \
--input-fields-terminated-by '\t' \
--export-dir /hive_single_user/warehouse/hive_users \
--num-mappers 1

mysql> select * from users_from_hive;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | Jed   |   15 |
|  2 | Tom   |   16 |
|  3 | Tony  |   17 |
|  4 | Bob   |   18 |
|  5 | Harry |   19 |
|  6 | Jack  |   20 |
+----+-------+------+

注意: 在sqoop-1.4.6以前,从MySQL中导出数据到hive表中,不能指定文件格式为parquet,只能先导入到HDFS,在从HDFS上load parquet file

4. 把sqoop命令写到文件中,sqoop执行时使用这个文件来执行命令

代码语言:javascript
复制
[root@repo myshell]# vim sqoop-options-test
--connect jdbc:mysql://192.168.9.100:3306/test \
--username root \
--password 123456 \ 
--target-dir /user/root/SQOOP/import/users_options \
--num-mappers 1 

[root@repo bin]# ./sqoop import \
--options-file /root/myshell/sqoop-options-test \
--table users_from_hive

[root@repo bin]# hdfs dfs -cat /user/root/SQOOP/import/users_options/*
1,Jed,15
2,Tom,16
3,Tony,17
4,Bob,18
5,Harry,19
6,Jack,20

注意: (1) 选项在文件中与手工设定可以同时使用 (2) 可以在选项文件中写注释,# ...

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 配置概览
  • 2. 把MySQL表中数据导入到hive表中
  • 3. 把hive表中数据导入到MySQL表中
  • 4. 把sqoop命令写到文件中,sqoop执行时使用这个文件来执行命令
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档