hive导入数据到mysql最简单的方式就是从hdfs直接读取hive表文件导入mysql,当然这需要知道数据表保存的目录
如果能直接从表到表的导入,无需路径,当然是最好了
1.需要下载合适的hive-hcatalog-core-*.jar,分别复制到hive以及sqoop对应的lib目录下,并且配置环境变量,不然导入直接报错
sqoop export --connect jdbc:mysql://localhost:3306/test --username root --password root --table role1 --hcatalog-database sqoopdb --hcatalog-table role
执行结果如下:
................................
18/06/06 09:55:42 INFO hcat.SqoopHCatUtilities: Database column name - info map :
level : [Type : 4,Precision : 11,Scale : 0]
roleid : [Type : 4,Precision : 10,Scale : 0]
sex : [Type : 4,Precision : 10,Scale : 0]
name : [Type : 12,Precision : 30,Scale : 0]
dateid : [Type : 93,Precision : 19,Scale : 0]
addr : [Type : 12,Precision : 100,Scale : 0]
18/06/06 09:55:42 WARN conf.HiveConf: HiveConf of name hive.enable.spark.execution.engine does not exist 18/06/06 09:55:42
INFO common.HiveClientCache: Initializing cache: eviction-timeout=120 initial-capacity=50 maximum-capacity=50
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hive.common.util.ShutdownHookManager.addShutdownHook(Ljava/lang/Runnable;)V
at org.apache.hive.hcatalog.common.HiveClientCache.createShutdownHook(HiveClientCache.java:221)
at org.apache.hive.hcatalog.common.HiveClientCache.<init>(HiveClientCache.java:153)
at org.apache.hive.hcatalog.common.HiveClientCache.<init>(HiveClientCache.java:97)
at org.apache.hive.hcatalog.common.HCatUtil.getHiveMetastoreClient(HCatUtil.java:553)
at org.apache.hive.hcatalog.mapreduce.InitializeInput.getInputJobInfo(InitializeInput.java:104)
at org.apache.hive.hcatalog.mapreduce.InitializeInput.setInput(InitializeInput.java:88)
at org.apache.hive.hcatalog.mapreduce.HCatInputFormat.setInput(HCatInputFormat.java:95)
at org.apache.hive.hcatalog.mapreduce.HCatInputFormat.setInput(HCatInputFormat.java:51)
at org.apache.sqoop.mapreduce.hcat.SqoopHCatUtilities.configureHCat(SqoopHCatUtilities.java:386)
at org.apache.sqoop.mapreduce.ExportJobBase.runExport(ExportJobBase.java:431)
at org.apache.sqoop.manager.SqlManager.exportTable(SqlManager.java:931)
at org.apache.sqoop.tool.ExportTool.exportTable(ExportTool.java:80)
at org.apache.sqoop.tool.ExportTool.run(ExportTool.java:99)
at org.apache.sqoop.Sqoop.run(Sqoop.java:147)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
at org.apache.sqoop.Sqoop.runSqoop(Sqoop.java:183)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:234)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:243)
at org.apache.sqoop.Sqoop.main(Sqoop.java:252)
以上问题未解决
2.删掉hive的表sqoopdb.role,执行
sqoop import --connect jdbc:mysql://localhost:3306/test --username root --password root --table role --hive-import --hive-table sqoopdb.role
执行结果是:建表+导数据
mysql导入hbase
[root@host lib]# sqoop import --connect jdbc:mysql://192.168.53.122:3306/gamedw --table account --hbase-table account --column-family info --hbase-row-key accid --hbase-create-table --username 'root' -P
以上执行过程中需要输入mysql密码,验证 成功
[root@host lib]# sqoop import --connect jdbc:mysql://192.168.53.122:3306/gamedw --table account --hbase-table account --column-family info --hbase-row-key accid --hbase-create-table --username 'root' -password root
以上执行过程中不需要输入密码,验证成功
[root@host lib]# sqoop import --connect jdbc:mysql://192.168.53.122:3306/database--table datatable--hbase-table hbasetable--column-family info --hbase-row-key key --hbase-create-table --username 'root' -password root
----------------------
[root@host lib]# sqoop import --connect jdbc:mysql://192.168.53.122:3306/gamedw --query "select * from account where dateid=20180116 and $CONDITIONS" --hbase-table account111 --column-family person --hbase-row-key accid --hbase-create-table --username root --password root --split-by platid
以上语句验证成功
[root@host lib]# sqoop import --connect jdbc:mysql://192.168.53.122:3306/gamedw --table account --hbase-table account22 --column-family person --hbase-row-key accid --hbase-create-table --username root --password root --columns "platid,dateid,accid" --where " platid=1 and dateid=20180116"
以上语句将mysql的表gamedw.account的三个字段platid,dateid,accid以platid=1 and dateid=20180116为条件查询出来导入hbase,验证成功
mysql导入数据到HDFS
sqoop import --connect jdbc:mysql://192.168.116.132:3306/sqoop --username root --password 123456 --table test_user --target-dir /sqoop/test_user -m 2 --fields-terminated-by " " --columns "id,name" --where 'id>2 and id<=6'
--connect 连接数据库
--username 用户
--password 密码
--table 表名
--target-dir 目标目录/sqoop/test_user
-m 2个mapreduce执行
--fields-terminated-by 用“ ”分隔 不用逗号默认是逗号.
--columns 只导入 id和name两个字段
--where 添加条件筛选 语句要用引号抱起来
sqoop import --connect jdbc:mysql://192.168.116.132:3306/sqoop --username root --password 123456 --query 'select * from test_user where id < 9 and id > 1 and $CONDITIONS' --target-dir /sqoop/test_user4 -m 2 --split-by test_user.id
--query 直接用查询语句 这样就可以join 查询多表
-m 1 的时候可以执行 大于1就不行 如果要多个map要设置每个map读多少数据用--split-by分隔
--split-by test_user.id