Sqoop工具的使用和詳解

2021-08-21 05:56:20 字數 2216 閱讀 4970

sqoop是用來進行資料匯入匯出的工具,一般用在hadoop這個框架中,常用的應用場景有將mysql資料庫中的資料匯入hdfs或者hive、hbase,

或者從後者中匯出到關係型資料庫中,下面將進行幾段**的演示匯入和匯出的過程。

將mysql中的資料匯入到hadoop集群上(hdfs):

首先將指令碼命令貼出:

./sqoop import --connect jdbc:mysql://localhost/test --username root --password lakers  --table persons -m 1

username:資料庫的使用者名稱

password:使用者名稱對應的密碼

table:資料中需要匯入的表名

-m :啟動mapreduce的個數。

這裡實現的是將乙個表(test)的全部資料匯入到hdfs中,其中可以設定匯入到hdfs的路徑 通過引數--target-dir 設定,其他的還有很多的引數設定可以通過去官網檢視:

將test表中的某些資料匯入到hdfs中:

./sqoop import --connect jdbc:mysql://localhost/test --username root --password lakers  --query 'select id,name from persons where $conditions' --target-dir /persons -m 1

將mysql資料匯入hive:

傳統地將mysql中的資料匯入hdfs有中方法就是:

1 將mysql資料匯出到本地

2 在hive中建立**式

3 將匯出在本地的資料匯入到hive中

但是用了sqoop之後我們可以一步完成上面的操作。

首先將mysql的資料匯入到hive上,其實也是匯入到hdfs上,但是有個好處是我們可以直接通過hive去處理資料,得到我們想要的結果,下面的shell指令碼和上面的指令碼很相似。

./sqoop import --connect jdbc:mysql://localhost/test --username root --password lakers  --table persons -m 1 --hive-import

這裡有個需要說明的是,我們將mysql資料匯入到hive中的時候是不需要去建立表,在匯入過程中sqoop會自動解析資料,幫我們建立對應格式的表結構。這裡不得不說省了很大的功夫,畢竟建立表結構很麻煩的,尤其是表字段很多的情況下。

和上面的區別就是最後加了--hive-import 同時也是可以通過引數來設定匯入的目錄,從官網查詢。

mysql中資料匯入到hbase:

./sqoop import --connect jdbc:mysql://localhost/test --username root --password lakers  --table persons -m 1 --hbase-create-table --hbase-table person2 --column-family info --hbase-row-key sid

--hbase-create-table :自動建立hbase表

--hbase-table :表明

--column-family:列簇名

--hbase-row-key:指定rowkey對應mysq中的鍵

注:目前還不能直接將hbase中資料直接匯出到mysql中,可以先導出到本地,然後再匯入mysql

將hive中的資料匯出到mysql中:

./sqoop export --connect jdbc:mysql://localhost/test --username root --password lakers -m 1 --table personcopy --export-dir /hive/warehouse/persons --input-fields-terminated-by '\0001'

--export-dir:匯出資料的目錄

--input-fileds-terminated-by : 字段之間的分割符

注意:這裡需要注意的是,我們在從mysql匯入hive中的時候是不需要建立表結構的,但是我們在將hive的資料匯出到mysql中的時候是需要自己建立表結構的,很簡單的原因,比如我們mysql 的乙個varchar(50)或者varchar(60)這種會自動在hive中解析為string型別,

但是相反的是,將hive中的string型別匯入到mysql中就不知道是解析為varchar(?),所以在這個時候我們需要手動的去建立對應的表結構!

sqoop使用詳解

sqoop用於關聯式資料庫和hadoop家族 hdfs hive hbase 之間的etl 資料庫匯出到hadoop家族 sqoop import hadoop家族匯出到資料庫 sqoop export 官方 tar zxvf sqoop 1.4.7.binhadoop 2.6.0.tar.gz m...

Sqoop (二)Sqoop 的簡單使用案例

二 匯出資料 三 指令碼打包 在sqoop中,匯入 概念指 從非大資料集群 rdbms 向大資料集群 hdfs,hive,hbase 中傳輸資料,叫做 匯入,即使用import關鍵字。確定mysql服務開啟正常 在mysql中新建一張表並插入一些資料 mysql uroot p000000 mysq...

Sqoop1 4 2資料同步工具基礎使用

我們使用sqoop主要是用作資料庫和hdfs之間的資料同步操作。那麼在hdfs中主要使用的管理工具是hive。目前,使用sqoop使用 hive import功能是有異常的。在使用hive import功能的時候,會自動生成create table 指令碼。而無法根據hive的metadata來進行...