postgresql导入数据到hive

整张表导入

在linux环境中运行:

[root@dthost27 ~]# 
sqoop import --connect jdbc:postgresql://192.168.xxx.xxx:5432/pgsql_db 
--username user11 --password 123456 --table pgsql_table11 
--hive-import --hive-database hive_db --hive-table  hive_table11  
--map-column-hive reportinfo=string --map-column-java reportinfo=String 
--hive-overwrite --delete-target-dir --hive-drop-import-delims --hive-drop-import-delims --null-string '' --null-non-string '' -m5e

注意:
(1)jdbc:postgresql://192.168.xxx.xxx:5432/pgsql_db 指定了postgresql的地址和库名
(2)username–用户名;password–密码;table–指定该库中的某个表
(3)hive-database–指定hive中的库;hive-table–指定hive中的表,这个表允许未创建,命令执行时可自动创建
(4) --map-column-hive reportinfo=string --map-column-java reportinfo=String:这是因为要导入的pgsql表中有一个字段的类型为json,hive中没有对应类型,因此增加了参数–map-column-hive reportinfo=string 和–map-column-java reportinfo=String,表示对应的那个json字段在hive中用string的类型来存
(5)hive-overwrite —如果hive中同名的表已存在,这次导入将会覆盖写入,即删除原有的表中的信息,再写入来自pgsql的数据
(6)delete-target-dir—会删除原有hive表的对应的hdfs路径上的文件

查询后导入

顾明思议,就是在postgresql上先查询出信息后,再将其导入hive中

[root@dthost27 ~]# 
sqoop import --connect jdbc:postgresql://192.168.xxx.xxx:5432/pgsql_db   
--username user11 --password 123456  
--query 'select id,check_code,lastupdatetime from pgsql_table11  where  $CONDITIONS'  
--delete-target-dir  --target-dir /user/hive/warehouse/pgsql_db/pgsql_test   
--fields-terminated-by '\001' --null-string '' --null-non-string '' -m1

参数解释:
(1)query:指定查询的语句
(2)target-dir 后面跟原有hive表的存放路径(hdfs上的存放路径,可通过show create table xxxx来查询)

你可能感兴趣的:(hive,postgresql)