使用sqoop導入數(shù)據(jù)至hive常用語句
直接導入hive表
sqoop import
--connect jdbc:postgresql://ip/db_name
--username user_name
--table table_name
--hive-import -m 5
內部執(zhí)行實際分三部,1.將數(shù)據(jù)導入hdfs(可在hdfs上找到相應目錄),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ū)名稱)
導入hive已經創(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導入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子句若用雙引號,則$CONDITIONS需要使用\轉義,若使用單引號,則不需要轉義。
遇到問題
若需要在導入hive數(shù)據(jù)表的前提下,再添加在原有關系型數(shù)據(jù)庫中沒有的一列數(shù)據(jù)如何解決。
首先,我們想到的是添加一個partition可很方便的添加“一列”數(shù)據(jù),partition的使用很類似普通一列,常用的sql執(zhí)行是沒有問題的。
其次,想到在query的sql中添加一個常量或者一個變量,例如:”select 'hello',* from retail_tb_order where \$CONDITIONS“,執(zhí)行后會報異常
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類會在postgresql表中解析sql中的所有列,當解析常量'hello'時,數(shù)據(jù)庫沒有該列也就找不到相應的數(shù)據(jù)類型。
若要解決該問題應該需修改ClassWriter源碼。
補充:使用Sqoop,最終導入到hive中的數(shù)據(jù)和原數(shù)據(jù)庫中數(shù)據(jù)不一致解決辦法
Sqoop是一款開源的工具,主要用于在Hadoop(Hive)與傳統(tǒng)的數(shù)據(jù)庫(mysql、postgresql...)間進行數(shù)據(jù)的傳遞,可以將一個關系型數(shù)據(jù)庫(例如 : MySQL ,Oracle ,Postgres等)中的數(shù)據(jù)導進到Hadoop的HDFS中,也可以將HDFS的數(shù)據(jù)導進到關系型數(shù)據(jù)庫中。
1.問題背景
使用Sqoop把oracle數(shù)據(jù)庫中的一張表,這里假定為student,當中的數(shù)據(jù)導入到hdfs中,然后再創(chuàng)建hive的external表,location到剛才保存到hdfs中數(shù)據(jù)的位置。最后發(fā)現(xiàn)對hive中表特定條件進行count時結果和oracle中結果不一致。
1.1 導入數(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
這個時候hdfs上/user/hadoop/student下就保存了從oracle上導入的表數(shù)據(jù)。
表數(shù)據(jù)在hdfs上是如何存儲的呢?注意這一點,造成了最后產生結果不一致的錯誤。
我們來看一看在hdfs上數(shù)據(jù)是如何存儲的。我們運行hadoop fs -cat /user/hadoop/student/part-m-00000,可以看到原來字段與字段之間都用‘,'分隔開,這是sqoop默認的,這時候,如果一個字段值當中包含‘,',再向hive中插入數(shù)據(jù)時分隔就會出錯。因為hive也是用‘,'分隔的。
2.分析問題
對hive中表select count(*) from student的結果和oracle中select count(*) from studeng的結果進行比較,發(fā)現(xiàn)條數(shù)是一樣的,說明沒有少load數(shù)據(jù)。那為什么對特定條件結果就會不一致,而且hive中條數(shù)比oracle中少。也就是同時運行select count(*) from student where class_id='003'
最后,發(fā)現(xiàn)hive用逗號分隔數(shù)據(jù)時,有幾條數(shù)據(jù)字段內值包含有逗號,所以字段與值對應起來就亂套了,所以得不到正確結果。
我們建議用‘\001'來進行sqoop 導入數(shù)據(jù)時的 分割。也就是--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
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- 在Hadoop集群環(huán)境中為MySQL安裝配置Sqoop的教程
- sqoop export導出 map100% reduce0% 卡住的多種原因及解決
- 解決sqoop從postgresql拉數(shù)據(jù),報錯TCP/IP連接的問題
- sqoop讀取postgresql數(shù)據(jù)庫表格導入到hdfs中的實現(xiàn)
- 解決sqoop import 導入到hive后數(shù)據(jù)量變多的問題
- 使用shell腳本執(zhí)行hive、sqoop命令的方法
- Sqoop的安裝與使用詳細教程