前言: 搭建环境,这里使用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导入导出操作的更多相关文章

  1. 从零自学Hadoop(16):Hive数据导入导出,集群数据迁移上

    阅读目录 序 导入文件到Hive 将其他表的查询结果导入表 动态分区插入 将SQL语句的值插入到表中 模拟数据文件下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并 ...

  2. Hive数据导入导出的几种方式

    一,Hive数据导入的几种方式 首先列出讲述下面几种导入方式的数据和hive表. 导入: 本地文件导入到Hive表: Hive表导入到Hive表; HDFS文件导入到Hive表; 创建表的过程中从其他 ...

  3. c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出

    c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...

  4. Winform开发框架之通用数据导入导出操作的事务性操作完善

    1.通用数据导入导出操作模块回顾 在我的Winfrom开发框架里面,有一个通用的导入模块,它在默默处理这把规范的Excel数据导入到不同的对象表里面,一直用它来快速完成数据导入的工作.很早在随笔< ...

  5. 循序渐进开发WinForm项目(5)--Excel数据的导入导出操作

    随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...

  6. 利用sqoop将hive数据导入导出数据到mysql

    一.导入导出数据库常用命令语句 1)列出mysql数据库中的所有数据库命令  #  sqoop list-databases --connect jdbc:mysql://localhost:3306 ...

  7. VB中Excel 2010的导入导出操作

    VB中Excel 2010的导入导出操作 编写人:左丘文 2015-4-11 近来这已是第二篇在讨论VB的相关问题,今天在这里,我想与大家一起分享一下在VB中如何从Excel中导入数据和导出数据到Ex ...

  8. 从零自学Hadoop(17):Hive数据导入导出,集群数据迁移下

    阅读目录 序 将查询的结果写入文件系统 集群数据迁移一 集群数据迁移二 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephis ...

  9. Hive 实战(1)--hive数据导入/导出基础

    前沿: Hive也采用类SQL的语法, 但其作为数据仓库, 与面向OLTP的传统关系型数据库(Mysql/Oracle)有着天然的差别. 它用于离线的数据计算分析, 而不追求高并发/低延时的应用场景. ...

随机推荐

  1. 第十五篇:在SOUI中消息通讯

    SOUI是一套基于Win32 SDK的窗口开发的一套DirectUI框架.在SOUI中除了有真窗口使用窗口消息通讯机制外,还有SOUI控件之间的通讯,及控件的事件处理等. 1.真窗口消息通讯 因此可以 ...

  2. 62个Android Studio小技巧合集

    1书签(Bookmarks) 描述:这是一个很有用的功能,让你可以在某处做个标记(书签),方便后面再跳转到此处. 调用:Menu → Navigate → Bookmarks 快捷键: 添加/移除书签 ...

  3. Linux学习笔记(22) Linux启动管理

    1. 系统运行级别 运行级别 含义 关机 1 单用户模式,可想象为windows的安全模式,主要用于系统修复 2 不完全的命令行模式,不含NFS服务 完全的命令行模式,就是标准字符界面 4 系统保留 ...

  4. YAML 技术研究

    YAML预研文档 YAML概要 YAML是"YAML Ain't a Markup Language"(YAML不是一种置标语言)的递归缩写,早先YAML的意思其实是:" ...

  5. css学习(1)-- 层叠样式表CSS的基础

    一.什么是CSS CSS是Cascading Style Sheets的简写,它除了可以轻松设置网页元素的显示位置和格式处,甚至还能产生滤镜,图像淡化,网页淡入淡出的渐变效果. 一个样式表由样式规则组 ...

  6. WPF,解决Listbox,按住ListboxItem向下拖出Listbox,横向滚动条跑到最后。

    类似这种样式的控件,.,在横向滚动条隐藏的情况下有这样的问题.(横向滚动条显示的时候也会,,目前不知道怎么解决.) 因为这个控件偏移是利用ListBox的ItemsPanelTemplate模版里的S ...

  7. jQuery下操作dropdownlist

    dropdownlist呈现到html中如下: <select id="ddlOrg" name="ddlOrg">     <option ...

  8. 后缀数组 UVA 11107 Life Forms

    题目链接 题意:训练指南P223 分析:二分长度,把所有字符串连成一个字符串,中间用不同的字符分隔(这是为了保证匹配长度始终在一个字符串内).height数组分段,vis数组标记哪些字符串被访问了,如 ...

  9. mac笔记

    chrome F12快捷键 option+command+i updatedb: http://www.jeffkit.info/2010/04/66/ /usr/libexec/locate.upd ...

  10. [杂谈] My Wikipedia

    // 此博文为迁移而来,写于2015年6月8日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w3de.html