sqoop使用教程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fibonacci2015/article/details/79283758
1.sqoop导数据
0.基础使用
sqoop list-databases
--connect "jdbc:mysql://10.1.6.21:3306/"
--username root
--password 123456

sqoop list-tables
--connect "jdbc:mysql://10.1.6.21:3306/platform_log"
--username root
--password 123456
1.将数据表映射为hive表
sqoop create-hive-table 
--connect "jdbc:mysql://10.1.6.21:3306/platform_log?useUnicode=true&characterEncoding=utf-8&useSSL=false" 
--table log_customer_browse 
--username root
--password 123456
--hive-table platform_log.log_customer_browse 
--fields-terminated-by "\t" 
--lines-terminated-by "\n";
2.导入hive数据 -- query
sqoop import
--connect "jdbc:mysql://10.1.6.21:3306/platform_log?useUnicode=true&characterEncoding=utf-8&useSSL=false"
--username root
--password 123456
--query "select * from platform_log.log_customer_browse where open_time < '2017-02-20' AND \$CONDITIONS"
// 上面一行,用这个也行 --table log_customer_browse --where "open_time > '2017-02-20'"  
--delete-target-dir
--target-dir /user/hive/warehouse/platform_log
--hive-table platform_log.log_customer_browse
--direct
--hive-import
--split-by open_time -m 5
--direct
--hive-overwrite
--fields-terminated-by '\t'
--null-string '\\N'
--null-non-string '\\N';
注意:--target-dir /user/hive/warehouse/platform_log    可以用  --hive-import --hive-table platform_log 进行替换
3.导入hive数据,不带query直接撸数据
sqoop import
--connect "jdbc:mysql://192.168.1.22:4417/platform_log?useUnicode=true&characterEncoding=utf-8"
--username dev
--password 123456
--table log_customer_browse
--hive-table platform_log.log_customer_browse
--hive-import
--split-by open_time -m 5
--fields-terminated-by '\t'
--null-string '\\N'
--null-non-string '\\N';
4.导出数据到mysql
sqoop export
--connect "jdbc:mysql://hadoop02:3306/test?useUnicode=true&characterEncoding=utf-8"
--username root
--password 123456
--table customer_statistics_stat
--export-dir /user/hive/warehouse/stat.db/union_finall
--input-fields-terminated-by '\t'
--input-null-string '\\N'
--input-null-non-string '\\N';
5.导入数据到hbase
sqoop import
--connect "jdbc:mysql://192.168.1.22:4417/platform_log?useUnicode=true&characterEncoding=utf-8"
--username dev
--password 123456
--table log_track_copy
--hbase-table log_track
--hbase-row-key id // 需要指定rowkey
--split-by create_time -m 5
--column-family track;
3.sqoop 参数

4.sqoop1
1.--fields-terminated-by '\t',表示将数据导入到 Hadoop中列记录之间的间隔符,默认符号为英文逗号。这里通常使用制表符\t来间隔数据,避免数据再次从HDFS到入到关系数据库时引起分割混乱
2.-m 1,是--num-mappers的缩写,表示指定MapReduce的个数为1个(默认会自动开启多个),sqoop转化的MR程 序不包含reduce
3.--append,表示数据导入到hadoop的方式为追加,否则不允许重复导入
4.--check-column '主键列名' --incremental append --last-value 5,表示数据为增量导入,根据--last-value的值来判断,有大于这个值的记录则执行导入,否则不执行导入操作
5.表示数据为增量导入,根据--last-value的值来判断, 有大于这个值的记录则执行导入,否则不执行导入操作
6.--hive-import,表示将数据导入到Hive中;
7.--where '',数据筛选条件
8.-e 或--query 'select * from table where id>5 and $CONDITIONS',自定义导入数据的sql语句。使用自定义sql语句 需要注意:
① 使用了自定义sql就不能指定--table;
② 自定义sql语句的where条件中必须包含字符串"$CONDITIONS",$CONDITIONS是一个变量,用于给多个map任务划分任务范 围;
③ 使用自定义sql时,如果通过参数-m指定多个map任务,由于自定义sql中可能存在多表查询,因此必须使用参数“--split-by 表名.字段名”指定多个map任务分割数据的根据字段,如--split-by users.id;
9. --target-dir,显示指定数据导入到HDFS中的位置,默认保存路径为:/user/{当前用户}/{表名}/表数据文件,
如果导入时需要将已存在的HDFS文件删除,可使用--delete-target-dir

猜你喜欢

转载自blog.csdn.net/fibonacci2015/article/details/79283758