当前位置: 首页 > article >正文

使用sqoop操作HDFS与MySQL之间的数据互传

一,数据从HDFS中导出至MySQL中

1)开启Hadoop、mysql进程

start-all.sh

/etc/init.d/mysqld start

/etc/init.d/mysqld status

2)将学生数据stu_data.csv传到HDFS的/local_student目录下

在hdfs中创建目录
hdfs dfs -mkdir /local_student
上传自定义的本地数据
hdfs dfs -put /root/lab/data/stu_data.csv /local_student/

注意:若出现以下错误:mkdir: Cannot create directory /student. Name node is in safe mode.

解决方案:hadoop dfsadmin -safemode leave

3)运行MySQL,创建数据库student_data,创建hdfs_stu_score_mysql表。

        进入MySQL:mysql -uroot -p123456(-u表示用户名,-pbiaosh)

create database student_data;

show databases;

use student_data;

create table hdfs_stu_score_mysql(

id int not null primary key,

name varchar(255),

age int,

score int);

退出客户端:

exit;

4)通过Sqoop,将HDFS上的数据导出到MySQL的hdfs_stu_score_mysql表中,具体命令如下:

sqoop export \

--connect jdbc:mysql://127.0.0.1:3306/student_data \

--username root \

--password 123456 \

--table hdfs_stu_score_mysql \
//--m 1 表示reduce数量定义为1个
--m 1 \

--export-dir /local_student/ \
//文件以制表符为分割符
--input-fields-terminated-by '\t' \

--columns="id,name,age,score"

5)MySQL中查询hdfs_stu_score_mysql表中数据

use student_data;

select * from hdfs_stu_score_mysql;

二,数据从MySQL中导入至HDFS中

1)在MySQL中筛选分数在85分(包括85分)以上的学生信息

# 进入MySQL客户端

mysql -uroot -p123456

use student_data;

# MySQL中建表

create table mysql_stu_top(

id int not null primary key,

name varchar(255),

age int,

score int

);

# 插入数据到mysql_stu_top表中:

insert into mysql_stu_top select * from hdfs_stu_score_mysql where score>=85;

# 查看结果

select * from mysql_stu_top;

# 退出客户端:

exit;

2)将MySQL中的mysql_stu_top表中数据导入到HDFS

sqoop import \

--connect jdbc:mysql://127.0.0.1:3306/student_data \

--username root \

--password 123456 \

--table mysql_stu_top \

--m 1 \

--target-dir /student/mysql_stu_top_hdfs

3)查看导入至HDFS中的数据

hdfs dfs -cat /student/mysql_stu_top_hdfs/part-m-00000


http://www.kler.cn/news/160106.html

相关文章:

  • Hello World
  • redis中使用pipeline
  • Qt Rsa 加解密方法使用(pkcs1, pkcs8, 以及文件存储和内存存储密钥)
  • 对于多台232modbus仪表低成本通讯的modbus转profinet网关
  • 微服务开发:断路器详解
  • 卡码网语言基础课 | 20. 排队取奶茶
  • Vue的methods中定时器的变量报错问题
  • 十年JK无人知!一朝泳衣天下识
  • 【数据结构】——二叉树特点
  • 区块链创新应用场景不断拓展,实现去中心化
  • 前端三大MV*模式:MVC、mvvm、mvp模式介绍
  • 数据库的设计规范
  • Element-UI 动态控制输入组件类型,定义代码组件、前端模板
  • 02数仓平台Zookeeper
  • prime靶机打靶记录
  • 数字化转型:利用软件电商平台与私有化软件提升竞争力
  • C++ 共享内存ShellCode跨进程传输
  • 54.多级缓存
  • 【PyTorch】数据集
  • 实战oj题——设计循环队列
  • 【Qt之QSqlRelationalTableModel】描述及使用
  • 【微信小程序】保存多张图片到本地相册 wx.saveImageToPhotosAlbum
  • 分支和循环
  • 目标检测——R-CNN算法解读
  • web理论测试
  • Insomnia -- 非常nice的开源 API 调试工具
  • Camunda 7.x 系列【57】流程设计器
  • Axios详解及运用案例
  • 详细学习PyQt5中的多线程
  • ubuntu下QT搭建Android开发环境