sparksql 函數-2

   字段映射處理,兩種方式:str_to_map, case when

 

select
    case substr(idcard,1,2)
            when '11' then '北京'
            when '33' then '浙江'
            when '46' then '海南'
            when '65' then '新疆'
            when '12' then '天津'
            when '34' then '安徽'
            when '50' then '重慶'
            when '71' then '臺灣'
            when '13' then '河北'
            when '35' then '福建'
            when '51' then '四川'
            when '81' then '香港'
            when '14' then '山西'
            when '36' then '江西'
            when '52' then '貴州'
    else '其他' end as addr
from idcard_tab


select
str_to_map('11=北京&33=浙江&46=海南&65=新疆&12=天津&34=安徽&50=重慶&71=臺灣&13=河北&35=福建&51=四川&81=香港&14=山西&36=江西&52=貴州', '&', '=')[substr(idcard,1,2)]
from idcard_tab

&分隔 key=value

get_json_object(string json_string, string path)

說明:
第一個參數填寫json對象變量,第二個參數使用$表示json變量標識,然後用 . 或 [] 讀取對象或數組;如果輸入的json字符串無效,那麼返回NULL。
每次只能返回一個數據項。

舉例:
data 爲 test表中的字段,數據結構如下:

data =
{
 "store":
        {
         "fruit":[{"weight":8,"type":"apple"}, {"weight":9,"type":"pear"}],  
         "bicycle":{"price":19.95,"color":"red"}
         }, 
 "email":"amy@only_for_json_udf_test.net", 
 "owner":"amy" 
}

1.get單層值

hive> select  get_json_object(data, '$.owner') from test;
結果:amy

2.get多層值.

hive> select  get_json_object(data, '$.store.bicycle.price') from test;
結果:19.95

3.get數組值[]

hive> select  get_json_object(data, '$.store.fruit[0]') from test;
結果:{"weight":8,"type":"apple"}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章