sqoop数据导入

创建数据库 

mysql全表数据导入hdfs

mysql查询数据导入hdfs

mysql指定列导入hdfs

使用查询条件关键字将mysql数据导入hdfs

mysql数据导入hive 


创建数据库 

        hive中创建user表

create table users(
id bigint,
name string
)
row format delimited fields terminated by "\t";

        mysql中创建user表并添加信息

CREATE TABLE `user`  (
  `id` int(20),
  `name` varchar(20)
);

 mysql全表数据导入hdfs

        全部导入 

bin/sqoop import \
--connect jdbc:mysql://master:3306/spark-sql \
--username root \
--password p@ssw0rd \
--table user \
--target-dir /user/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t"

        参数解读:

bin/sqoop import \  导入命令
--connect jdbc:mysql://master:3306/spark-sql \ 选择数据源的mysql数据库路径
--username root \ mysql用户名
--password p@ssw0rd \  mysql用户密码
--table user \ 数据源表名
--target-dir /user/company \ 导入到的hdfs路径
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t" 

        hdfs中查看导入的结果 

mysql查询数据导入hdfs

        注意:使用查询导入时必须要在查询语句后面加上 $CONDITIONS

bin/sqoop import \
--connect jdbc:mysql://master:3306/spark-sql \
--username root \
--password p@ssw0rd \
--target-dir /user/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t" \
--query 'select * from user where id = 2 and $CONDITIONS;'

        hdfs中查看导入的结果 

mysql指定列导入hdfs

        使用 --columns 来指定导入的列

bin/sqoop import \
--connect jdbc:mysql://master:3306/spark-sql \
--username root \
--password p@ssw0rd \
--target-dir /user/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t" \
--columns name \
--table user

         hdfs中查看导入的结果 

 使用查询条件关键字将mysql数据导入hdfs

        使用 --where来指定查询的条件

bin/sqoop import \
--connect jdbc:mysql://master:3306/spark-sql \
--username root \
--password p@ssw0rd \
--target-dir /user/company \
--delete-target-dir \
--num-mappers 1 \
--fields-terminated-by "\t" \
--table user \
--where "id=1"

        hdfs中查看导入的结果 

 mysql数据导入hive 

        其实将mysql导入hive中 在中间还是在hdsf中中转存储了一下 然后在从hdfs导入到hive中 

bin/sqoop import \
--connect jdbc:mysql://master:3306/spark-sql \
--username root \
--password p@ssw0rd \
--table user \
--num-mappers 1 \
--hive-import \
--fields-terminated-by "\t" \
--hive-overwrite \
--hive-table default.users

        参数解读:

--hive-import \ 导入hive的命令
--hive-overwrite \ 写入方式
--hive-table default.users hive表 指定数据库中的表:数据库.表

         在hive中查看导入结果

        导入hive并添加分区字段 

sqoop import \
    --connect jdbc:mysql://mysql_host/mydatabase \
    --username mysql_username \
    --password mysql_password \
    --table my_table \
    --hive-import \
    --create-hive-table \
    --hive-table my_db.my_table \
    --hive-partition-key part_col \
    --hive-partition-value '2023-04-13' \
    --target-dir /user/hive/warehouse/my_db.db/my_table/part_col=2023-04-13;
--create-hive-table:生成与关系数据库表结构对应的 hive 表结构
--hive-partition-key:生成分区字段名称
--hive-partition-value:设置分区字段值
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

open_test01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值