Sqoop 使用shell命令的各种参数的配置及使用方法
点击上方蓝色字体,选择“设为星标”


2.1.1 全表导入(部分导入)
bin/sqoop import \##连接的关系型数据库的url,用户名,密码--connect jdbc:mysql://hadoop102:3306/test \--username root \--password 123 \##连接的表--table t_emp \##导出数据在hdfs上存放路径--target-dir /sqoopTest \##如果路径已存在则先删除--delete-target-dir \##导入到Hdfs上后,每个字段使用什么参数进行分割--fields-terminated-by "\t" \##要启动几个MapTask,默认4个--num-mappers 2 \##数据集根据哪个字段进行切分,切分后每个MapTask负责一部分--split-by id \##要实现部分导入,加入下面的参数,表示导入哪些列##columns中如果涉及到多列,用逗号分隔,分隔时不要添加空格--columns id,name,age
2.1.2 使用sqoop关键字筛选查询导入数据
bin/sqoop import \--connect jdbc:mysql://hadoop102:3306/test \--username root \--password 123 \--table t_emp \##指定过滤的where语句,where语句最好使用引号包裹--where 'id>6' \--target-dir /sqoopTest \--delete-target-dir \--fields-terminated-by "\t" \--num-mappers 1 \--split-by id
2.1.3 使用查询语句导入
bin/sqoop import \--connect jdbc:mysql://hadoop102:3306/test \--username root \--password 123 \##查询语句最好使用单引号##如果query后使用的是双引号,则$CONDITIONS前必须加转移符,防止shell识别为自己的变量--query 'select * from t_emp where id>3 and $CONDITIONS' \--target-dir /sqoopTest \--delete-target-dir \--fields-terminated-by "\t" \--num-mappers 1 \--split-by id
bin/sqoop import \--connect jdbc:mysql://hadoop102:3306/test \--username root \--password 123 \--query 'select * from t_emp where id>3 and $CONDITIONS' \--target-dir /sqoopTest \##如果不限定分隔符,那么hive存储的数据将不带分隔符,之后再想操作很麻烦,所以建议加上--fields-terminated-by "\t" \--delete-target-dir \##导入到hive--hive-import \##是否覆盖写,不加这个参数就是追加写--hive-overwrite \##指定要导入的hive的表名--hive-table t_emp \--num-mappers 1 \--split-by id
import_data(){$sqoop import \--connect jdbc:mysql://hadoop102:3306/gmall \--username root \--password 123 \--target-dir /origin_data/gmall/db/$1/$do_date \--delete-target-dir \--query "$2 and \$CONDITIONS" \--num-mappers 1 \--fields-terminated-by '\t' \# 使用压缩,和指定压缩格式为lzop--compress \--compression-codec lzop \#将String类型和非String类型的空值替换为\N,方便Hive读取--null-string '\\N' \--null-non-string '\\N'}
bin/sqoop import \--connect jdbc:mysql://hadoop102:3306/test \--username root \--password 123 \--query 'select * from t_emp where id>3 and $CONDITIONS' \--target-dir /sqoopTest \--delete-target-dir \##表不存在是否创建--hbase-create-table \##hbase中的表名--hbase-table "t_emp" \##将导入数据的哪一列作为rowkey--hbase-row-key "id" \##导入的列族--column-family "info" \--num-mappers 2 \--split-by id
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.hbase.HTableDescriptor.addFamily(Lorg/apache/hadoop/hbase/HColumnDescriptor;)V

bin/sqoop export \--connect 'jdbc:mysql://hadoop102:3306/test?useUnicode=true&characterEncoding=utf-8' \--username root \--password 123 \##导出的表名,需要自己提前创建好--table t_emp2 \--num-mappers 1 \##hdfs上导出的数据的路径--export-dir /user/hive/warehouse/t_emp \##hdfs上数据的分隔符--input-fields-terminated-by "\t"
INSERT INTO t_emp2 VALUE(5,'jack',30,3,1111)ON DUPLICATE KEY UPDATE NAME=VALUES(NAME),deptid=VALUES(deptid),empno=VALUES(empno);
3.2.1updateonly模式
bin/sqoop export \--connect 'jdbc:mysql://hadoop103:3306/mydb?useUnicode=true&characterEncoding=utf-8' \--username root \--password 123456 \--table t_emp2 \--num-mappers 1 \--export-dir /hive/t_emp \--input-fields-terminated-by "\t" \--update-key id
3.2.2allowinsert模式
bin/sqoop export \--connect 'jdbc:mysql://hadoop103:3306/mydb?useUnicode=true&characterEncoding=utf-8' \--username root \--password 123456 \--table t_emp2 \--num-mappers 1 \--export-dir /hive/t_emp \--input-fields-terminated-by "\t" \--update-key id \--update-mode allowinsert
3.3.1配置/etc/my.cnf
bin/sqoop export \--connect 'jdbc:mysql://hadoop103:3306/mydb?useUnicode=true&characterEncoding=utf-8' \--username root \--password 123456 \--table t_emp2 \--num-mappers 1 \--export-dir /hive/t_emp \--input-fields-terminated-by "\t" \--update-key id \--update-mode allowinsert
3.3.2重启mysql服务
3.3.3进入/var/lib/mysql,调用方法
sudo mysqlbinlog mysql-bin.000001
文章不错?点个【在看】吧! ?
评论



