Sqoop知識點整理

Sqoop

Sqoop是一款開源的工具,主要用於在HADOOP不傳統的數據庫(mysql、postgresql等)進行數據的傳遞,可以將一個關係型數據庫(例如:MySQL、Oracle、Postgres等)中的數據導進到Hadoop的HDFS中,也可以將HDFS的數據導進到關係型數據庫中。 Sqoop中一大亮點就是可以通過hadoop的mapreduce把數據從關係型數據庫中導入數據到HDFS。

版本選擇

CDH 5.3.x 版本,非常的穩定,好用 cdh-5.3.6,各個版本之間的依賴和兼容不同
* hadoop-2.5.0-cdh5.3.6.tar.gz * hive-0.13.1-cdh5.3.6.tar.gz * zookeeper-3.4.5-cdh5.3.6.tar.gz * sqoop-1.4.5-cdh5.3.6.tar.gz 下載地址 http://archive.cloudera.com/cdh5/cdh/5/

sqoop安裝

  
  
  
  1. tar -zxvf sqoop-1.4.5-cdh5.3.6.tar.gz -C /opt/cdh5.3.6

  2. #在sqoop下的conf目錄下修改sqoop-env.sh

  3. #Set path to where bin/hadoop is available

  4. export HADOOP_COMMON_HOME=/opt/cdh-5.3.6/hadoop-2.5.0-cdh5.3.6

  5. #Set path to where hadoop-*-core.jar is available

  6. export HADOOP_MAPRED_HOME=/opt/cdh-5.3.6/hadoop-2.5.0-cdh5.3.6

  7. #set the path to where bin/hbase is available

  8. #export HBASE_HOME=

  9. #Set the path to where bin/hive is available

  10. export HIVE_HOME=/opt/cdh-5.3.6/hive-0.13.1-cdh5.3.6

  11. #Set the path for where zookeper config dir is

  12. #export ZOOCFGDIR=

  13. #RDBMS以Mysql數據庫爲例講解,拷貝jdbc驅動包到$SQOOP_HOME/lib目錄下

  14. cp hive-0.13.1-cdh5.3.6/lib/mysql-connector-java-5.1.27-bin.jar  sqoop-1.4.5-cdh5.3.6/lib/

sqoop常用命令

  
  
  
  1. **************************************

  2. sqoop-import 注意點

  3. import 可能會用到的參數:

  4. Argument    Described

  5. --append    Append data to an existing dataset in HDFS

  6. --as-sequencefile    import序列化的文件

  7. --as-textfile    import plain文件 ,默認

  8. --columns <col,col,col…>    指定列import,逗號分隔,比如:--columns "id,name"

  9. --delete-target-dir    刪除存在的import目標目錄

  10. --direct    直連模式,速度更快(HBase不支持)

  11. --fetch-size <n>    一次從數據庫讀取 n 個實例,即n條數據

  12. -m,--num-mappers <n>    建立 n 個併發執行task import

  13. -e,--query <statement>    構建表達式<statement>執行

  14. --split-by <column-name>    根據column分隔實例

  15. --autoreset-to-one-mappe    如果沒有主鍵和split-by one mapper import split-by 和此選項不共存)

  16. --table <table-name>    指定表名import

  17. --target-dir <d>    HDFS destination dir

  18. --warehouse-dir <d>    HDFS parent for table destination

  19. --where <where clause>    指定where從句,如果有雙引號,注意轉義 \$CONDITIONS,不能用or,子查詢,join

  20. -z,--compress    開啓壓縮

  21. --null-string <null-string>    string列爲空指定爲此值

  22. --null-non-string <null-string>    string列爲空指定爲此值,-null這兩個參數are optional, 如果不設置,會指定爲"null"

  23. 列出mysql數據庫中的所有數據庫

  24. bin/sqoop list-databases --connect jdbc:mysql://hadoop.jianxin.com:3306 --username root --password missandlove

  25. **************************************

  26. **************************************

  27. 建立mysql

  28. CREATE TABLE my_user(

  29.  `id` int(4) NOT NULL AUTO_INCREMENT,

  30.  `account` varchar(255) DEFAULT NULL,

  31.  `passwd` varchar(255) DEFAULT NULL,

  32.  PRIMARY KEY (`id`)

  33. );

  34. 插入數據

  35. INSERT INTO `my_user` VALUES ('1', 'admin', 'admin');

  36. INSERT INTO `my_user` VALUES ('3', 'system', 'system');

  37. INSERT INTO `my_user` VALUES ('5', 'lee', 'lee');

  38. INSERT INTO `my_user` VALUES ('6', 'les', 'les');

  39. INSERT INTO `my_user` VALUES ('7', 'jianxin', 'jianxin');

  40. **************************************

  41. **************************************

  42. mysql導出數據

  43. bin/sqoop import \

  44. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  45. --username root \

  46. --password missandlove \

  47. --table my_user

  48. 如果不指定–target-dir,數據會存儲在HDFS中的對應用戶名目錄下

  49. **************************************

  50. **************************************

  51. 導出到指定文件目錄下

  52. sqoop 底層的實現就是MapReduce,import來說,僅僅運行Map Task

  53. bin/sqoop import \

  54. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  55. --username root \

  56. --password missandlove \

  57. --table my_user \

  58. --delete-target-dir \

  59. --target-dir /user/jianxin/sqoop/import_myuser \

  60. --num-mappers 3

  61. **************************************

  62. **************************************

  63. 數據導出儲存方式(數據存儲文件格式---( textfil parquet

  64. --as-textfile    Imports data as plain text (default)

  65. --as-parquetfile    Imports data to Parquet Files

  66. bin/sqoop import \

  67. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  68. --username root \

  69. --password missandlove \

  70. --table my_user \

  71. --target-dir /user/jianxin/sqoop/import_myuser_parquet \

  72. --delete-target-dir \

  73. --num-mappers 3 \

  74. --fields-terminated-by ',' \

  75. --as-parquetfile

  76. **************************************

  77. **************************************

  78. hive建立表

  79. drop table if exists default.hive_user_prq;

  80. create table default.hive_user_prq(

  81. id int,

  82. username string,

  83. password string

  84. )

  85. ROW FORMAT DELIMITED FIELDS TERMINATED  BY ','

  86. STORED AS parquet;

  87. 數據入表

  88. load data inpath '/user/jianxin/sqoop/import_myuser_parquet' into table default.hive_user_prq;

  89. **************************************

  90. **************************************

  91. 指定的列導入到數據庫

  92. bin/sqoop import \

  93. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  94. --username root \

  95. --password missandlove \

  96. --table my_user \

  97. --delete-target-dir \

  98. --target-dir /user/jianxin/sqoop/import_column \

  99. --as-parquetfile \

  100. --num-mappers 3 \

  101. --fields-terminated-by ',' \

  102. --column id,account \

  103. **************************************

  104. **************************************

  105. 初步清洗和過濾數據

  106. bin/sqoop import \

  107. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  108. --username root \

  109. --password missandlove \

  110. --table my_user \

  111. --delete-target-dir \

  112. --target-dir /user/jianxin/sqoop/import_query \

  113. --as-parquetfile \

  114. --num-mappers 3 \

  115. --fields-terminated-by ',';

  116. --query 'select id, account from my_user where $CONDITIONS'

  117. 使用query查詢導入必須指定條件$CONDITIONS,如果不指定將報錯。

  118. create table query_table(id int ,account string) ROW FORMAT DELIMITED FIELDS TERMINATED  BY ',' STORED AS parquet;

  119. load data inpath '/user/jianxin/sqoop/import_query' into table query_table;  

  120. bin/sqoop import \

  121. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  122. --username root \

  123. --password missandlove \

  124. --table my_user \

  125. --delete-target-dir \

  126. --target-dir /user/jianxin/sqoop/import_query \

  127. --as-parquetfile \

  128. --num-mappers 3 \

  129. --fields-terminated-by ',';

  130. --query 'select id, account from my_user where id>1'

  131. **************************************

  132. **************************************

  133. 數據壓縮

  134. bin/sqoop import \

  135. --connect jdbc:mysql://hadoop.jianxin.com/test \

  136. --username root \

  137. --password missandlove \

  138. --table my_user  \

  139. --target-dir  /user/jianxin/sqoop/imp_my_sannpy \

  140. -- delete-target-dir \

  141. --num-mappers 3 \

  142. --compress \

  143. -- compression-codec  org.apache.hadoop.io.compress.SnappyCodec \

  144. --fields-terminated-by ',' \

  145. --as-parquetfile

  146. **************************************

  147. **************************************

  148. 數據的增量導入

  149. sqoop

  150. Incremental import arguments:

  151. --check-column <column>        Source column to check for incremental change

  152. --incremental <import-type>    Define an incremental import of type 'append' or 'lastmodified'

  153. --last-value <value>         Last imported value in the incremental check column    

  154. check-column,用來指定一些列,這些列在導入時用來檢查做決定數據是否要被作爲增量數據,在一般關係型數據庫中,都存在類似Last_Mod_Date的字段或主鍵。注意:這些被檢查的列的類型不能是任意字符類型,例如CharVARCHAR…(即字符類型不能作爲增量標識字段)

  155. incremental,用來指定增量導入的模式(Mode),appendlastmodified

  156. last-value,指定上一次導入中檢查列指定字段最大值

  157. 1append,在導入的新數據ID值是連續時採用,對數據進行附加 加不加–last-value的區別在於:數據是否冗餘,如果不加,則會導入源表中的所有數據導致數據冗餘。

  158. 2lastmodified,在源表中有數據更新的時候使用,檢查列就必須是一個時間戳或日期類型的字段,更新完之後,last-value會被設置爲執行增量導入時的當前系統時間,當使用–incremental lastmodified模式進行導入且導入目錄已存在時,需要使用–merge-key或–append

  159. 導入>=last-value的值。--incremental lastmodified --check-column created --last-value '2012-02-01 11:0:00'

  160. 就是隻導入created '2012-02-01 11:0:00'更大的數據。

  161. bin/sqoop import \

  162. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  163. --username root \

  164. --password missandlove \

  165. --table my_user \

  166. --target-dir /user/jianxin/sqoop/increament \

  167. --num-mappers 1 \

  168. --incremental append \

  169. --check-column id \

  170. --last-value 0

  171. [root@hadoop hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -cat /user/jianxin/sqoop/increament/part-m-00000

  172. 1,admin,admin

  173. 3,system,system

  174. 5,lee,lee

  175. 6,les,les

  176. 7,jianxin,jianxin

  177. INSERT INTO `my_user` VALUES ('12', 'admin', 'admin');

  178. INSERT INTO `my_user` VALUES ('11', 'system', 'system');

  179. INSERT INTO `my_user` VALUES ('10', 'lee', 'lee');

  180. INSERT INTO `my_user` VALUES ('8', 'les', 'les');

  181. INSERT INTO `my_user` VALUES ('9', 'jianxin', 'jianxin');

  182. bin/sqoop import \

  183. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  184. --username root \

  185. --password missandlove \

  186. --table my_user \

  187. --target-dir /user/jianxin/sqoop/increament \

  188. --num-mappers 1 \

  189. --incremental append \

  190. --check-column id \

  191. --last-value 7

  192. [root@hadoop hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -cat /user/jianxin/sqoop/increament/part-m-00001

  193. 8,les,les

  194. 9,jianxin,jianxin

  195. 10,lee,lee

  196. 11,system,system

  197. 12,admin,admin

  198. create table customer(id int,name varchar(20),last_mod timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);

  199. insert into customer(id,name) values(1,'neil');

  200. insert into customer(id,name) values(2,'jack');

  201. insert into customer(id,name) values(3,'martin');

  202. insert into customer(id,name) values(4,'tony');

  203. insert into customer(id,name) values(5,'eric');

  204. mysql> select * from customer;

  205. +------+--------+---------------------+

  206. | id   | name   | last_mod            |

  207. +------+--------+---------------------+

  208. |    1 | neil   | 2018-04-26 14:00:08 |

  209. |    2 | jack   | 2018-04-26 14:00:08 |

  210. |    3 | martin | 2018-04-26 14:00:08 |

  211. |    4 | tony   | 2018-04-26 14:00:08 |

  212. |    5 | eric   | 2018-04-26 14:00:09 |

  213. +------+--------+---------------------+

  214. bin/sqoop import \

  215. --connect jdbc:mysql://hadoop.jianxin.com/test \

  216. --username root \

  217. --password missandlove \

  218. --table customer \

  219. --target-dir /user/jianxin/sqoop/increament_bytime \

  220. --num-mappers 1 \

  221. --check-column last_mod \

  222. --incremental lastmodified  \

  223. --last-value  0

  224. [root@hadoop hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -cat /user/jianxin/sqoop/increament_bytime/part-m-00000

  225. 1,neil,2018-04-26 14:00:08.0

  226. 2,jack,2018-04-26 14:00:08.0

  227. 3,martin,2018-04-26 14:00:08.0

  228. 4,tony,2018-04-26 14:00:08.0

  229. 5,eric,2018-04-26 14:00:09.0

  230. 使用lastmodified模式進行增量處理要指定增量數據是以append模式(附加)還是merge-key(合併)模式添加

  231. insert into customertest(id,name) values(6,'james')

  232. bin/sqoop import \

  233. --connect jdbc:mysql://hadoop.jianxin.com/test \

  234. --username root \

  235. --password missandlove \

  236. --table customer \

  237. --target-dir /user/jianxin/sqoop/increament_bytime \

  238. --num-mappers 1 \

  239. --check-column last_mod \

  240. --incremental lastmodified  \

  241. --last-value  '2018-04-26 14:00:09' \

  242. --merge-key id

  243. 這種方式不會新建立hdfs文件系統下的文件

  244. **************************************

  245. **************************************

  246. 直接導入

  247. bin/sqoop import \

  248. --connect jdbc:mysql://hadoop.jianxin.com:3306/test \

  249. --username root \

  250. --password missandlove \

  251. --table customer \

  252. --num-mappers 1 \

  253. --target-dir /user/jianxin/sqoop/derict \

  254. --delete-target-dir

  255. --direct

  256. **************************************

  257. **************************************

  258. 導出到數據庫

  259. 論據    描述

  260. --connect <jdbc-uri>    指定JDBC連接字符串

  261. --connection-manager <class-name>    指定要使用的連接管理器類

  262. --driver <class-name>    手動指定要使用的JDBC驅動程序類

  263. --hadoop-mapred-home <dir>    覆蓋$ HADOOP_MAPRED_HOME

  264. --help    打印使用說明

  265. --password-file    爲包含認證密碼的文件設置路徑

  266. -P    從控制檯讀取密碼

  267. --password <password>    設置驗證密碼

  268. --username <username>    設置驗證用戶名

  269. --verbose    在工作時打印更多信息

  270. --connection-param-file <filename>    提供連接參數的可選屬性文件

  271. --relaxed-isolation    將連接事務隔離設置爲未提交給映射器的讀取。

  272. --columns <col,col,col…>    要導出到表格的列

  273. --direct    使用直接導出快速路徑

  274. --export-dir <dir>    用於導出的HDFS源路徑

  275. -m,--num-mappers <n>    使用n個地圖任務並行導出

  276. --table <table-name>    要填充的表

  277. --call <stored-proc-name>    存儲過程調用

  278. --update-key <col-name>    錨點列用於更新。如果有多個列,請使用以逗號分隔的列列表。

  279. --update-mode <mode>    指定在數據庫中使用不匹配的鍵找到新行時如何執行更新。

  280. mode包含的 updateonly默認 值(默認)和 allowinsert

  281. --input-null-string <null-string>    字符串列被解釋爲空的字符串

  282. --input-null-non-string <null-string>    要對非字符串列解釋爲空的字符串

  283. --staging-table <staging-table-name>    數據在插入目標表之前將在其中展開的表格。

  284. --clear-staging-table    表示可以刪除登臺表中的任何數據。

  285. --batch    使用批處理模式執行基礎語句。

  286. touch /opt/datas/my_user_fromhive

  287. [root@hadoop datas]# vi my_user_fromhive

  288. 16,hiveuser,hiveuser

  289. 18,hivemiss,hivemiss

  290. [root@hadoop hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -mkdir -p /user/jianxin/hive_to_mysql

  291. [root@hadoop hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -put /opt/datas/my_user_fromhive  /user/jianxin/hive_to_mysql

  292. bin/sqoop export \

  293. --connect  jdbc:mysql://hadoop.jianxin.com:3306/test \

  294. --username root \

  295. --password missandlove \

  296. --table my_user \

  297. --export-dir /user/jianxin/hive_to_mysql \

  298. --num-mappers 1

  299. mysql> select * from my_user;

  300. +----+----------+----------+

  301. | id | account  | passwd   |

  302. +----+----------+----------+

  303. |  1 | admin    | admin    |

  304. |  3 | system   | system   |

  305. |  5 | lee      | lee      |

  306. |  6 | les      | les      |

  307. |  7 | jianxin  | jianxin  |

  308. |  8 | les      | les      |

  309. |  9 | jianxin  | jianxin  |

  310. | 10 | lee      | lee      |

  311. | 11 | system   | system   |

  312. | 12 | admin    | admin    |

  313. | 13 | admin    | admin    |

  314. | 16 | hiveuser | hiveuser |

  315. | 18 | hivemiss | hivemiss |

  316. +----+----------+----------+

  317. **************************************


本文分享自微信公衆號 - 我愛問讀書(wawds_)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章