MySQL同步到Hive操作步驟
1. 下載sqoop
地址為:http://archive.cloudera.com/cdh/3/下載相應版本,如sqoop-1.2.0-CDH3B4.tar.gz
2. 下載 hadoop
地址為:http://archive.cloudera.com/cdh/3/,版本可以為hadoop-0.20.2-CDH3B4.tar.gz
3.解壓 sqoop-1.2.0-CDH3B4.tar.gz ,hadoop-0.20.2-CDH3B4.tar.gz 到某目錄如/home/hadoop/,解壓後的目錄為
A:/home/hadoop/ sqoop-1.2.0-CDH3B4.tar
B:/home/hadoop/ hadoop-0.20.2-CDH3B4
4.複製B裡hadoop-core-0.20.2-CDH3B4.jar到sqoop(A)的lib下
5. sqoop匯入mysql資料執行過程中依賴mysql-connector-java-*.jar,所以需要下載mysql-connector-java-*.jar到sqoop(A)的lib下
6.修改configure-sqoop
註釋掉hbase zookeeper檢查:
#if [ ! -d "${HBASE_HOME}" ]; then # echo "Error: $HBASE_HOME does not exist!" # echo 'Please set $HBASE_HOME to the root of your HBase installation.' # exit 1 #fi #if [ ! -d "${ZOOKEEPER_HOME}" ]; then # echo "Error: $ZOOKEEPER_HOME does not exist!" # echo 'Please set $ZOOKEEPER_HOME to the root of your ZooKeeper installation.' # exit 1 #fi
設定表的訪問許可權:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'hadoop_test'@'%' WITH GRANT OPTION,
此命令的含義是把mysql下的hadoop_test的訪問許可權為任意IP,如果需要設定特定IP訪問,則把%換成具體IP,如
mysql> GRANT ALL PRIVILEGES ON *.* TO 'hadoop_test'@'10.6.42.101' WITH GRANT
列出mysql所有的表:
./sqoop list-tables --connect jdbc:mysql://10.6.42.101:3306/test --username hadoop_test --password 123456
匯入mysql表到hive:
./sqoop import --connect jdbc:mysql://10.6.42.101:3306/test --username hadoop_test --password 123456 --table mytest--hive-import匯入需要表裡有主建,還要注意不要使用127.0.0.1,因為map出去不一定在哪個節點執行。
如果曾經執行失敗過,那再執行的時候,會有錯誤提示:
ERROR tool.ImportTool: Encountered IOException running import job: org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory xxx already exists
執行 $HADOOP_HOME/bin/hadoop fs -rmr xxx 即可
8.驗證:
bin/hive
show tables;多了一個表
9.經驗:
sqoop做了一些mysqldump時的map reduce,所以速度會比單純的dump後load快。