七十五、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

相关文章

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

java如何实现高并发场景下三级缓存的数据一致性

《java如何实现高并发场景下三级缓存的数据一致性》这篇文章主要为大家详细介绍了java如何实现高并发场景下三级缓存的数据一致性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 下面代码是一个使用Java和Redisson实现的三级缓存服务,主要功能包括:1.缓存结构:本地缓存:使

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF