自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從MySQL到Hive,數(shù)據(jù)遷移就這么簡(jiǎn)單

譯文
大數(shù)據(jù)
從存儲(chǔ)基礎(chǔ)設(shè)施的一個(gè)位置移動(dòng)數(shù)據(jù)到另一個(gè)位置是個(gè)艱難的過程。至少,過去是這樣。而現(xiàn)在,在合適的工具和基礎(chǔ)設(shè)施條件下,傳統(tǒng)的數(shù)據(jù)遷移過程中涉及到的許多困難點(diǎn)都可以消除。使用Sqoop能夠極大簡(jiǎn)化MySQL數(shù)據(jù)遷移至Hive之流程,并降低Hadoop處理分析任務(wù)時(shí)的難度。

使用Sqoop能夠極大簡(jiǎn)化MySQL數(shù)據(jù)遷移至Hive之流程,并降低Hadoop處理分析任務(wù)時(shí)的難度。

 

[[166114]]

先決條件:安裝并運(yùn)行有Sqoop與Hive的Hadoop環(huán)境。為了加快處理速度,我們還將使用Cloudera Quickstart VM(要求至少4 GB內(nèi)存),不過大家也可以使用Hortonworks Data Platform(至少要求8 GB內(nèi)存)。由于我的筆記本電腦只有8 GB內(nèi)存,因此我在這里使用CLoudera VM鏡像。

如果大家需要使用Virtualbox運(yùn)行Cloudera/HDP VM,則可以輕松使用其它多種Hadoop生態(tài)系統(tǒng)預(yù)裝軟件包(包括MySQL、Oozie、Hadoop、Hive、Zookeeper、Storm、Kafka以及Spark等等)。

在MySQL中創(chuàng)建表

在Cloudera VM中,打開命令界面并確保MySQL已經(jīng)安裝完畢。

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

示例當(dāng)中自然要使用自己的數(shù)據(jù)庫(kù),因此使用以下命令在MySQL中創(chuàng)建一套數(shù)據(jù)庫(kù):

  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表中并不包含主鍵。我在該表中并未添加多少記錄。默認(rèn)情況下,Sqoop能夠識(shí)別出表中的主鍵列(如果有的話),并將其作為劃分列。該劃分列的低值與高值檢索自該數(shù)據(jù)庫(kù),而映射任務(wù)則指向符合區(qū)間要求的均勻部分。

如果主鍵并未均勻分布在該區(qū)間當(dāng)中,那么任務(wù)將出現(xiàn)不平衡狀況。這時(shí),大家應(yīng)當(dāng)明確選定一個(gè)與--split-by參數(shù)不同的列,例如--split-by id。

由于我們希望將此表直接導(dǎo)入至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命令各選項(xiàng)的具體作用:

connect – 提供jdbc字符串

username – 數(shù)據(jù)庫(kù)用戶名

-P – 將在控制臺(tái)中詢問密碼。大家也可以使用-passwaord,但并不推薦這種作法,因?yàn)槠鋾?huì)顯示在任務(wù)執(zhí)行日志中并可能導(dǎo)致問題。解決辦法之一在于將數(shù)據(jù)庫(kù)密碼存儲(chǔ)在HDFS中的文件內(nèi),并將其向運(yùn)行時(shí)交付。

  • table – 告知計(jì)算機(jī)我們希望導(dǎo)入哪個(gè)MySQL表。在這里,表名稱為customer。
  • split-by – 指定劃分列。在這里我們指定id列。
  • target-dir – HDFS目標(biāo)目錄。
  • fields-terminated-by – 我已經(jīng)指定了逗號(hào)作為分隔值(默認(rèn)情況下,導(dǎo)入HDFS的數(shù)據(jù)以逗號(hào)作為分隔值)。
  • hive-import – 將表導(dǎo)入Hive(如果不加設(shè)置,則使用Hive的默認(rèn)分隔符)。
  • create-hive-table – 檢查如果已經(jīng)存在一個(gè)Hive表,任務(wù)設(shè)置是否會(huì)因此失敗。
  • hive-table – 指定.。本示例中為sqoop_workspace.customers,其中sqoop_workspace為數(shù)據(jù)庫(kù)名稱,而customers則為表名稱。

如下所示,Sqoop為一項(xiàng)map-reduce任務(wù)。請(qǐng)注意,這里我使用-P作為密碼選項(xiàng)。除了這種方式,我們也可以使用-password實(shí)現(xiàn)參數(shù)化,并從文件中讀取密碼內(nèi)容。

  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 

***,讓我們驗(yàn)證Hive中的輸出結(jié)果:

  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,數(shù)據(jù)遷移工作就是這么簡(jiǎn)單。

責(zé)任編輯:Ophira 來源: 51CTO.com
相關(guān)推薦

2015-06-30 12:53:40

秒殺應(yīng)用MySQL數(shù)據(jù)庫(kù)優(yōu)化

2025-02-12 08:21:55

OllamaChatboxDeepSeek

2021-11-19 11:16:29

Git命令Linux

2020-11-27 10:34:01

HTTPHTTPS模型

2014-04-02 10:20:20

銳捷網(wǎng)絡(luò)云課堂

2010-04-22 14:38:24

培訓(xùn)

2015-05-07 10:10:06

云應(yīng)用開發(fā)開發(fā)者云平臺(tái)

2015-12-15 16:54:00

戴爾云計(jì)算

2014-12-16 10:55:06

硅谷

2012-11-14 16:57:37

手機(jī)刷機(jī)

2017-07-13 13:13:49

AndroidAPK反編譯

2019-04-23 10:06:16

微軟Windows 10系統(tǒng)更新

2022-02-10 14:24:28

LinuxWindows文件

2014-03-29 22:42:00

微信公眾平臺(tái)開發(fā)C#

2015-08-27 09:00:41

產(chǎn)品用戶體驗(yàn)設(shè)計(jì)設(shè)計(jì)

2018-05-31 14:16:47

SQL ServerMySQL數(shù)據(jù)遷移

2015-07-06 14:59:49

技術(shù)周刊

2016-03-03 11:36:09

浪潮

2019-12-17 16:04:25

微軟

2015-01-05 09:35:54

云計(jì)算應(yīng)用程序開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)