sqoop 常见用法

1 mysql -> hdfs

bin/sqoop import --connect jdbc:mysql://172.16.235.128:3306/test --username root --password 'my' --table items  --m 1

 

2 mysql->hive

bin/sqoop import --connect jdbc:mysql://1725.128:3306/test --username root --password 'my' --table items --hive-import --m 1

(会直接在hive中创建一个一模一样的表)

 

3  bin/sqoop import --connect jdbc:mysql://1725.128:3306/test --username root --password 'mys' --table items --m 1 --target-dir /wherequery --where "id=2"


bin/sqoop import --connect jdbc:mysql://17.128:3306/test --username root --password 'mys' --m 1 --target-dir /wherequery2 --query 'select id,name,price,detail from items where price > 200 and $CONDITIONS' --split-by id --fields-terminated-by ','

--query ‘’引号中为sql语句 如果有where后面加上and $CONDITIONS --split-by按照什么切片, --fields-terminated-by 导入到hive中按照什么分割

 

4增量导入

 bin/sqoop import --connect jdbc:mysql://17128:3306/test --username root --password 'm6' --m 1 --table items --incremental append --check-column id --last-value 2

--check-column 以哪个column为参考, --last-value最后一次导入的值

 

5 hdfs->mysql

首先需要在mysql中建立一个与之相对应的表

bin/sqoop export --connect jdbc:mysql://172.:3306/test --username root --password 'my6' --table items --export-dir /wherequery
 

如果导出的数据库是mysql 则可以添加一个属性 --direct可以加快导出
 

 

sqoop import \
--connect jdbc:mysql://172.168.100.122:3306/test \
--username root \
--password root \
--query 'select * from mydb where  $CONDITIONS and DATE(created_time)<=DATE_SUB(NOW(),INTERVAL 1 DAY) ' \
--fields-terminated-by '\t' \
--target-dir /user/hive/test \
--delete-target-dir \
--num-mappers 1 \
--hive-import \
--hive-database test \
--hive-table test \
--hive-partition-key create_time \
--hive-partition-value xxxxxx
--direct 

where条件需加¥CONDITIONS

target-dir 只是个临时目录,因为sqoop从mysql导入到hive中,分两部首先导入到hdfs即target-dir路径,然后load 到hive中

然后会删除targetdir  分区表就是以次来作为不同的层级,--direct可以加快导出速度

hive-overwrite参数 覆盖

 

hive->mysql 

sqoop export \
  --hcatalog-database dm \
  --hcatalog-table foods \
  --hcatalog-partition-keys datetime \
  --hcatalog-partition-values 20171206 \
  --connect jdbc:mysql://${mysql_host}:${mysql_port}'/eat?useUnicode=true&characterEncoding=utf-8'\
  --username $mysql_user \
  --password $mysql_password \
  --table $table_name \
  --columns $table_columns \
  --update-key $table_update_key \
  --map-column-java dt=String \
  --update-mode allowinsert
  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值