hive 解析 json 字符串

json_tuple (推荐用这个)

  1. json_tuple udtf 功能是在 hive 0.7 版本之后加入的,具体使用方法:
select a.timestamp, b.*
from log a lateral view json_tuple(a.appevent, 'eventid', 'eventname') b as f1, f2;

get_json_object

  1. 基本使用方法
select get_json_object(src_json.json,'[key]') from src_json;
  1. 可以使用某些JSONPath
  • $ : Root object
  • . : Child operator
  • [] : Subscript operator for array
    • : Wildcard for []
+----+
                               json
+----+
{"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"
}
+----+
hive> SELECT get_json_object(src_json.json, '$.owner') FROM src_json;
amy
 
hive> SELECT get_json_object(src_json.json, '$.store.fruit\[0]') FROM src_json;
{"weight":8,"type":"apple"}
 
hive> SELECT get_json_object(src_json.json, '$.non_exist_key') FROM src_json;
NULL

参考文献: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-get_json_object

你可能感兴趣的:(hive 解析 json 字符串)