hive如何使用json格式的資料

2021-08-28 11:03:24 字數 2405 閱讀 8425

檢視當前hive支援的json函式

show functions like "*json*";

okget_json_object

json_tuple

time taken: 0.006 seconds, fetched: 2 row(s)

describe function get_json_object;

ok get_json_object(json_txt, path) - extract a json object from path

解析json的字串json_txt,返回path指定的內容。如果輸入的json字串無效,那麼返回null。

例如:ok

null

time taken: 0.219 seconds, fetched: 1 row(s)

沒有ownes,返回值是null

okamy

通過$.獲取對應key的value 值ok

ok[8,9]

效果如上,如果是巢狀json,也通過.獲取,如果並列結構獲取的值將是list的形式

desc function json_tuple;

okjson_tuple(jsonstr, p1, p2, ..., pn) - like get_json_object, but it takes multiple names and return a tuple. all the input parameters and output column types are string.

引數為一組鍵p1,p2……和json字串,返回值的元組。該方法可以在一次呼叫中輸入多個鍵ok

可以通過 「store」,「email」,「owner」 獲取三個key 對應的value值,相比get_json_object顯得更為高效率

> ;

oknull amy@only_for_json_udf_test.net amy

可以看出。不能通過.獲取件套資料對應的值,如果key不存在,將返回null。

要獲取json各列的資料的方法

raw001	  

raw002

raw003

---------------------

select t.cl1 , get_json_object(t.cl2json,'$.age'), get_json_object(t.cl2json,'$.gender') from tmp_json_test t ;
select t1.cl1, t2.* from tmp_json_test t1 lateral view json_tuple(t1.cl2json, 'age', 'gender') t2 as c1, c2;
上面這種方式不能處理複雜型別(如果hive表中字段為array,map等),並且處理上非常麻煩,要寫好長的sql。

還有一種對於資料是整個json格式的情況,相比第一種來說更靈活,更通用。重要的是每行必須是乙個完整的json,乙個json不能跨越多行,也就是說,serde不會對多行的json有效。 因為這是由hadoop處理檔案的工作方式決定,檔案必須是可拆分的,基於次開源的序列化工具hive-json-serde使用起來就顯得特別方便了

hive.aux.jars.path

auxlib/json-serde-1.3.8-jar-with-dependencies.jar

注意之前版本有bug,在使用hiveserver2的時候解析會實效,有型別轉化的錯誤。

注意建表的時候 需要指定序列化對應的方式org.openx.data.jsonserde.jsonserde

row format serde 

'org.openx.data.jsonserde.jsonserde'

with serdeproperties (

'ignore.malformed.json'='true',

對應map這種 在union的時候,如果欄位為null 如何拼接呢

mapstr_to_map(text[, delimiter1, delimiter2])

splits text into key-value pairs using two delimiters. delimiter1 separates text into k-v pairs, and delimiter2 splits each k-v pair. default delimiters are 『,』 for delimiter1 and 『=』 for delimiter2.

select str_to_map(「a:a&b:b」, 『&』, 『:』);

ok

hive儲存複雜的json格式

1.hive復合資料型別 array data type map primitive type,data type struct col name data type comment col comment 2.json建構於兩種結構 名稱 值 對的集合 a collection of name v...

hive基於json格式建立hive表

由於前段的資料是json格式的,因此想基於json建表。hive裡是使用命令新增jar包 add jar hdfs user hive jars hive hcatalog core 1.1.0 cdh5.13.3.jar 新增了之後便可根據json的內容建表了 基於sjon檔案建表 單條json文...

Hive處理json格式資料

1 資料示例 假設info表中存有兩個字段,分別是id,content content 2 使用hive內建函式 get json object json tuple regexp replace 3 說明 使用get json object 解析content,獲取properties,使用reg...