文檔編寫目的
Hive在0.14及以后版本支持字段的多分隔符,參考:
https://cwiki.apache.org/confluence/display/Hive/MultiDelimitSerDe
而在以前的文章中也基于C5的環境介紹過如何在Hive中使用多分隔符hive 用中間表,參考《》。本文主要介紹在CDH6中如何讓Hive支持多分隔符。
1..2
2.CDH6.2.0
3.Hive2.1
數據準備
如何將多個字符作為字段分割符的數據文件加載到Hive表中hive 用中間表,示例數據如下:
字段分隔符為“@#$”
test1@#$test1name@#$test2value
test2@#$test2name@#$test2value
test3@#$test3name@#$test4value
如何將上述示例數據加載到Hive表()中,表結構如下:
字段名
字段類型
s1
s2
s3
實現方式
1.從CM進入Hive,點擊配置搜索aux,在Hive 輔助 JAR 目錄 中輸入/opt///CDH/lib/hive/,保存更改,重啟。
2.準備多分隔符文件并裝載到HDFS對應目錄
[root@cdh1?~]#?ll?-h?multi_de.txt?
-rw-r--r--?1?root?root?1.1G?Jan??6?23:14?multi_de.txt
[root@cdh1?~]#?tail?-10?multi_de.txt?
test2949@#$test2949name@#$test2950value
test2950@#$test2950name@#$test2951value
test2951@#$test2951name@#$test2952value
test2952@#$test2952name@#$test2953value
test2953@#$test2953name@#$test2954value
test2954@#$test2954name@#$test2955value
test2955@#$test2955name@#$test2956value
test2956@#$test2956name@#$test2957value
test2957@#$test2957name@#$test2958value
test2958@#$test2958name@#$test2959value
[root@cdh1?~]#?hadoop?fs?-put?multi_de.txt?/test/
[root@cdh1?~]#?hadoop?fs?-ls?/test/
Found?1?items
-rw-r--r--???3?root?supergroup?1079408772?2020-01-06?23:33?/test/multi_de.txt
3.基于準備好的多分隔符文件建表
create?external?table?multi_delimiter_test(
s1?string,
s2?string,
s3?string)
ROW?FORMAT?SERDE?'org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe'?WITH?SERDEPROPERTIES?("field.delim"="@#$")
stored?as?textfile?location?'/test';
4.測試
0:?jdbc:hive2://localhost:10000/>?select?*?from?multi_delimiter_test?limit?10;
0:?jdbc:hive2://localhost:10000/>?select?count(*)?from?multi_delimiter_test;
常見問題
1.在執行HQL時報錯
Error:?Error?while?compiling?statement:?FAILED:?RuntimeException?MetaException(message:java.lang.ClassNotFoundException?Class?org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe?not?found)?(state=42000,code=40000)
這是由于沒有指定Hive 的輔助 JAR 目錄,導致找不到類。需要按照在 中指定Hive的輔助JAR目錄,然后重啟,再次查詢即可。目錄的路徑為/opt///CDH/lib/hive/