Sqoop是一款数据迁移工具,将关系型数据库数据库与hdfs里面的数据相互迁移的工具。在Hadoop之上,将自己的语法转换成MapReduce来并行读取数据库里面的数据,将分析之后的数据保存在hdfs。
1.mysql –> hdfs (sqoop 方式过滤数据)
1 2 3 4
| ./sqoop import –connect jdbc:mysql://192.168.10.1000:3306/hnbian –username root –password 123 –table trade_detail –target-dir /sqoop/td1 -m 2 –fields-terminated-by ‘/t’ –columns “id,account,income” –where “id>2 and id<=9”
|
| 命令 |
含义 |
| ./sqoop |
Sqoop 命令 |
| import |
指定要进行哪种操作(导入) |
| –connect jdbc:mysql://192.168.10.1000:3306/ hnbian –username root –password 123 |
连接数据库 |
| –table trade_detail |
指定表名(trade_detail) |
| –target-dir /sqoop/td1 |
指定导出的文件放在哪里(/sqoop/td1) |
| -m 2 |
启动几个mapper(2) |
| –fields-terminated-by ‘\t’ |
指定到处的列与列之间的分隔符(’\t’)默认(,)逗号 |
| –columns “id,account,income” |
指定要导入表中的哪些列(”id,account,income” ) |
| –where “id>2 and id<=9” |
指定要导入的数据的where条件 (”id>2 and id<=9″) |
2.mysql –> hdfs (sql 方式过滤数据)
1 2 3
| ./sqoop import –connect jdbc:mysql://192.168.10.1000:3306/hnbian –username root –password 123 –query ‘select * from trade_detail where id>5 and $CONDITIONS’ –target-dir /sqoop/td4 -m 1 –fields-terminated-by ‘/t’ –split-by trade_detail.id
|
| 命令 |
含义 |
| ./sqoop |
Sqoop 命令 |
| import |
指定要进行哪种操作(导入) |
| –connect jdbc:mysql://192.168.10.1000:3306/hnbian –username root –password 123 |
连接数据库 |
| –query ‘select * from trade_detail where id>5 and $CONDITIONS’ |
指定query 中间可以写sql 注意语句中必须添加$CONDITIONS’ 且查询时启动多个mapper必须指定分割split-by |
| $CONDITIONS: |
用来检查数据库里面有多少数据 |
| –target-dir /sqoop/td2 |
指定导出的文件放在哪里(/sqoop/td2) |
| -m 1 |
启动几个mapper(1) |
| –fields-terminated-by ‘\t’ |
指定到处的列与列之间的分隔符(’\t’)默认(,)逗号 |
| –split-by trade_detail.id |
指定分割数据 通过(id)分割 |
3.hdfs –> mysql
1 2 3
| ./sqoop export –connect jdbc:mysql://192.168.10.1000:3306/hnbian –username root –password 123 –export-dir ‘/sqoop/td3’ –table t2 -m 1 –fields-terminated-by ‘/t’
|
| 命令 |
含义 |
| ./sqoop |
Sqoop 命令 |
| export |
指定要进行哪种操作(导出) |
| –connect jdbc:mysql://192.168.10.1000:3306/ hnbian –username root –password 123 |
连接数据库 |
| –export-dir ‘/sqoop/td3’ |
指定到处的文件夹 |
| –table t2 |
指定导出的文件放在哪张表里面(t2) |
| -m 1 |
启动几个mapper(1) |
| –fields-terminated-by ‘\t’ |
指定到处的列与列之间的分隔符(’\t’)默认(,)逗号 |