Hadoop Sqoop应用案例

1421625392898_.pic.jpg

环境

  • CentOS 6.8 64位 1核 2GB

  • JDK 1.7.0_75 64 位

  • Hadoop 1.1.2

  • Hive 0.12.0

  • Mysql 5.6.21

  • Sqoop 1.4.5

MySql数据导入到HDFS

  • 查看 mysql 中的数据表
mysql -uhive -phive
mysql> show databases;
mysql> use hive;
mysql> show tables;
mysql> select TBL_ID, CREATE_TIME, DB_ID, OWNER, TBL_NAME,TBL_TYPE from TBLS;
+--------+-------------+-------+--------+------------+---------------+
| TBL_ID | CREATE_TIME | DB_ID | OWNER  | TBL_NAME   | TBL_TYPE      |
+--------+-------------+-------+--------+------------+---------------+
|      1 |  1621827317 |     1 | yohann | test       | MANAGED_TABLE |
|      6 |  1625316873 |     1 | yohann | mysql2hive | MANAGED_TABLE |
+--------+-------------+-------+--------+------------+---------------+
2 rows in set (0.00 sec)
  • sqoop 列出 mysql 中所有数据库
$ sqoop list-databases --connect jdbc:mysql://hadoop:3306/ --username hive --password hive
information_schema
hive
mysql
performance_schema
test
  • 把 hive 数据库 TBLS 表中数据导入到 HDFS
$ sqoop import --connect jdbc:mysql://hadoop:3306/hive --username hive --password hive --table TBLS -m 1
  • 查看导出结果
$ hadoop fs -ls /user/yohann/TBLS
Found 3 items
-rw-r--r--   1 yohann supergroup          0 2021-07-04 16:36 /user/yohann/TBLS/_SUCCESS
drwxr-xr-x   - yohann supergroup          0 2021-07-04 16:36 /user/yohann/TBLS/_logs
-rw-r--r--   1 yohann supergroup        120 2021-07-04 16:36 /user/yohann/TBLS/part-m-00000
$ hadoop fs -cat /user/yohann/TBLS/part-m-00000
1,1621827317,1,0,yohann,0,1,test,MANAGED_TABLE,null,null

MySql导出表数据到Hive

  • 启动 metastore 和 hiveserver
$ hive --service metastore &
$ hive --service hiveserver &
  • 查看启动进程
$ jps
10873 RunJar
10741 RunJar

确保以上进程都存在。

  • 导出
$ hadoop fs -rmr /user/yohann/TBLS
$ sqoop import --connect jdbc:mysql://hadoop:3306/hive --username hive --password hive --table TBLS --hive-table MySql2Hive --hive-import -m 1

导出之前,先把刚刚生成的文件夹删掉

  • 查看导出结果
$ hive
hive> show tables;
mysql2hive
test
hive> desc MySql2Hive;
tbl_id                  bigint                  None
create_time             int                     None
db_id                   bigint                  None
last_access_time        int                     None
owner                   string                  None
retention               int                     None
sd_id                   bigint                  None
tbl_name                string                  None
tbl_type                string                  None
view_expanded_text      string                  None
view_original_text      string                  None
Time taken: 0.152 seconds, Fetched: 11 row(s)

登录 hive,在 hive 中查看该表。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值