sqoop基本操作

列出数据库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  

在这里插入图片描述
在这里插入图片描述
细水长流~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值