考慮一個簡單的json:
{"openid":"a43dd9f","unionid":"cae4332f"}
在hive中如何提取出這個json的key呢?一個思路是利用正則表達式替換,將非key的部分去除並設置一個分割符,最后切分這個字符串就可以得到json的key,如有特殊需要還可利用explode函數,將結果轉為多行。
select explode( split( substr( regexp_replace( '{"openid":"a43dd9f","unionid":"cae4332f"}', '.*?"(\\w+)":".*?"\}?', ',$1' ), 2 ), ',' ) );
結果: