Sqoop的使用
SQL——>Hadoop
# 环境需求:
Hadoop、Hive、Mysql(MariaDB)、Sqoop
# 导入数据
## 导入到HDFS
从非大数据集群导入到大数据集群
### 全部导入
```inline code
sqoop import --connect jdbc:mysql://IP:3306/company \
--username root --password root \
--table staff \
--target-dir /usr/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t"
```
### 查询导入
```inline code
sqoop import --connect jdbc:mysql://IP:3306/company \
--username root --password root \
--table staff \
--target-dir /usr/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t" \
--query 'select name,sex from staff where id <=1 and $CONDITIONS;'
```
### 导入指定列
```inline code
sqoop import --connect jdbc:mysql://IP:3306/company \
--username root --password root \
--table staff \
--target-dir /usr/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t" \
--columns id,sex \
--table staff
```
### 使用Sqoop关键字筛选查询导入数据
```inline code
sqoop import --connect jdbc:mysql://IP:3306/company \
--username root --password root \
--table staff \
--target-dir /usr/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t" \
--columns id,sex \
--table staff \
--where "id=1"
```
## 导入到HIVE
导入过程SQL——>HDFS——>Hive
```inline code
sqoop import --connect jdbc:mysql://IP:3306/company \
--username root --password root \
--table staff \
--num-mappers 1 \
--hive-import \
--fields-terminated-by "\t" \
--hive-overwrite \
--hive-table staff_hive
```
## 导入到HBase
```inline code
sqoop import --connect jdbc:mysql://IP:3306/company \
--username root --password root \
--table staff \
--columns "id,name,sex" \
--column-famliy "info" \ 列族
--hbase-create-table \
--hbase-row-key "id" \
--hbase-table "hbase_company" \
--num-mappers 1 \
--split-by id
```
#导出数据
```
sqoop export --connect jdbc:mysql://IP:3306/company \
--username root --password root \
--table staff \
--num-mappers 1 \
--export-dir /user/hive/warehouse/staff_hive \
--input-fields-terminated-by "\t"
```
注:MySQL中表不存在不会自己创建
注:在导出的时候可能会遇到导出的数据中文变成“?”的情况,只需将:
`--connect jdbc:mysql://Hadoop-01:3306/demo`
更换成:
`--connect "jdbc:mysql://Hadoop-01:3306/demo?useUnicode=true&characterEncoding=utf-8"`
注意引号不能去掉
# 脚本打包
使用opt格式的文件打包sqoop命令,然后执行
```
export
--connect jdbc:mysql://IP:3306/company
--username root --password root
--table staff
--num-mappers 1
--export-dir /user/hive/warehouse/staff_hive
--input-fields-terminated-by "\t"
```
执行脚本
sqoop --options-file opt/job_HDFStRDBMS.opt