濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫(kù) > sqoop 實(shí)現(xiàn)將postgresql表導(dǎo)入hive表

sqoop 實(shí)現(xiàn)將postgresql表導(dǎo)入hive表

熱門標(biāo)簽:漯河外呼電話系統(tǒng) 美容工作室地圖標(biāo)注 合肥公司外呼系統(tǒng)運(yùn)營(yíng)商 地圖標(biāo)注和圖片名稱的區(qū)別 打電話智能電銷機(jī)器人授權(quán) 辦公外呼電話系統(tǒng) 海豐有多少商家沒有地圖標(biāo)注 重慶自動(dòng)外呼系統(tǒng)定制 外呼調(diào)研系統(tǒng)

使用sqoop導(dǎo)入數(shù)據(jù)至hive常用語句

直接導(dǎo)入hive表

sqoop import 
--connect jdbc:postgresql://ip/db_name
--username user_name 
--table table_name 
--hive-import -m 5 

內(nèi)部執(zhí)行實(shí)際分三部,1.將數(shù)據(jù)導(dǎo)入hdfs(可在hdfs上找到相應(yīng)目錄),2.創(chuàng)建hive表名相同的表,3,將hdfs上數(shù)據(jù)傳入hive表中

sqoop根據(jù)postgresql表創(chuàng)建hive表

sqoop create-hive-table 
--connect jdbc:postgresql://ip/db_name 
--username user_name 
--table table_name 
--hive-table hive_table_name
( --hive-partition-key partition_name若需要分區(qū)則加入分區(qū)名稱) 

導(dǎo)入hive已經(jīng)創(chuàng)建好的表中

sqoop import 
--connect jdbc:postgresql://ip/db_name 
--username user_name 
--table table_name 
--hive-import -m 5 
--hive-table hive_table_name 
(--hive-partition-key partition_name --hive-partition-value partititon_value);

使用query導(dǎo)入hive表

sqoop import 
--connect jdbc:postgresql://ip/db_name 
--username user_name 
--query "select ,* from retail_tb_order where \$CONDITIONS" 
--hive-import -m 5 
--hive-table hive_table_name 
(--hive-partition-key partition_name --hive-partition-value partititon_value); 

注意:$CONDITIONS條件必須有,query子句若用雙引號(hào),則$CONDITIONS需要使用\轉(zhuǎn)義,若使用單引號(hào),則不需要轉(zhuǎn)義。

遇到問題

若需要在導(dǎo)入hive數(shù)據(jù)表的前提下,再添加在原有關(guān)系型數(shù)據(jù)庫(kù)中沒有的一列數(shù)據(jù)如何解決。

首先,我們想到的是添加一個(gè)partition可很方便的添加“一列”數(shù)據(jù),partition的使用很類似普通一列,常用的sql執(zhí)行是沒有問題的。

其次,想到在query的sql中添加一個(gè)常量或者一個(gè)變量,例如:”select 'hello',* from retail_tb_order where \$CONDITIONS“,執(zhí)行后會(huì)報(bào)異常

12/08/28 14:41:31 INFO tool.CodeGenTool: Beginning code generation 
12/08/28 14:41:31 INFO manager.SqlManager: Executing SQL statement: select 'hello',* from retail_tb_order where (1 = 0) 
12/08/28 14:41:32 INFO manager.SqlManager: Executing SQL statement: select 'hello',* from retail_tb_order where (1 = 0) 
12/08/28 14:41:32 ERROR orm.ClassWriter: Cannot resolve SQL type 1111 
12/08/28 14:41:32 ERROR orm.ClassWriter: Cannot resolve SQL type 1111 
12/08/28 14:41:32 ERROR orm.ClassWriter: No Java type for SQL type 1111 for column _column_ 
12/08/28 14:41:32 ERROR orm.ClassWriter: No Java type for SQL type 1111 for column _column_ 
12/08/28 14:41:32 ERROR orm.ClassWriter: No Java type for SQL type 1111 for column _column_ 
12/08/28 14:41:32 ERROR orm.ClassWriter: No Java type for SQL type 1111 for column _column_ 
12/08/28 14:41:32 ERROR orm.ClassWriter: No Java type for SQL type 1111 for column _column_ 
12/08/28 14:41:32 ERROR orm.ClassWriter: No Java type for SQL type 1111 for column _column_ 
12/08/28 14:41:32 ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.NullPointerException 
java.lang.NullPointerException 
 at org.apache.sqoop.orm.ClassWriter.parseNullVal(ClassWriter.java:900) 
 at org.apache.sqoop.orm.ClassWriter.parseColumn(ClassWriter.java:925) 
 at org.apache.sqoop.orm.ClassWriter.generateParser(ClassWriter.java:999) 
 at org.apache.sqoop.orm.ClassWriter.generateClassForColumns(ClassWriter.java:1314) 
 at org.apache.sqoop.orm.ClassWriter.generate(ClassWriter.java:1138) 
 at org.apache.sqoop.tool.CodeGenTool.generateORM(CodeGenTool.java:82) 
 at org.apache.sqoop.tool.ImportTool.importTable(ImportTool.java:367) 
 at org.apache.sqoop.tool.ImportTool.run(ImportTool.java:453) 
 at org.apache.sqoop.Sqoop.run(Sqoop.java:145) 
 at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:65) 
 at org.apache.sqoop.Sqoop.runSqoop(Sqoop.java:181) 
 at org.apache.sqoop.Sqoop.runTool(Sqoop.java:220) 
 at org.apache.sqoop.Sqoop.runTool(Sqoop.java:229) 
 at org.apache.sqoop.Sqoop.main(Sqoop.java:238) 
 at com.cloudera.sqoop.Sqoop.main(Sqoop.java:57) 

該問題出現(xiàn)原因是sqoop ClassWriter類會(huì)在postgresql表中解析sql中的所有列,當(dāng)解析常量'hello'時(shí),數(shù)據(jù)庫(kù)沒有該列也就找不到相應(yīng)的數(shù)據(jù)類型。

若要解決該問題應(yīng)該需修改ClassWriter源碼。

補(bǔ)充:使用Sqoop,最終導(dǎo)入到hive中的數(shù)據(jù)和原數(shù)據(jù)庫(kù)中數(shù)據(jù)不一致解決辦法

Sqoop是一款開源的工具,主要用于在Hadoop(Hive)與傳統(tǒng)的數(shù)據(jù)庫(kù)(mysql、postgresql...)間進(jìn)行數(shù)據(jù)的傳遞,可以將一個(gè)關(guān)系型數(shù)據(jù)庫(kù)(例如 : MySQL ,Oracle ,Postgres等)中的數(shù)據(jù)導(dǎo)進(jìn)到Hadoop的HDFS中,也可以將HDFS的數(shù)據(jù)導(dǎo)進(jìn)到關(guān)系型數(shù)據(jù)庫(kù)中。

1.問題背景  

使用Sqoop把oracle數(shù)據(jù)庫(kù)中的一張表,這里假定為student,當(dāng)中的數(shù)據(jù)導(dǎo)入到hdfs中,然后再創(chuàng)建hive的external表,location到剛才保存到hdfs中數(shù)據(jù)的位置。最后發(fā)現(xiàn)對(duì)hive中表特定條件進(jìn)行count時(shí)結(jié)果和oracle中結(jié)果不一致。

1.1 導(dǎo)入數(shù)據(jù)到hdfs中/user/hadoop/student路徑下

sqoop import --connect "jdbc:oracle:thin:@//localhost:1521/student" --password "***" --username "***" --query "select * from student where name='zhangsan' and class_id='003' and \$CONDITIONS" --target-dir "/user/hadoop/student" --verbose -m 1

這個(gè)時(shí)候hdfs上/user/hadoop/student下就保存了從oracle上導(dǎo)入的表數(shù)據(jù)。

表數(shù)據(jù)在hdfs上是如何存儲(chǔ)的呢?注意這一點(diǎn),造成了最后產(chǎn)生結(jié)果不一致的錯(cuò)誤。

我們來看一看在hdfs上數(shù)據(jù)是如何存儲(chǔ)的。我們運(yùn)行hadoop fs -cat /user/hadoop/student/part-m-00000,可以看到原來字段與字段之間都用‘,'分隔開,這是sqoop默認(rèn)的,這時(shí)候,如果一個(gè)字段值當(dāng)中包含‘,',再向hive中插入數(shù)據(jù)時(shí)分隔就會(huì)出錯(cuò)。因?yàn)閔ive也是用‘,'分隔的。

2.分析問題  

對(duì)hive中表select count(*) from student的結(jié)果和oracle中select count(*) from studeng的結(jié)果進(jìn)行比較,發(fā)現(xiàn)條數(shù)是一樣的,說明沒有少load數(shù)據(jù)。那為什么對(duì)特定條件結(jié)果就會(huì)不一致,而且hive中條數(shù)比oracle中少。也就是同時(shí)運(yùn)行select count(*) from student where class_id='003'

最后,發(fā)現(xiàn)hive用逗號(hào)分隔數(shù)據(jù)時(shí),有幾條數(shù)據(jù)字段內(nèi)值包含有逗號(hào),所以字段與值對(duì)應(yīng)起來就亂套了,所以得不到正確結(jié)果。

我們建議用‘\001'來進(jìn)行sqoop 導(dǎo)入數(shù)據(jù)時(shí)的 分割。也就是--fields-terminated-by char>參數(shù)。

參考:http://sqoop.apache.org/docs/1.4.2/SqoopUserGuide.html#_large_objects

最后優(yōu)化后的sqoop語句為:

sqoop import --connect "jdbc:oracle:thin:@//localhost:1521/student" --password "***" --username "***" --query "select * from student where name='zhangsan' and class_id='003' and \$CONDITIONS" --target-dir "/user/hadoop/student" --fields-terminated-by "\001" --verbose -m 1

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 在Hadoop集群環(huán)境中為MySQL安裝配置Sqoop的教程
  • sqoop export導(dǎo)出 map100% reduce0% 卡住的多種原因及解決
  • 解決sqoop從postgresql拉數(shù)據(jù),報(bào)錯(cuò)TCP/IP連接的問題
  • sqoop讀取postgresql數(shù)據(jù)庫(kù)表格導(dǎo)入到hdfs中的實(shí)現(xiàn)
  • 解決sqoop import 導(dǎo)入到hive后數(shù)據(jù)量變多的問題
  • 使用shell腳本執(zhí)行hive、sqoop命令的方法
  • Sqoop的安裝與使用詳細(xì)教程

標(biāo)簽:衡陽 來賓 蚌埠 晉城 烏海 錦州 珠海 株洲

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《sqoop 實(shí)現(xiàn)將postgresql表導(dǎo)入hive表》,本文關(guān)鍵詞  sqoop,實(shí),現(xiàn)將,postgresql,表,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《sqoop 實(shí)現(xiàn)將postgresql表導(dǎo)入hive表》相關(guān)的同類信息!
  • 本頁收集關(guān)于sqoop 實(shí)現(xiàn)將postgresql表導(dǎo)入hive表的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    通河县| 深泽县| 边坝县| 集贤县| 德化县| 乃东县| 金昌市| 武川县| 阜宁县| 库尔勒市| 罗城| 台中县| 吕梁市| 云林县| 贵州省| 冀州市| 珠海市| 邯郸市| 淮北市| 凤冈县| 阜宁县| 垫江县| 津南区| 沁阳市| 桂林市| 安宁市| 彭阳县| 高碑店市| 珠海市| 阳江市| 永平县| 大姚县| 隆回县| 乌拉特中旗| 东乡族自治县| 通化县| 澜沧| 临清市| 平凉市| 鄂尔多斯市| 惠东县|