2016-08-13 28 views
2

我的字符串數據如下所示。如何在配置單元中的多分隔符字符串上使用字符串拆分功能?

data = 'ABCD/~DEFG/~HJKL/~MNOP' 

我試圖與下面查詢

select split(data,'[/~]')[1] from test_table; 

預期輸出: ABCD 原始輸出: [SPACES]

當我試圖與索引相同的查詢[2]其加工。

select split(data,'[/~]')[2] from test_table; 

預期輸出: DEFG 原始輸出: DEFG

我的觀察: 它像0,2,4 .. 每偶數的索引編號做工精細,它是填充空間對於奇數指數如1,3,5 ..

有人可以幫我解決這個問題。

回答

3

您需要使用拆分爲:split('\\/~')

hive> select split('word1/~word2/~word3','\\/~')[0] as word1; 
word1 

此外, 檢查我的答案是:使用load-data-into-hive-with-custom-delimiterMultiDelimitSerDe
和其他選項使用regexp_extracthive-split-string-using-regex


例子:應該有一些簡單的正則表達式來實現這一點,但我想出了在此之後跟着example from here

hive> select regexp_extract('word1/~word2/~word3','^(\\w.*)\\/~(\\w.*)$',2) as word3; 
word3 

hive> select regexp_extract('word1/~word2/~word3','^(?:([^/~]+)\\/~?){1}',1) as word1; 
word1 

hive> select regexp_extract('word1/~word2/~word3','^(?:([^/~]+)\\/~?){2}',1) as word2; 
word2 
相關問題