七十五、Sqoop——Sqoop导入数据和导出数据(内有每条语句的截图,一目了然)

本文主要是介绍七十五、Sqoop——Sqoop导入数据和导出数据(内有每条语句的截图,一目了然),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

sqoop安装好后先测试一下

[root@gree128 conf]# sqoop version
[root@gree128 conf]# sqoop help
[root@gree128 conf]# sqoop help list-databases

展示mysql的数据库
[root@gree128 ~]# sqoop list-databases \
--connect jdbc:mysql://gree128:3306 \
--username root \
--password 123456

-------------------------------------------
[root@gree128 ~]# sqoop list-tables \
> --connect jdbc:mysql://gree128:3306/mybatisdb \
> --username root \
> --password root


----------------将mysql数据库中的表数据导入到HDFS---------------------------
sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--delete-target-dir \
--target-dir /sqoop/d1 \
--m 1


--------------将表数据导入到HDFS,并且分为两个文件-----------------------------
sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--delete-target-dir \
--target-dir /sqoop/demo2 \
--split-by classId \
--fields-terminated-by '\t' \
--m 2


------------mysql 导入数据到hive (一,先创建hive表,导入数据)-------------------------------
先创建表kb15.student_mysql,数据结构与mysql中student相同
[root@gree128 ~]# sqoop create-hive-table \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--hive-table kb15.student_mysql 

hive> show tables;
OK
student_mysql

hive> desc student_mysql;
OK
id                      int                                         
name                    string                                      
age                     int                                         
gender                  string                                      
telephone               string                                      
email                   string                                      
classid                 int           

[root@gree128 ~]# sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--hive-table kb15.student_mysql \
--hive-import \
--m 1

---------------mysql 导入数据到hive (二,创建hive表导入数据,一次性完成)----------------------------
导入到student表
[root@gree128 ~]# sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--hive-import \
--hive-database kb15 \
--m 1


查找条件  where
-------------where------------------------------
[root@gree128 ~]# sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--where "classId=1" \
--target-dir /sqoop/d3 \
--delete-target-dir \
--m 1

[root@gree128 ~]# sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--where "telephone='13613309876'" \
--target-dir /sqoop/d4 \
--delete-target-dir \
--m 1

[root@gree128 ~]# sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--where "telephone='13613309876' and classId=2" \
--target-dir /sqoop/d5 \
--delete-target-dir \
--m 1


-------------- query -----------------------------
sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--query 'select id,name,age,gender,telephone,email,classId from student where id>3 and classId=4 and $CONDITIONS' \
--target-dir /sqoop/d6 \
--delete-target-dir \
--m 1

sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--query 'select id,name,age,gender,telephone,email,classId from student where id>3 and classId=4 and $CONDITIONS' \
--target-dir /sqoop/d7 \
--delete-target-dir \
--fields-terminated-by '\t' \
--split-by classId \
--m 1

-----------增量导入--------------------------------
sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--delete-target-dir \
--target-dir /sqoop/in1 \
--m 1
insert into student(name,age,gender,telephone,email,classId) values
("刘勇",24,"男","13695847598","liuyong@qq.com",1),
("刘小勇",2,"男","1360000000","liuxiaoyong@qq.com",1)


sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--target-dir /sqoop/in1 \
--incremental append \
--check-column id \
--last-value 4 \
--m 1

-------------incremental 增量导入 lastmodified   <append  ,  merge-key> ------------------------------
create table student3(
id int,
name varchar(32),
last_mod timestamp default current_timestamp on update current_timestamp
)

insert into student3(id,name) values(1,'zhangxiaohua'),(2,'litiechui');

select * from student3;

sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student3 \
--delete-target-dir \
--target-dir /sqoop/in2 \
--m 1

insert into student3(id,name) values(3,'zhaodaqiang'),(4,'chenxiaowang');

sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student3 \
--target-dir /sqoop/in2 \
--incremental lastmodified \
--check-column last_mod \
--last-value "2021-11-24 17:08:34" \
--append \
--m 1

-----------------incremental增量导入   lastmodified   <append  ,  merge-key> --------------------------

sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student3 \
--delete-target-dir \
--target-dir /sqoop/incre3 \
--m 1

insert into student3(id,name) values(5,'zhaoweiwei'),(6,'liangshanbo');

导入全部的数据
sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student3 \
--target-dir /sqoop/incre3 \
--incremental lastmodified \
--check-column last_mod \
--last-value "2021-11-24 17:12:59" \
--merge-key id \
--m 1

导入新增的数据
sqoop import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student3 \
--target-dir /sqoop/incre3 \
--incremental lastmodified \
--check-column last_mod \
--last-value "2021-11-24 17:12:34" \
--append \
--m 1

------------导出 export---- allowinsert updateonly ---------------------------
[root@gree128 tmp]# vi ./teacher.txt 

1,gree,jy,dsj
2,cat,leader,dsj

[root@gree128 tmp]# hdfs dfs -mkdir  -p/sqoop/exportdata/
[root@gree128 tmp]# hdfs dfs -put ./teacher.txt /sqoop/exportdata/


[root@gree128 tmp]# sqoop export \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table teacher \
--export-dir /sqoop/exportdata/ \
--update-key id \
--update-mode allowinsert

先修改表数据,在运行语句
[root@gree128 tmp]# sqoop export \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table teacher \
--export-dir /sqoop/exportdata2/ \
--update-key id \
--update-mode updateonly  (表数据只读)


[root@gree128 tmp]# sqoop export \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table teacher \
--export-dir /sqoop/exportdata3/ \
--update-key id \
--update-mode allowinsert

-----------job --------------------------------

sqoop job --create demojob -- import \
--connect jdbc:mysql://gree128:3306/mybatisdb \
--username root \
--password 123456 \
--table student \
--delete-target-dir \
--target-dir /sqoop/job/ \
--m 1

sqoop job的基本语句

[root@gree128 tmp]# sqoop job --list
[root@gree128 tmp]# sqoop job --exec demojob          执行
[root@gree128 tmp]# sqoop job --delete demojob
[root@gree128 tmp]# sqoop job --show demojob

这篇关于七十五、Sqoop——Sqoop导入数据和导出数据(内有每条语句的截图,一目了然)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/877575

相关文章

oracle 11g导入\导出(expdp impdp)之导入过程

《oracle11g导入导出(expdpimpdp)之导入过程》导出需使用SEC.DMP格式,无分号;建立expdir目录(E:/exp)并确保存在;导入在cmd下执行,需sys用户权限;若需修... 目录准备文件导入(impdp)1、建立directory2、导入语句 3、更改密码总结上一个环节,我们讲了

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

MySQL中REPLACE函数与语句举例详解

《MySQL中REPLACE函数与语句举例详解》在MySQL中REPLACE函数是一个用于处理字符串的强大工具,它的主要功能是替换字符串中的某些子字符串,:本文主要介绍MySQL中REPLACE函... 目录一、REPLACE()函数语法:参数说明:功能说明:示例:二、REPLACE INTO语句语法:参数

Java实现TXT文件导入功能的详细步骤

《Java实现TXT文件导入功能的详细步骤》在实际开发中,很多应用场景需要将用户上传的TXT文件进行解析,并将文件中的数据导入到数据库或其他存储系统中,本文将演示如何用Java实现一个基本的TXT文件... 目录前言1. 项目需求分析2. 示例文件格式3. 实现步骤3.1. 准备数据库(假设使用 mysql