sqoop导入和导出数据操作(保姆级教程)

导入:

一、将mysql数据库中的表数据导入到HDFS
表数据和格式如下:
请添加图片描述

1、首先先在hdfs中创建目录,用于导入后存放数据
hdfs dfs -mkdir /sqooptest
hdfs dfs -mkdir /sqooptest/demo1
使用sqoop import 命令将MySQL中的表导入到hdfs中

sqoop import 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table student 
--delete-target-dir 
--target-dir /sqooptest/demo1 
--m 1

执行完成后,打开浏览器进入hdfs页面,可以看到数据被导入进来了
在这里插入图片描述
点击download下载下来看看数据是否一致
在这里插入图片描述
二、MySQL导入数据到hive
首先先创建hive表,然后再导入数据(注意:hive中创建的表要与mysql中的表的数据结构相同)
使用sqoop create-hive-table命令创建hive表

sqoop create-hive-table 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table student 
--hive-table test.student_mysql

hive中使用命令show tables;查看表是否创建成功,使用desc student_mysql;查看表结构。
使用sqoop import 命令将MySQL中的表数据导入到hive表中

sqoop import 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table student 
--delete-target-dir 
--hive-table test.student_mysql 
--hive-import 
--m 1

三、新增导入(append)
在上述mysql数据导入到hdfs中,表中原先是有数据的,如果我们后续插入数据,又该怎么导入呢?

首先先导入数据

insert into student(`name`,age,gender,telephone,email,classId)
values
("东",20,"男","13910101010","[email protected]",3),
("浩",18,"男","13613309876","[email protected]",3),
("达",18,"男","13809090909","[email protected]",1)
sqoop import 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table student 
--target-dir /sqooptest/demo2 
--incremental append 
--check-column id 
--last-value 2 
--m 1

check-column 绑定id列,last-value是2因为之前表中有两条数据。
在这里插入图片描述
四、新增导入(lastmodified)
首先在mysql中创建一个新表student2

create table student2(
id int,
name varchar(32),
last_mod timestamp default current_timestamp on update 
current_timestamp
)

插入两条数据

insert into student2(id,name) values(1,'zhangsan'),(2,'lisi')

将表student2导入到hdfs中

sqoop import 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table student2 
--delete-target-dir 
--target-dir /sqooptest/demo3 
--m 1

接着再插入两条数据

insert into student2(id,name) values(3,'wangwu'),(4,'sunliu')
sqoop import 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table student2 
--target-dir /sqooptest/demo3 
--incremental lastmodified 
--check-column last_mod 
--last-value "2021-11-24 15:35:52" 
--append 
--m 1

请添加图片描述

导出:

新建一个teacher.txt vim teacher.txt
写两条数据

1,wan,jiaoshou,bd
2,jia,leader,bd
3,lin,jy,bd

上传到hdfs
在hdfs中创建目录hdfs dfs -mkdir /sqooptest/exportdata
上传teacher.txt到hdfs中hdfs dfs -put teacher.txt /sqooptest/exportdata

sqoop export 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table teacher 
--export-dir /sqooptest/exportdata/ 
--update-key id 
--update-mode allowinsert

请添加图片描述
请添加图片描述
在这里插入图片描述

另一种方式,只支持更新操作
在这里插入图片描述
修改一些数据,然后再到处看一下

sqoop export 
--connect jdbc:mysql://nnode1:3306/stu 
--username root 
--password 123123 
--table teacher 
--export-dir /sqooptest/exportdata/ 
--update-key id 
--update-mode updateonly

使用命令hdfs dfs -mkdir /sqooptest/exportdata2 创建目录,
hdfs dfs -put teacher.txt /sqooptest/exportdata2 上传文件
在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>