0738-6.2.0-如何在Hive中使用多分隔符

  • 2020 年 2 月 10 日
  • 筆記

文檔編寫目的

Hive在0.14及以後版本支援欄位的多分隔符,參考:

https://cwiki.apache.org/confluence/display/Hive/MultiDelimitSerDe

而Fayson在以前的文章中也基於C5的環境介紹過如何在Hive中使用多分隔符,參考《Hive多分隔符支援示例》。本文主要介紹在CDH6中如何讓Hive支援多分隔符。

  • 測試環境

1.Redhat7.2

2.CDH6.2.0

3.Hive2.1

數據準備

如何將多個字元作為欄位分割符的數據文件載入到Hive表中,示例數據如下:

欄位分隔符為「@#$」

test1@#$test1name@#$test2value  test2@#$test2name@#$test2value  test3@#$test3name@#$test4value  

如何將上述示例數據載入到Hive表(multi_delimiter_test)中,表結構如下:

欄位名

欄位類型

s1

String

s2

String

s3

String

實現方式

1.從CM進入Hive,點擊配置搜索aux,在Hive 輔助 JAR 目錄 中輸入/opt/cloudera/parcels/CDH/lib/hive/contrib,保存更改,重啟。

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 目錄,導致找不到類。需要按照在Cloudera Manager中指定Hive的輔助JAR目錄,然後重啟,再次查詢即可。目錄的路徑為/opt/cloudera/parcels/CDH/lib/hive/contrib