1. 程式人生 > >利用sqoop將hive和MySQL資料互匯入

利用sqoop將hive和MySQL資料互匯入

1.    hadoop、hive、MySQL安裝(略)啟動hadoop

執行start-all.sh start-dfs.sh start-yarn.sh

2.    下載sqoop

3.    解壓

#tar -zxvfsqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz

4.    建立軟連線

#ln -s sqoop-1.4.6.bin__hadoop-2.0.4-alpha sqoop

5.    加執行檔案路徑

#export PATH=$PATH:/home/grid/sqoop/bin

6.    把MySQL Java連結器的jar檔案拷貝到sqoop的lib目錄下

(注意:聯結器的版本最低為5.1.35)

#cpmysql-connector-java-5.1.3-bin.jar /etc/yum.repos.d/sqoop/lib/(sqoop的所在的目錄)

7.    測試

(1)   列出mysql資料庫中的所有資料庫

#sqooplist-databases --connect jdbc:mysql://192.168.56.104:3306?useSSL=false--username root --password 123456  (其中192.168.56.104位主機ip,root為MySQL資料庫使用者名稱、123456位密碼(我的MySQL資料庫名與密碼為hive,hive其他的會出現許可權問題))

(2)     連線MySQL並列出資料庫中的表

#sqoop list-tables --connect jdbc:mysql://192.168.56.104:3306/test?useSSL=false--username root --password 123456(test為資料庫名

(3)     將MySQL的test.t1(資料庫test中的t1表)表結構複製到Hive的test庫中,表名為mysql_t1

#sqoop create-hive-table --connectjdbc:mysql://192.168.56.104:3306/test?useSSL=false --table t1 --username root--password 123456 --hive-table test.mysql_t1 (test.mysql_t1

為Hive資料庫中test資料庫中的mysql_t1表)

注:該命令可以多次執行不報錯

(4)     將MySQL表的資料匯入到hive中

追加資料

#sqoop import --connect jdbc:mysql://192.168.56.104:3306/test?useSSL=false--username root --password 123456 --table t1 --hive-import --hive-tabletest.mysql_t1

(5)     將Hive表的資料匯入到MySQL中

#sqoop export --connectjdbc:mysql://192.168.56.104:3306/test?useSSL=false --username root --password123456 --table t2 --export-dir /user/hive/warehouse/test.db/mysql_t1  (注意主鍵中資料不可以重複

注意:若出現Output directory hdfs://SparkMaster:9000/user/root/uk already exists的錯誤時,

解決方案:先把HDFS中的輸出目錄幹掉

#hadoopfs -ls -R (可檢視目錄)

#hadoopfs -rmr output  (刪除多餘的輸出檔案例如錯誤提示中所提到已經存在的檔案)

MapReduce執行是不允許輸出目錄存在的,自動建立!

參考:http://blog.csdn.net/wzy0623/article/details/50921702

            https://www.cnblogs.com/wrencai/p/3908533.html