使用sqoop從MySQL匯入資料到HBase
阿新 • • 發佈:2019-01-09
工作需求:將MySQL資料庫的資料傳入HBase
第一次將工作經驗上傳:
- hadoop叢集是在35
- MySQL資料庫是在31
- 最終因為35不能訪問31,所以換了完成方法
實現步驟
下載
下載地址
需要的jar包
jdbc 驅動包
拷貝mysql-connector-Java-5.1.31-bin.jar到sqoop/lib目錄下。
配置環境變數
vi etc/profile
export SQOOP_HOME=/opt/cdh5/sqoop
export PATH=$PATH:$SQOOP_HOME/bin
修改conf/sqoop-env.sh
#Set path to where bin/hadoop is available
export HADOOP_COMMON_HOME=/opt/cloud/hadoop-2.6.0-cdh5.4.1/
#Set path to where hadoop-*-core.jar is available
export HADOOP_MAPRED_HOME=/opt/cloud/hadoop-2.6.0-cdh5.4.1/
#set the path to where bin/hbase is available
export HBASE_HOME=/opt/cloud/hbase-1.0.0-cdh5.4.1/
#Set the path to where bin/hive is available
export HIVE_HOME=/opt/cloud/hive-1.1.0-cdh5.4.1/
#Set the path for where zookeper config dir is
export ZOOCFGDIR=/opt/cloud/zookeeper-3.4.8/
修改bin/configure-sqoop
## Moved to be a runtime check in sqoop.
#if [ ! -d "${HBASE_HOME}" ]; then
# echo "Warning: $HBASE_HOME does not exist! HBase imports will fail."
# echo 'Please set $HBASE_HOME to the root of your HBase installation.'
#fi
## Moved to be a runtime check in sqoop.
#if [ ! -d "${HCAT_HOME}" ]; then
# echo "Warning: $HCAT_HOME does not exist! HCatalog jobs will fail."
# echo 'Please set $HCAT_HOME to the root of your HCatalog installation.'
#fi
#
#if [ ! -d "${ACCUMULO_HOME}" ]; then
# echo "Warning: $ACCUMULO_HOME does not exist! Accumulo imports will fail."
# echo 'Please set $ACCUMULO_HOME to the root of your Accumulo installation.'
#fi
#if [ ! -d "${ZOOKEEPER_HOME}" ]; then
# echo "Warning: $ZOOKEEPER_HOME does not exist! Accumulo imports will fail."
# echo 'Please set $ZOOKEEPER_HOME to the root of your Zookeeper installation.'
#fi
測試
./sqoop version
17/05/09 19:02:04 INFO sqoop.Sqoop: Running Sqoop version: 1.4.5-cdh5.4.1
Sqoop 1.4.5-cdh5.4.1
git commit id 8e266e052e423af592871e2dfe09d54c03f6a0e8
Compiled by jenkins on Thu May 7 22:44:23 PDT 2015
如上就成功
匯入資料
./sqoop import --connect jdbc:mysql://10.10.97.116:3306/rsearch --table researchers--hbase-table A--column-family person --hbase-row-key id --hbase-create-table --username 'root' -P
說明:
–connect jdbc:mysql://10.10.97.116:3306/rsearch 表示遠端或者本地 Mysql 服務的URI,3306是Mysql預設監聽埠,rsearch是資料庫,若是其他資料庫,如Oracle,只需修改URI即可。
–table researchers 表示匯出rsearch資料庫的researchers表。
–hbase-table A 表示在HBase中建立表A。
–column-family person 表示在表A中建立列族person。
–hbase-row-key id 表示表A的row-key是researchers表的id欄位。
–hbase-create-table 表示在HBase中建立表。
–username ‘root’ 表示使用使用者root連線Mysql。