使用Sqoop能够极大简化MySQL数据迁移至Hive之流程,并降低Hadoop处理分析任务时的难度。

先决条件:安装并运行有Sqoop与Hive的Hadoop环境。为了加快处理速度,我们还将使用Cloudera Quickstart VM(要求至少4 GB内存),不过大家也可以使用Hortonworks Data Platform(至少要求8 GB内存)。由于我的笔记本电脑只有8 GB内存,因此我在这里使用CLoudera VM镜像。

如果大家需要使用Virtualbox运行Cloudera/HDP VM,则可以轻松使用其它多种Hadoop生态系统预装软件包(包括MySQL、Oozie、Hadoop、Hive、Zookeeper、Storm、Kafka以及Spark等等)。

在MySQL中创建表

在Cloudera VM中,打开命令界面并确保MySQL已经安装完毕。

  1. shell> mysql --version
  2. mysql  Ver 14.14 Distrib 5.1.66, for redhat-linux-gnu (x86_64) using readline 5.

示例当中自然要使用自己的数据库,因此使用以下命令在MySQL中创建一套数据库:

  1. mysql> create database sqoop;

接下来:

  1. mysql> use sqoop;
  2. mysql> create table customer(id varchar(3), name varchar(20), age varchar(3), salary integer(10));
  3. Query OK, 0 rows affected (0.09 sec)
  4. mysql> desc customer;
  5. +--------+-------------+------+-----+---------+-------+
  6. | Field  | Type        | Null | Key | Default | Extra |
  7. +--------+-------------+------+-----+---------+-------+
  8. | id     | varchar(3)  | YES  |     | NULL    |       |
  9. | name   | varchar(20) | YES  |     | NULL    |       |
  10. | age    | varchar(3)  | YES  |     | NULL    |       |
  11. | salary | int(10)     | YES  |     | NULL    |       |
  12. +--------+-------------+------+-----+---------+-------+
  1. mysql> select * from customer;
  2. +------+--------+------+--------+
  3. | id   | name   | age  | salary |
  4. +------+--------+------+--------+
  5. | 1    | John   | 30   |  80000 |
  6. | 2    | Kevin  | 33   |  84000 |
  7. | 3    | Mark   | 28   |  90000 |
  8. | 4    | Jenna  | 34   |  93000 |
  9. | 5    | Robert | 32   | 100000 |
  10. | 6    | Zoya   | 40   |  60000 |
  11. | 7    | Sam    | 37   |  75000 |
  12. | 8    | George | 31   |  67000 |
  13. | 9    | Peter  | 23   |  70000 |
  14. | 19   | Alex   | 26   |  74000 |
  15. +------+--------+------+-----

开始Sqoop之旅

如大家所见,其中customer表中并不包含主键。我在该表中并未添加多少记录。默认情况下,Sqoop能够识别出表中的主键列(如果有的话),并将其作为划分列。该划分列的低值与高值检索自该数据库,而映射任务则指向符合区间要求的均匀部分。

如果主键并未均匀分布在该区间当中,那么任务将出现不平衡状况。这时,大家应当明确选定一个与--split-by参数不同的列,例如--split-by id。

由于我们希望将此表直接导入至Hive中,因此需要在Sqoop命令中添加–hive-import:

  1. sqoop import --connect jdbc:mysql://localhost:3306/sqoop
  2. --username root
  3. -P
  4. --split-by id
  5. --columns id,name
  6. --table customer
  7. --target-dir /user/cloudera/ingest/raw/customers
  8. --fields-terminated-by ","
  9. --hive-import
  10. --create-hive-table
  11. --hive-table sqoop_workspace.customers

下面来看Sqoop命令各选项的具体作用:

connect – 提供jdbc字符串

username – 数据库用户名

-P – 将在控制台中询问密码。大家也可以使用-passwaord,但并不推荐这种作法,因为其会显示在任务执行日志中并可能导致问题。解决办法之一在于将数据库密码存储在HDFS中的文件内,并将其向运行时交付。

  • table – 告知计算机我们希望导入哪个MySQL表。在这里,表名称为customer。
  • split-by – 指定划分列。在这里我们指定id列。
  • target-dir – HDFS目标目录。
  • fields-terminated-by – 我已经指定了逗号作为分隔值(默认情况下,导入HDFS的数据以逗号作为分隔值)。
  • hive-import – 将表导入Hive(如果不加设置,则使用Hive的默认分隔符)。
  • create-hive-table – 检查如果已经存在一个Hive表,任务设置是否会因此失败。
  • hive-table – 指定.。本示例中为sqoop_workspace.customers,其中sqoop_workspace为数据库名称,而customers则为表名称。

如下所示,Sqoop为一项map-reduce任务。请注意,这里我使用-P作为密码选项。除了这种方式,我们也可以使用-password实现参数化,并从文件中读取密码内容。

  1. sqoop import --connect jdbc:mysql://localhost:3306/sqoop --username root -P --split-by id --columns id,name --table customer  --target-dir /user/cloudera/ingest/raw/customers --fields-terminated-by "," --hive-import --create-hive-table --hive-table sqoop_workspace.customers
  2. Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
  3. Please set $ACCUMULO_HOME to the root of your Accumulo installation.
  4. 16/03/01 12:59:44 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.5.0
  5. Enter password:
  6. 16/03/01 12:59:54 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
  7. 16/03/01 12:59:54 INFO tool.CodeGenTool: Beginning code generation
  8. 16/03/01 12:59:55 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `customer` AS t LIMIT 1
  9. 16/03/01 12:59:56 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `customer` AS t LIMIT 1
  10. 16/03/01 12:59:56 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /usr/lib/hadoop-mapreduce
  11. Note: /tmp/sqoop-cloudera/compile/6471c43b5c867834458d3bf5a67eade2/customer.java uses or overrides a deprecated API.
  12. Note: Recompile with -Xlint:deprecation for details.
  13. 16/03/01 13:00:01 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-cloudera/compile/6471c43b5c867834458d3bf5a67eade2/customer.jar
  14. 16/03/01 13:00:01 WARN manager.MySQLManager: It looks like you are importing from mysql.
  15. 16/03/01 13:00:01 WARN manager.MySQLManager: This transfer can be faster! Use the --direct
  16. 16/03/01 13:00:01 WARN manager.MySQLManager: option to exercise a MySQL-specific fast path.
  17. 16/03/01 13:00:01 INFO manager.MySQLManager: Setting zero DATETIME behavior to convertToNull (mysql)
  18. 16/03/01 13:00:01 INFO mapreduce.ImportJobBase: Beginning import of customer
  19. 16/03/01 13:00:01 INFO Configuration.deprecation: mapred.job.tracker is deprecated. Instead, use mapreduce.jobtracker.address
  20. 16/03/01 13:00:02 INFO Configuration.deprecation: mapred.jar is deprecated. Instead, use mapreduce.job.jar
  21. 16/03/01 13:00:04 INFO Configuration.deprecation: mapred.map.tasks is deprecated. Instead, use mapreduce.job.maps
  22. 16/03/01 13:00:05 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
  23. 16/03/01 13:00:11 INFO db.DBInputFormat: Using read commited transaction isolation
  24. 16/03/01 13:00:11 INFO db.DataDrivenDBInputFormat: BoundingValsQuery: SELECT MIN(`id`), MAX(`id`) FROM `customer`
  25. 16/03/01 13:00:11 WARN db.TextSplitter: Generating splits for a textual index column.
  26. 16/03/01 13:00:11 WARN db.TextSplitter: If your database sorts in a case-insensitive order, this may result in a partial import or duplicate records.
  27. 16/03/01 13:00:11 WARN db.TextSplitter: You are strongly encouraged to choose an integral split column.
  28. 16/03/01 13:00:11 INFO mapreduce.JobSubmitter: number of splits:4
  29. 16/03/01 13:00:12 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1456782715090_0004
  30. 16/03/01 13:00:13 INFO impl.YarnClientImpl: Submitted application application_1456782715090_0004
  31. 16/03/01 13:00:13 INFO mapreduce.Job: The url to track the job: http://quickstart.cloudera:8088/proxy/application_1456782715090_0004/
  32. 16/03/01 13:00:13 INFO mapreduce.Job: Running job: job_1456782715090_0004
  33. 16/03/01 13:00:47 INFO mapreduce.Job: Job job_1456782715090_0004 running in uber mode : false
  34. 16/03/01 13:00:48 INFO mapreduce.Job:  map 0% reduce 0%
  35. 16/03/01 13:01:43 INFO mapreduce.Job:  map 25% reduce 0%
  36. 16/03/01 13:01:46 INFO mapreduce.Job:  map 50% reduce 0%
  37. 16/03/01 13:01:48 INFO mapreduce.Job:  map 100% reduce 0%
  38. 16/03/01 13:01:48 INFO mapreduce.Job: Job job_1456782715090_0004 completed successfully
  39. 16/03/01 13:01:48 INFO mapreduce.Job: Counters: 30
  40. File System Counters
  41. FILE: Number of bytes read=0
  42. FILE: Number of bytes written=548096
  43. FILE: Number of read operations=0
  44. FILE: Number of large read operations=0
  45. FILE: Number of write operations=0
  46. HDFS: Number of bytes read=409
  47. HDFS: Number of bytes written=77
  48. HDFS: Number of read operations=16
  49. HDFS: Number of large read operations=0
  50. HDFS: Number of write operations=8
  51. Job Counters
  52. Launched map tasks=4
  53. Other local map tasks=5
  54. Total time spent by all maps in occupied slots (ms)=216810
  55. Total time spent by all reduces in occupied slots (ms)=0
  56. Total time spent by all map tasks (ms)=216810
  57. Total vcore-seconds taken by all map tasks=216810
  58. Total megabyte-seconds taken by all map tasks=222013440
  59. Map-Reduce Framework
  60. Map input records=10
  61. Map output records=10
  62. Input split bytes=409
  63. Spilled Records=0
  64. Failed Shuffles=0
  65. Merged Map outputs=0
  66. GC time elapsed (ms)=2400
  67. CPU time spent (ms)=5200
  68. Physical memory (bytes) snapshot=418557952
  69. Virtual memory (bytes) snapshot=6027804672
  70. Total committed heap usage (bytes)=243007488
  71. File Input Format Counters
  72. Bytes Read=0
  73. File Output Format Counters
  74. Bytes Written=77
  75. 16/03/01 13:01:48 INFO mapreduce.ImportJobBase: Transferred 77 bytes in 104.1093 seconds (0.7396 bytes/sec)
  76. 16/03/01 13:01:48 INFO mapreduce.ImportJobBase: Retrieved 10 records.
  77. 16/03/01 13:01:49 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `customer` AS t LIMIT 1
  78. 16/03/01 13:01:49 INFO hive.HiveImport: Loading uploaded data into Hive
  79. Logging initialized using configuration in jar:file:/usr/jars/hive-common-1.1.0-cdh5.5.0.jar!/hive-log4j.properties
  80. OK
  81. Time taken: 2.163 seconds
  82. Loading data to table sqoop_workspace.customers
  83. chgrp: changing ownership of 'hdfs://quickstart.cloudera:8020/user/hive/warehouse/sqoop_workspace.db/customers/part-m-00000': User does not belong to supergroup
  84. chgrp: changing ownership of 'hdfs://quickstart.cloudera:8020/user/hive/warehouse/sqoop_workspace.db/customers/part-m-00001': User does not belong to supergroup
  85. chgrp: changing ownership of 'hdfs://quickstart.cloudera:8020/user/hive/warehouse/sqoop_workspace.db/customers/part-m-00002': User does not belong to supergroup
  86. chgrp: changing ownership of 'hdfs://quickstart.cloudera:8020/user/hive/warehouse/sqoop_workspace.db/customers/part-m-00003': User does not belong to supergroup
  87. Table sqoop_workspace.customers stats: [numFiles=4, totalSize=77]
  88. OK
  89. Time taken: 1.399 seconds

最后,让我们验证Hive中的输出结果:

  1. hive> show databases;
  2. OK
  3. default
  4. sqoop_workspace
  5. Time taken: 0.034 seconds, Fetched: 2 row(s)
  6. hive> use sqoop_workspace;
  7. OK
  8. Time taken: 0.063 seconds
  9. hive> show tables;
  10. OK
  11. customers
  12. Time taken: 0.036 seconds, Fetched: 1 row(s)
  13. hive> show create table customers;
  14. OK
  15. CREATE TABLE `customers`(
  16. `id` string,
  17. `name` string)
  18. COMMENT 'Imported by sqoop on 2016/03/01 13:01:49'
  19. ROW FORMAT DELIMITED
  20. FIELDS TERMINATED BY ','
  21. LINES TERMINATED BY '\n'
  22. STORED AS INPUTFORMAT
  23. 'org.apache.hadoop.mapred.TextInputFormat'
  24. OUTPUTFORMAT
  25. 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
  26. LOCATION
  27. 'hdfs://quickstart.cloudera:8020/user/hive/warehouse/sqoop_workspace.db/customers'
  28. TBLPROPERTIES (
  29. 'COLUMN_STATS_ACCURATE'='true',
  30. 'numFiles'='4',
  31. 'totalSize'='77',
  32. 'transient_lastDdlTime'='1456866115')
  33. Time taken: 0.26 seconds, Fetched: 18 row(s)

hive> select * from customers;

OK

1 John

2 Kevin

19 Alex

3 Mark

4 Jenna

5 Robert

6 Zoya

7 Sam

8 George

9 Peter

Time taken: 1.123 seconds, Fetched: 10 row(s).

到此完成!从MySQL到Hive,数据迁移工作就是这么简单。

从MySQL到Hive,数据迁移就这么简单的更多相关文章

  1. django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x97\\xAE\\xE9\\xA2\\x98' for column 'name' at row 5")

    django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x ...

  2. 分布式计算(二)使用Sqoop实现MySQL与HDFS数据迁移

    近期接触了一个需求,业务背景是需要将关系型数据库的数据传输至HDFS进行计算,计算完成后再将计算结果传输回关系型数据库.听到这个背景,脑海中就蹦出了Sqoop迁移工具,可以非常完美的支持上述场景. 当 ...

  3. 大数据平台Hive数据迁移至阿里云ODPS平台流程与问题记录

    一.背景介绍 最近几天,接到公司的一个将当前大数据平台数据全部迁移到阿里云ODPS平台上的任务.而申请的这个ODPS平台是属于政务内网的,因考虑到安全问题当前的大数据平台与阿里云ODPS的网络是不通的 ...

  4. Mongodb到mysql数据库的数据迁移(Java,Windows)

    运行环境为windows 测试过260万的数据表,迁移大概要10分钟左右,当然肯定和网络,字段大小什么的有关系. 遇到的坑和注意点都用紫色标记了(对,就是我大乃团的高冷紫--Nogizaka 46) ...

  5. mysql搭建及数据迁移教程

    1.如果jumbo不存在,先安装jumbo 参考  http://hetu.baidu.com/api/tool/show?toolId=174: bash -c "$( curl  htt ...

  6. sqoop用法之mysql与hive数据导入导出

    目录 一. Sqoop介绍 二. Mysql 数据导入到 Hive 三. Hive数据导入到Mysql 四. mysql数据增量导入hive 1. 基于递增列Append导入 1). 创建hive表 ...

  7. Mysql或者Hive数据行变成列

    对于mysql /  hive 再进行统计的时候假设须要行变成列,能够使用函数 CASE 字段a WHEN 值b THEN c [WHEN d THEN e]* [ELSE f] END 当字段a=值 ...

  8. CentOS7中MySQL跨机器数据迁移

    1.概况 在CentOS7环境下,使用命令方式将MySQL数据从源端主机迁移到目标端主机上. 2.迁移全部数据库 1)源端备份: [root@hadoop102 /]# mysqldump -u ro ...

  9. Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'

    问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...

随机推荐

  1. Vue中拆分视图层代码的5点建议

    目录 一.框架的定位 二. Vue开发中的script拆分优化 1.组件划分 2.剥离业务逻辑代码 3. 剥离数据转换代码 4. 善用computed和filters处理数据展示 5. 使用direc ...

  2. JQGrid之文件上传

    文件/图片上传功能,简单总结如下 1.引入ajaxfileupload.js 注意:该文件需要在引入Jquery之后引入 下载链接:https://i.cnblogs.com/Files.aspx 2 ...

  3. php Basic HTTP与Digest HTTP 应用

    Basic HTTP 认证范例 <?php //Basic HTTP 认证 if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authen ...

  4. python学习之并发编程(理论部分)

    第一章 操作系统 管理控制协调计算机中硬件与软件的关系. 操作系统的作用? 第一个作用: 将一些对硬件操作的复杂丑陋的接口,变成简单美丽的接口. open函数. 第二个作用: 多个进程抢占一个(CPU ...

  5. [HEOI2013]SAO(树上dp,计数)

    [HEOI2013]SAO (这写了一个晚上QAQ,可能是我太蠢了吧.) 题目说只有\(n-1\)条边,然而每个点又相互联系.说明它的结构是一个类似树的结构,但是是有向边连接的,题目问的是方案个数,那 ...

  6. Hive 系列(六)—— Hive 视图和索引

    一.视图 1.1 简介 Hive 中的视图和 RDBMS 中视图的概念一致,都是一组数据的逻辑表示,本质上就是一条 SELECT 语句的结果集.视图是纯粹的逻辑对象,没有关联的存储 (Hive 3.0 ...

  7. Windos 上逆天又好用的软件有哪些?

    谷歌浏览器 Chrome 浏览器是大名鼎鼎的科技公司谷歌开发的一款浏览器,国内的360浏览器等大多都是基于谷歌开源出的浏览器内核,然后给他穿了一层360的衣服.至于性能和启动速度上来讲,我个人觉得Ch ...

  8. C#高级语法之泛型、泛型约束,类型安全、逆变和协变(思想原理)

    一.为什么使用泛型? 泛型其实就是一个不确定的类型,可以用在类和方法上,泛型在声明期间没有明确的定义类型,编译完成之后会生成一个占位符,只有在调用者调用时,传入指定的类型,才会用确切的类型将占位符替换 ...

  9. SpingBoot:整合Elasticsearch7.2.0

    Spring boot 2.1.X整合Elasticsearch最新版的一处问题 新版本的Spring boot 2的spring-boot-starter-data-elasticsearch中支持 ...

  10. java多线程----悲观锁与乐观锁

    java多线程中悲观锁与乐观锁思想 一.悲观锁 总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁(共享资源每次只给一个线 ...