sqoop基础

使用sqoop连接mysql时,如果java的版本过高有密码安全机制

查询时需要在mysql地址后加上 useSSL=false

sqoop -list-databases --connect jdbc:mysql://localhost:3306?useSSl=false --username root -password long

将mysql的数据导入到hdfs上

1
sqoop import  --connect jdbc:mysql://localhost:3306/testDatabase?useSSl=false --username root -password long --table tableName --columns 'id,name'

import 会将mysql的数据导入到hdfs中

将mysql数据的表结构复制到hive中

1
sqoop create-Hive-table --connect jdbc:mysql://localhost:3306/testDatabase?useSSL=false --username root -password long --table tableName --hive-table users --fields-terminated-by "," 

将数据从mysql数据库导入文件到hive表中——import

1
2
3
4
5
sqoop import --connect jbdc:mysql://localhost:3306/testDatabase 
--username root
--password long
--table tableName
--hive-import --hive-table users -m 2 --fields-terminated-by ","

参数说明:
1、-m 2 表示由两个map作业执行;
2、–fields-terminated-by “,” 需同创建hive表时保持一致;
注意:先根据mysql表的结构得到hive表,然后将mysql表中的数据导入到hive表(也就是导入到HDFS,因为hive表的数据是存在hdfs上的warehouse)

–columns –where 语句使用

1
2
3
4
5
6
sqoop import --append --connect jdbc:mysql://loclhost:3306/test --username root --password long --table tableName 
--columns "id,age,name"
--where "id > 3 and (age = 88 or age = 80)"
-m 1
--hive-import --hive-table userinfos2
--fields-terminated-by ",";

追加导入

1
2
3
sqoop import 
--connect jdbc:mysql://db.foo.com/somedb --table sometable
--where "id > 100000" --target-dir /incremental_dataset –append

将hive的数据导入到myslq

1
2
3
4
5
6
sqoop export 
--connect jdbc:mysql://localhost:3306/trafficdata?useSSL=false
--username root --password hadoop
-table order_province_time
--export-dir /user/hive/warehouse/trafficdata.db/order_province_time
--input-fields-terminated-by '\001'

Hive 的元数据在mysql中,但是Hive的数据存在hdfs上

hive数据库保存的路径 => /user/hive/warehouse/trafficdata.db

hive默认的字段分隔符为’\001’

如果出现拒绝连接的错误信息,请检查mysql中root是否开放远程连接,后将localhost改为ip地址。或者开启hive metastore 服务

其他sqoop常用命令

sqoop常用命令