利用Sqoop将MySQL海量测试数据导入HDFS和HBase
作者:帥氣陳吃蘋果
一、安裝Sqoop
1、下載sqoop,解壓、文件夾重命名
wget http://mirror.bit.edu.cn/apache/sqoop/1.4.6/sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gztar -zxvf sqoop-1.4.6.bin_hadoop-2.0.4.alpha.tar.gz -C /root/hadoop/mv sqoop-1.4.6.bin_hadoop-2.0.4.alpha sqoop-1.4.62、配置環(huán)境變量
vim /etc/profile
export SQOOP_HOME=/root/hadoop/sqoop-1.4.6export PATH = $PATH:$SQOOP_HME/bin3、驗證
若正常顯示Sqoop版本信息,則安裝配置成功:
[root@localhost ~ ] # sqoop version二、創(chuàng)建數(shù)據(jù)庫和表
安裝MySql后,創(chuàng)建測試數(shù)據(jù)庫和測試表。
數(shù)據(jù)庫名:test
表結(jié)構(gòu):point(pointId int(10) PRIMARYKEY,pointName varchar(16),pointValue int(10))
三、shell腳本生成測試數(shù)據(jù)
編寫Shell腳本,此處的插入語句需要根據(jù)所創(chuàng)建的表結(jié)構(gòu)來修改:
#!/bin/bash i=1; MAX_INSERT_ROW_COUONT=$1; while(( $i <= $MAX_INSERT_ROW_COUNT )) domysql -uhive -phive test -e "insert into test.point(pointId,pointName,pointValue) values($i,'point"$i"',$i);"i=(($i+1)) done exit 0 上述腳本生成測試數(shù)據(jù)速度很慢,筆者生成1000萬條數(shù)據(jù)有如懷胎十月,歡迎建議,感謝!四、MySql數(shù)據(jù)導入
MySQL作為數(shù)據(jù)源時,Sqoop需要依賴MySQL數(shù)據(jù)庫連接驅(qū)動包,下載地址:https://dev.mysql.com/get/Dow...
下載后解壓,將mysql-connector-java-5.1.45文件夾下的mysql-connector-java-5.1.44-bin.jar包拷貝到$SQOOP_HOME/lib目錄下即可。
1、導入HDFS
sqoop import --connect jdbc:mysql://localhost:3306/test --username hive --password hive --table point參數(shù)解析:
import:表示從傳統(tǒng)數(shù)據(jù)庫導入數(shù)據(jù)到 HDFS/HIVE/HBASE等;
--connect:建立數(shù)據(jù)庫連接;
jdbc:mysql://localhost:3306/test:使用jdbc方式連接mysql數(shù)據(jù)庫,數(shù)據(jù)庫名為test;
--username:指定數(shù)據(jù)庫用戶名;
--password:指定數(shù)據(jù)庫密碼;
--table:指定表名
注意:a)HDFS輸出目錄不能已經(jīng)存在;
b)沒有指定-m或者splite-by時,即沒有指定并行度時,要導入的表必須有主鍵,否則會出錯。
c)導入到指定目錄:sqoop import --connect jdbc:mysql://localhost:3306/test --username hive --password hive --table point --target-dir /directory
如果沒有指定輸出目錄,默認在/user/root/下創(chuàng)建一個與表名一致的子目錄作為輸出目錄,導入操作執(zhí)行后,查看HDFS中是否有導入的文件:
hdfs dfs -ls /user/root/point/2、導入HBase
sqoop import --connect jdbc:mysql://localhost:3306/test --username root --password root --table point --hbase-table HPoint --column-family info --hbase-row-key pointId --hbase-create-table參數(shù)解析:
--hbase-table:指定導入到HBase數(shù)據(jù)庫中某表;
--column-family:指定列族名;
--hbase-row-key:指定rowKey;
--hbase-create-table:表示按照參數(shù)創(chuàng)建HBase中的表;
最后,歡迎指正。喜歡的話,點個贊唄,請你吃蘋果。總結(jié)
以上是生活随笔為你收集整理的利用Sqoop将MySQL海量测试数据导入HDFS和HBase的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]ASP.NET Core 指定环境
- 下一篇: linux cmake编译源码,linu