sqoop导入导出实例以及多分区案例


1.从mysql导出到hive

   1.只插入

    sqoop import   \

     --connect  jdbc:mysql://localhost:3306/test  \

     --username  escheduler_dev  \

     --password  2wsxVFR$  \

     --query  "select id  as  id,name  as  name,age  as  age from a_demo where  1=1  and  \$CONDITIONS "   \

     --hive-import  \

     --hive-database  default  \

     --hive-table  demo10   \

     --target-dir  /tmp/hyj-$RANDOM  \

     --hive-partition-key  date_id  \

     --hive-partition-value  1000  \

     --m  1;  \

   注释:--connect是源数据库即mysql;--username和--password是mysql的用户名密码,--query是做查询条件,可以筛选数据,也可以对字段起别名,用于字段映射;

            --hive-database和--hive-table用于hive非orc表的语法,即hive的库名和表明;--target-dir缓存目录;--hive-partition-key和--hive-partition-value是分区字段名以及值

    2.覆盖

             在例1后面拼接 --hive-overwrite

             标注:如果hive表为orc表,就sqoop无法进行overwrite覆盖

 

2.从hive到mysql

   1.insert仅插入

    sqoop export   

    --connect  jdbc:mysql://localhost:3306/escheduler_dev 

    --username  escheduler_dev

    --password  2wsxVFR$ 

    --table a_demo 

    --columns  "id,name,age" 

    --hcatalog-database  default 

    --hcatalog-table  demo10 

    --hive-partition-key  date_id 

    --hive-partition-value  1000 

    --m  1 ; 

    注释:hcatalog模式可用于orc表

   

    2.仅更新

    sqoop export   

    --connect  jdbc:mysql://localhost:3306/escheduler_dev 

    --username  escheduler_dev 

    --password  2wsxVFR$ 

    --table a_demo 

    --columns  "id,name,age" 

    --hcatalog-database  default 

    --hcatalog-table  demo10 

    --hive-partition-key  date_id 

    --hive-partition-value  1000 

    --m  1 

    --update-key id 

    --update-mode updateonly;    

    注释:--columns是更新或插入哪些字段;--update-key更新依据的字段名,比如上述案例以id进行更新,--update-mode是更新模式,updateonly仅更新

    

    3.更新并插入

    sqoop export   

    --connect  jdbc:mysql://localhost:3306/escheduler_dev 

    --username  escheduler_dev 

    --password  2wsxVFR$ 

    --table a_demo 

    --columns  "id,name,age" 

     --hcatalog-database  default 

    --hcatalog-table  demo10 

    --hive-partition-key  date_id 

     --hive-partition-value  1000 

     --m  1 

    --update-key id 

     --update-mode allowinsert;  

    注释:--update-mode的模式是allowinsert更新并插入

 

3.sqoop的多分区支持

  sqoop import   

  --connect  jdbc:mysql://localhost:3306/escheduler_dev 

  --username  escheduler_dev 

  --password  2wsxVFR$ 

  --query  "select name  as  name from a_demo where  1=1  and  \$CONDITIONS "   

  --hcatalog-database  default 

  --hcatalog-table  test88 

  --hcatalog-partition-keys  pt,hh,mm 

  --hcatalog-partition-values  100,200,300 

  --m  1 ;

注释:--hcatalog-partition-keys可以写多个分区字段名,--hcatalog-partition-values是对应多个字段名的值,但是仅限hcatalog模式,该模式下不支持overwrite

 

以上案例经过测试均可用


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM