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