列出数据库sakila中的表:
sqoop list-tables --connect jdbc:mysql://localhost/sakila --username root --P
创建一张跟数据库sakila中city表一样的hive表city_copy
sqoop create-hive-table --connect jdbc:mysql://localhost/sakila --username root --P --table city --hive-table city_copy
报错了,Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf
此时需要把hive/3.1.2/libexec/lib的hive-common-3.1.2.jar包放到sqoop的lib中去,重新运行就可以了。
上面建的表,没有指明在哪个数据库,故默认存储到default数据库下面了。
若要放到某个数据库下:
sqoop create-hive-table --connect jdbc:mysql://localhost/sakila --username root --P --table city --hive-table mydb.city_copy
sqoop的数据导入
1)从RDBMS导入到HDFS中,语法:sqoop import (generic-args) (import-args)
备注m表示map任务的个数
将sakila数据库下面的city表传递到hdfs上,默认在/user/XXX(用户名)/目录下面
回顾下查看hdfs上的文件内容
以上可知默认是用逗号分隔的,如果要指定分隔符和上传的路径,该如何写呢?
下例子为以\t为分隔符
sqoop import --connect jdbc:mysql://localhost/sakila --username root --P --table city --target-dir /user/XXX/city_split --fields-terminated-by '\t' --m 2
#此时m为2
查看内容
带条件导入
sqoop import --connect jdbc:mysql://localhost/sakila --username root --P --where "city_id = 4" --table city --target-dir /user/XXX/city_select --m 1
先看数据库中搜素出来的结果:
查看hdfs中:
导入某一列
sqoop import --connect jdbc:mysql://localhost/sakila --username root --P --columns "last_update" --where "city = 'Acua'" --table city --target-dir /user/XXX/city_choose --m 1
这种类似于查询
select last_update from city where city='Acua'
自定义查询sql
这里的$CONDITIONS一定要有,它被解释成: where (1 = 0) 这都行!!
sqoop import --connect jdbc:mysql://localhost/sakila --username root --P --target-dir /user/XXX/city_customize --query 'select city_id,country_id,last_update from city where $CONDITIONS' --split-by city_id --fields-terminated-by '\t' --m 1
把mysql数据库中的表数据导入到hive中
sqoop导入关系型数据到hive的过程是先导入到hdfs,然后在load进入hive
普通导入:数据存储在默认的default hive库中,表名就是对应的mysql的表名。
sqoop import --connect jdbc:mysql://localhost/sakila --username root --P --target-dir /user/XXX/city_import_hive --table city --hive-import --m 1
在hdfs链接上,在运行的过程中,是看到有city_import_hive这个目录的,在运行完后,被删除了。
此时是默认到hive中default数据库中:
放到指定数据库mydb中
sqoop import --connect jdbc:mysql://localhost/sakila --username root --password root --table city --fields-terminated-by "\t" --lines-terminated-by "\n" --hive-import --hive-overwrite --create-hive-table --delete-target-dir --hive-database mydb --hive-table new_city_table
细水长流~