Browse Source

添加 sqoop

wudla 4 years ago
parent
commit
215b009c43
1 changed files with 150 additions and 0 deletions
  1. 150 0
      docs/dw/sqoop.md

+ 150 - 0
docs/dw/sqoop.md

@@ -0,0 +1,150 @@
+# Sqoop安装与使用
+
+## 安装
+
+1. 下载地址:http://mirrors.hust.edu.cn/apache/sqoop/1.4.6/
+
+2. 上传服务器,解压到指定目录(过程略)
+
+3. 修改配置文件/opt/module/sqoop-1.4.6/conf 目录下 sqoop-env.sh
+
+   ```sh
+   # 各个目录根据环境自行修改
+   export HADOOP_COMMON_HOME=/opt/module/hadoop-2.7.2
+   export HADOOP_MAPRED_HOME=/opt/module/hadoop-2.7.2
+   export HIVE_HOME=/opt/module/hive
+   export ZOOKEEPER_HOME=/opt/module/zookeeper-3.4.10
+   export ZOOCFGDIR=/opt/module/zookeeper-3.4.10/conf
+   export HBASE_HOME=/opt/module/hbase
+   ```
+
+4. 拷贝 JDBC 驱动
+
+   ```shell
+   cp mysql-connector-java-5.1.27-bin.jar  $SQOOP_HOME/lib/
+   ```
+
+5. 验证测试
+
+   1. command 验证
+
+      ```shell
+      bin/sqoop help
+      ```
+
+   2. 测试是否可以连接数据库
+
+      ```shell
+      # 列出连接数据库的所有数据库名
+      bin/sqoop list-databases 
+      --connect jdbc:mysql://localhost2:3306/ 
+      --username root 
+      --password 000000
+      ```
+
+## 简介
+
+### 参数说明
+
+```shell
+# 数据库url
+--connect jdbc:mysql://hadoop102:3306/company \
+
+# 数据库用户名
+--username root \
+
+# 数据库密码
+--password 000000 \
+
+# 指定表名(注:从方式导入数据才会自动指定路径)
+--table staff \
+
+# 指定要导入的列
+--columns id,name
+
+# 指定要导入的列,where条件需要加 ''
+--where 'id >= 10'
+
+# 自定义 SQL, where 后面的 $CONDITIONS 是必须的
+# 与--split-by id 一起使用
+# 需要指定 --target-dir 的路径,此方式导入数据不会自动创建
+--query 'select id, name from tableName where $CONDITIONS '
+
+# 根据指定的列分区,该列为数字
+--split-by id 
+
+# 指定保存目录,不指定则默认使用数据库
+--target-dir /user/company \
+
+# 删除路径,不建议使用,不安全
+--delete-target-dir \
+
+# map的个数
+--num-mappers 1 \
+
+# 在hdfs存储格式,指定分隔符
+--fields-terminated-by "\t"
+```
+
+
+
+### 案例
+
+1. sqoop to hive
+
+   ```shell
+   bin/sqoop import \
+   --connect jdbc:mysql://host:3306/company \
+   --username root \
+   --password 000000 \
+   --table tableName \
+   --num-mappers 1 \
+   
+   # hive 导入
+   --hive-import \
+   --fields-terminated-by "\t" \
+   
+   # 覆写
+   --hive-overwrite \
+   
+   # 指定 hive 表
+   --hive-table staff_hive
+   ```
+
+2. sqoop to hbase
+   
+   <font color='red'>sqoop1.4.6只支持HBase1.0.1之前的版本的自动创建HBase表的功能</font>
+
+   ```shell
+   bin/sqoop import \
+   --connect jdbc:mysql://hadoop102:3306/company \
+   --username root \
+   --password 000000 \
+   --table company \
+   --columns "id,name,sex" \
+   
+   # 只能往一个列族导数据
+   --column-family "info" \
+   
+   # hbase 自动创建表
+   --hbase-create-table \
+   --hbase-row-key "id" \
+   --hbase-table "hbase_company" \
+   --num-mappers 1 \
+   --split-by id
+   ```
+
+3. sqoop to mysql
+   
+   <font color='red'>Mysql中如果表不存在,不会自动创建</font>
+
+   ```shell
+   bin/sqoop export \
+   --connect jdbc:mysql://hadoop102:3306/company \
+   --username root \
+   --password 000000 \
+   --table staff \
+   --num-mappers 1 \
+   --export-dir /user/hive/warehouse/staff_hive \
+   --input-fields-terminated-by "\t"
+   ```