Sqoop的從入門到簡單上手

下載地址:http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.5-cdh5.3.6.tar.gz
參考:
   http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.5-cdh5.3.6/SqoopUserGuide.html
   https://cwiki.apache.org/confluence/display/SQOOP/Home

一、Sqoop安裝步驟
   1. 下載
   2. 解壓
   3. copy mysql的驅動類到lib文件夾中
      cp ~/bigdater/hive-0.13.1-cdh5.3.6/lib/mysql-connector-java-5.1.31.jar ./lib/
      或者
      cp ~/bigdater/softs/mysql-connector-java-5.1.31.jar ./lib/
   4. copy hadoop的hadoop-common-2.5.0-cdh5.3.6.jar hadoop-hdfs-2.5.0-cdh5.3.6.jar hadoop-mapreduce-client-core-2.5.0-cdh5.3.6.jar三個jar到lib文件夾中。
      cp ~/bigdater/hadoop-2.5.0-cdh5.3.6/share/hadoop/common/hadoop-common-2.5.0-cdh5.3.6.jar ./lib/
      cp ~/bigdater/hadoop-2.5.0-cdh5.3.6/share/hadoop/hdfs/hadoop-hdfs-2.5.0-cdh5.3.6.jar ./lib/
      cp ~/bigdater/hadoop-2.5.0-cdh5.3.6/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.5.0-cdh5.3.6.jar ./lib/
   5. 配置sqoop-env.sh文件內容,
      vim conf/sqoop-env.sh
      內容如下:
      export HADOOP_COMMON_HOME=/home/hadoop/bigdater/hadoop-2.5.0-cdh5.3.6
      export HADOOP_MAPRED_HOME=/home/hadoop/bigdater/hadoop-2.5.0-cdh5.3.6
      export HBASE_HOME=/home/hadoop/bigdater/hbase-0.98.6-cdh5.3.6
      export HIVE_HOME=/home/hadoop/bigdater/hive-0.13.1-cdh5.3.6
   6. 添加sqoop常量導用戶環境變量中去
      vim ~/.bash_profile
      在最後添加內容(如下):
      ###### sqoop
      export SQOOP_HOME=/home/hadoop/bigdater/sqoop-1.4.5-cdh5.3.6
      export PATH=$PATH:$SQOOP_HOME/bin
      退出保存後執行命令source ~/.bash_profile
   7. 測試是否安裝成功sqoop version

二、Sqoop命令案例介紹
   1、import命令
案例1:將mysql表test中的數據導入hive的hivetest表,hive的hivetest表不存在。
   sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --hive-table hivetest --hive-import -m 1

案例2:在案例1的基礎上,分別進行overwrite(覆蓋)導入和into(直接加入)導入。
   into: 命令同案例1
   overwrite:
      sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --hive-table hivetest --hive-import -m 1 --hive-overwrite
案例3:在案例2的基礎上,通過增加mysql的test表數據,增量導入到hive表中。
   sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --where "id>9" --hive-table hivetest --hive-import -m 1
   或者
   sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --query "select id,name from test where id>9" --hive-table hivetest --hive-import -m 1
案例4:將test表中的數據導出到使用','分割字段的hive表(hivetest2)中。
   創建表: create table hivetest2(id int,name string) row format delimited fields terminated by ',';
   sqoop:
      sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --hive-table hivetest2 --hive-import -m 1 --fields-terminated-by ","
案例5:將test表的數據導入到hdfs中。
   sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --target-dir /test -m 1
案例6:在案例5的基礎上,增量導入數據到hdfs中。
   sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --target-dir /test -m 1 --check-column id --incremental append --last-value 11

   2、export命令
案例1:將hdfs上的文件導出到關係型數據庫test2表中。
   sqoop export --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test2 --export-dir /test
案例2:將hive表(hivetest)數據導出到關係型數據庫test2表中(使用insertOrUpdate方法導入)。
   hivetest表只留id爲1,2,3,4,5的數據,其他數據刪除。
   hivetest表分隔方式是'\u0001',但是export命令默認使用','分隔數據
   sqoop export --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test2 --export-dir /hive/hivetest --input-fields-terminated-by "\\01" --update-mode allowinsert --update-key id 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章