Sqoop_mysql,hive,hdfs导入导出操作
前言: 搭建环境,这里使用cdh版hadoop+hive+sqoop+mysql
下载 hadoop-2.5.0-cdh5.3.6.tar.gz
hive-0.13.1-cdh5.3.6.tar.gz
sqoop-1.4.5-cdh5.3.6.tar.gz
配置 Hadoop
*.env(3个)--jdk_Path
core-sit.xml
fs.defaultFS
hadoop.tmp.dir
hdfs-site.xml
dfs.replication
mapred-site.xml
mapreduce.framework.name--yarn
mapreduce.jobhistory.address # 10020
mapreduce.jobhistory.webapp.address # 19888
yarn-site.xml
yarn.resourcemanager.hostname
yarn.nodemanager.aux-services--mapreduce_shuffle
yarn.log-aggregation-enable--true
yarn.log-aggregation.retain-seconds--108600
slave
主机地址
PS: 格式化namenode,启动hdfs与yarn
$ bin/hdfs dfs -mkdir /tmp
$ bin/hdfs dfs -mkdir -p /user/hive/warehouse
$ bin/hdfs dfs -chmod g+w /tmp
$ bin/hdfs dfs -chmod g+w /user/hive/warehouse
配置Hive
hive-env.sh
HADOOP_HOME=/opt/cdh-5.6.3/hadoop-2.5.0-cdh5.3.6
export HIVE_CONF_DIR=/opt/cdh-5.6.3/hive-0.13.1-cdh5.3.6/conf
hive-log4j.properties
hive.log.threshold=ALL
hive.root.logger=INFO,DRFA
hive.log.dir=/opt/cdh-5.6.3/hive-0.13.1-cdh5.3.6/logs
hive.log.file=hive.log
hive-site.xml # 事先将mysql部署好
javax.jdo.option.ConnectionURL--jdbc:mysql://hadoop09-linux-01.ibeifeng.com:3306/chd_metastore?createDatabaseIfNotExist=true
javax.jdo.option.ConnectionDriverName--com.mysql.jdbc.Driver
javax.jdo.option.ConnectionUserNam
javax.jdo.option.ConnectionPassword
hive.cli.print.header--true
hive.cli.print.current.db--true
hive.fetch.task.conversion--more
PS: hive目录下
$ mkdir logs
将准备好的mysql.jar包放入lib
启动 $ bin/hive
配置Sqoop
sqoop-env.sh
export HADOOP_COMMON_HOME=/opt/cdh-5.6.3/hadoop-2.5.0-cdh5.3.6
export HADOOP_MAPRED_HOME=/opt/cdh-5.6.3/hadoop-2.5.0-cdh5.3.6
export HIVE_HOME=/opt/cdh-5.6.3/hive-0.13.1-cdh5.3.6
将准备好的mysql.jar包放入lib
一、准备数据
# 在我的mysql下创建数据库和表,并插入几条数据
mysql> create database if not exists student default character set utf8 collate utf8_general_ci;
mysql> use student;
mysql> create table if not exists stu_info( id int(10) primary key not null auto_increment, name varchar(20) not null) default character set utf8 collate utf8_general_ci;
mysql> insert into stu_info(name) values("李建");
mysql> insert into stu_info(name) values("张明");
mysql> insert into stu_info(name) values("赵兴");
mysql> insert into stu_info(name) values("陈琦");
mysql> insert into stu_info(name) values("刘铭");
mysql> select id,name from stu_info;
+----+--------+
| id | name |
+----+--------+
| 1 | 李建 |
| 2 | 张明 |
| 3 | 赵兴 |
| 4 | 陈琦 |
| 5 | 刘铭 |
+----+--------+
5 rows in set (0.00 sec)
二、使用sqoop将mysql中的这张表导入到hdfs上
bin/sqoop import \
--connect \
jdbc:mysql://10.0.0.108:3306/student \
--username root \
--password root \
--table stu_info \
--target-dir /student \
--num-mappers 1 \
--fields-terminated-by '\t'
三、使用sqoop将mysql中的这张表导入到hive
方式一、
1. 在hive中创建数据库和表
create database if not exists student;
create table if not exists stu_info(id int,name string) row format delimited fields terminated by '\t';
2. bin/sqoop import \
--connect jdbc:mysql://hadoop09-linux-01.ibeifeng.com:3306/student \
--username root --password root \
--table stu_info \
--delete-target-dir \
--target-dir /user/hive/warehouse/student.db/stu_info \
--hive-import \
--hive-database student \
--hive-table stu_info \
--hive-overwrite \
--num-mappers 1 \
--fields-terminated-by '\t'
方式二、
1. 使用sqoop create-hive-table,但必须创建出自定义数据库,否则目标路径将是元数据库
2. bin/sqoop create-hive-table 、
--connect jdbc:mysql://10.0.0.108:3306/student 、
--username root --password root \
--table stu_info \
--hive-table student.stu_info
3. bin/sqoop import --connect jdbc:mysql://10.0.0.108:3306/student \
--username root --password root \
--table stu_info \
--hive-import \
--hive-database student \
--hive-table stu_info \
--hive-overwrite \
--num-mappers 1 \
--fields-terminated-by '\t' \
--delete-target-dir \
--target-dir /user/hive/warehouse/student.db/stu_info
4. 在hive中查询会发现数据全部为NULL
但是从hdfs上查看却是正常的,确定hive无法解析数据,定位在分隔符问题
使用--fields-terminated-by '\001' 即可 # \001就是ctrl+A,hive默认分隔符,mysql默认分隔符为","
五、从hdfs或hive导出数据到mysql表
1. 在mysql上准备好数据库和表
2. 数据库我就直接使用student数据库
create table if not exists stu_info_export like stu_info;
3. 根据hdfs/hive表数据分隔符为主
bin/sqoop export \
--connect jdbc:mysql://10.0.0.108/student \
--username root --password root \
--table stu_info_export \
--export-dir /user/hive/warehouse/student.db/stu_info \
--num-mappers 1 \
--input-fields-terminated-by '\001'
六、sqoop --option-file
另外 企业级增量迁移数据使用 --option-file + shell脚本
-- $ sqoop import --connect jdbc:mysql://localhost/db --username foo --table TEST
-- $ sqoop --options-file /users/homer/work/import.txt --table TEST
注意:脚本格式开头直接导入导出命令然后一行一个属性,如:
-->import
--connect
jdbc:mysql://localhost/db
--username
foo
七、使用sqoop job
$ bin/sqoop job --delete <job_id>
$ bin/sqoop job --list
$ bin/sqoop job --show <job_id>
$ bin/sqoop job --exec <job_id>
$ bin/sqoop job --create job_id -- <job-info>
$ bin/sqoop job --create stu_info -- \
import \
--connect \
jdbc:mysql://hadoop09-linux-01.ibeifeng.com:3306/sqoop \
--username root \
--password root \
--table tohdfs \
--target-dir /sqoop \
--num-mappers 1 \
--fields-terminated-by '\t' \
--check-column id \
--incremental append \
--last-value 11
PS: 增量导入(与--delete-target-dir冲突)
--check-column id
--incremental append/lastmodified(时间戳的更改)
--last-value 11
另外:
--columns field1,field2,field3
--query <ql> # 需要加 $CONDITIONS,且不能和--table连用
--where <where xxx> # 无需加$CONDITIONS
Sqoop_mysql,hive,hdfs导入导出操作的更多相关文章
- 从零自学Hadoop(16):Hive数据导入导出,集群数据迁移上
阅读目录 序 导入文件到Hive 将其他表的查询结果导入表 动态分区插入 将SQL语句的值插入到表中 模拟数据文件下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并 ...
- Hive数据导入导出的几种方式
一,Hive数据导入的几种方式 首先列出讲述下面几种导入方式的数据和hive表. 导入: 本地文件导入到Hive表: Hive表导入到Hive表; HDFS文件导入到Hive表; 创建表的过程中从其他 ...
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...
- Winform开发框架之通用数据导入导出操作的事务性操作完善
1.通用数据导入导出操作模块回顾 在我的Winfrom开发框架里面,有一个通用的导入模块,它在默默处理这把规范的Excel数据导入到不同的对象表里面,一直用它来快速完成数据导入的工作.很早在随笔< ...
- 循序渐进开发WinForm项目(5)--Excel数据的导入导出操作
随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...
- 利用sqoop将hive数据导入导出数据到mysql
一.导入导出数据库常用命令语句 1)列出mysql数据库中的所有数据库命令 # sqoop list-databases --connect jdbc:mysql://localhost:3306 ...
- VB中Excel 2010的导入导出操作
VB中Excel 2010的导入导出操作 编写人:左丘文 2015-4-11 近来这已是第二篇在讨论VB的相关问题,今天在这里,我想与大家一起分享一下在VB中如何从Excel中导入数据和导出数据到Ex ...
- 从零自学Hadoop(17):Hive数据导入导出,集群数据迁移下
阅读目录 序 将查询的结果写入文件系统 集群数据迁移一 集群数据迁移二 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephis ...
- Hive 实战(1)--hive数据导入/导出基础
前沿: Hive也采用类SQL的语法, 但其作为数据仓库, 与面向OLTP的传统关系型数据库(Mysql/Oracle)有着天然的差别. 它用于离线的数据计算分析, 而不追求高并发/低延时的应用场景. ...
随机推荐
- jackson对多态or多子类序列化的处理配置
[TOC] Jackson Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 多态类型的处理 jackson允许配置多态类型处理, ...
- tab_切换
记忆: 一.这里用到了jQuery遍历---filter()方法: filter() 方法将匹配元素集合缩减为匹配指定选择器的元素. 二.HTML DOM hash属性 hash 属性是一个可读可写的 ...
- minix3(一)安装以及编辑文件
作为一条通信狗,最近开始自学操作系统.听说用MINIX3学操作系统很好,就决定跟UCSB的课程试试. 首先在虚拟机上安装MINIX3. 开始用的VM Station,按照百度文库里安装minix3的教 ...
- user_jj两条记录改成一条
1.前台index控制器,用user_jj.*add找到,home_ddxx_pcz_cl() 2.前台index控制器,用user_jj.*add找到,tgbz_list_sd_cl(),tgbz_ ...
- PB之入门-itemchanged(long row,dwobject dwo,string data)
每天的总结都是必须,好记性不如烂笔头,好吧,一星期没做笔记了,最近忙上PB了,哎东学学西学学,最可怕的就是最后都半斤八两,吐槽一下关于PB的资源为何如此之少,今天记录的是关于itemchanged事件 ...
- 使用Eclipse将Web项目打Jar包方法
1.对下载.安装和运行Eclipse,就不再说了: 2.找到待打包项目: 3.右键,Export-->Export: 4.选择,Jar: 5.按如图操作: 6.完成后:
- JavaScript 入门 (1)
一. javascript的调用 JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中: <html> <hea ...
- javascript优化--11模式(设计模式)02
策略模式 在选择最佳策略以处理特定任务(上下文)的时候仍然保持相同的接口: //表单验证的例子 var data = { firs_name: "Super", last_name ...
- Dapper ORM 用法—Net下无敌的ORM(转)
假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载Dapper的优势:1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编译后 ...
- 解决js(ajax)提交后端的“ _xsrf' argument missing from POST” 的错误
首先先简述一下CSRF: CSRF是Cross Site Request Forgery的缩写(也缩写为XSRF),直译过来就是跨站请求伪造的意思,也就是在用户会话下对某个CGI做一些GET/POST ...