Fork me on GitHub

sqoop之旅5-sqoop实例

假设MySQL数据库中有一张表,库名是sqooptest,表名是digdata,表的字段包含:

  • class_id
  • class_name
  • class_month
  • teacher

导入HDFS中

1
2
3
4
5
6
7
sqoop import \
--connect jdbc:mysql://ubuntu:3306/sqooptest \ # ubuntu 是主机名;sqoop 是数据库的名字
--username root \
--password 123456 \
--table bigdata \ # 指定导入的表名
--warehouse-dir /sqoopim # 导入到HDFS中的目录
-m 1

导入Hive中

1
2
3
4
5
6
7
8
sqoop import \
--connect jdbc:mysql://ubuntu:3306/sqooptest \ # ubuntu 是主机名;sqoop 是数据库的名字
--username root \
--password 123456 \
--table sqoop \ # 指定导入到hive中的表名
--hive-default \
--create-table \ # 创建表
-m 1

查看远程服务器的数据库

1
2
3
4
5
6
7
8
9
10
sqoop list-databases \  # 显示所有的数据库
--connect jdbc:mysql://ubuntu:3306 \
--username root \
--password 123456 \


sqoop list-tables \ # 显示所有sqooptest数据库的中的表
--connect jdbc:mysql://ubuntu:3306/sqooptest/ \
--username root \
--password 123456 \

密码操作

  1. 直接使用明文
1
2
3
4
5
6
7
sqoop import \
--connect jdbc:mysql://ubuntu:3306/sqooptest \
--username root \
--password 123456 \ # 明文形式
--table bigdata \
--warehouse-dir /sqoopim
-m 1
  1. 使用暗文,交互式输入密码
1
2
3
4
5
6
sqoop import \
--connect jdbc:mysql://ubuntu:3306/sqooptest \ # ubuntu 是主机名;sqoop 是数据库的名字
--username root \
-P # 在接下来的额交互式界面中输入密码
--table bigdata \
-m 1
  1. 通过password-profile形式
  • 将密码写进一个文件中
1
2
3
4
5
6
7
8
9
>> vim password.txt
>> pwd
/usr/root/password.txt # 假设为该路径

sqoop import \
--connect jdbc:mysql://ubuntu:3306/sqooptest \ # ubuntu 是主机名;sqoop 是数据库的名字
--username root \
--password-profile \ # 在接下来的额交互式界面中输入密码
/usr/root/password.txt # 密码文件路径

指定分隔符

分隔符默认的是逗号,通过fields-terminated-by来指定分隔符

1
2
3
4
5
6
7
sqoop import 
--connect jdbc://mysql://ubuntu:3306/sqooptest \
--username root \
--password 123456 \
--table bigdata \
-m 2 \
--fields-terminated-by "|"

使用文件进行导入

  • 编辑文件sqoop.im,参数和它们的值必须各一行
1
2
3
4
5
6
7
8
9
10
11
12
13
import 
--connect
jdbc://mysql://ubuntu:3306/sqooptest
--username
root
--password
123456
--table
bigdata
--columns
class_id,teacher,class_name
-m
2
  • 执行文件,通过options-file
1
sqoop --options-file sqoop.im

本文标题:sqoop之旅5-sqoop实例

发布时间:2019年11月20日 - 13:11

原始链接:http://www.renpeter.cn/2019/11/20/sqoop%E4%B9%8B%E6%97%855-sqoop%E5%AE%9E%E4%BE%8B.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea