hive 將hive表數據查詢出來轉爲json對象和json數組輸出

1、將hive表數據查詢出來轉爲json對象輸出apache

一、將查詢出來的數據轉爲一行一行,並指定分割符的數據json

二、使用UDF函數,將每一行數據做爲string傳入UDF函數中轉換爲json再返回數組

一、準備數據函數

二、查詢出來的數據轉爲一行一行,並指定分割符的數據oop

 

三、準備UDF函數測試

package com.laotou; import org.apache.hadoop.hive.ql.exec.UDF; import org.json.JSONException; import org.json.JSONObject; /** * @Author: * @Date: 2019/8/9 */
public class HiveJsonOut extends UDF{public static String evaluate(String jsonStr) throws JSONException { String[] split = jsonStr.split(","); JSONObject result = new JSONObject(); result.put("key", split[0]); result.put("value", split[1]); return String.valueOf(result); } }

package com.laotou; import org.apache.hadoop.hive.ql.exec.UDF; import org.json.JSONException; import org.json.JSONObject; /** * @Author: * string轉json:{"notifyType":13,"notifyEntity":{"school":"小學","name":"張三","age":"13"}} * @Date: 2019/8/14 */
public class Record2Notify extends UDF { private static final String split_char = "!"; private static final String null_char = "\002"; public static String evaluate(int type, String line) throws JSONException { if (line == null) { return null; } JSONObject notify = new JSONObject(); JSONObject entity = new JSONObject(); notify.put("notifyType", type); String[] columns = line.split(split_char, -1); int size = columns.length / 2; for (int i = 0; i < size; i++) { String key = columns[i*2]; String value = columns[i*2+1]; if (isNull(key)) { throw new JSONException("Null key.1111111111"); } if (!isNull(value)) { entity.put(key, value); } } notify.put("notifyEntity", entity); return notify.toString(); } private static boolean isNull(String value) { return value == null || value.isEmpty() || value.equals(null_char); } public static void main(String[] args) throws JSONException { System.out.println(evaluate(13,"name!張三!age!13!school!小學")); } }

 

 

2、將hive表數據查詢出來轉爲json數組輸出lua

思路:spa

一、使用UDF函數(見上面內容)將查詢出來的每一條數據轉成json對象code

select getJsonOut(concat_ws(',',key,value)) as content from test1

二、將第一步查詢的結果進行列轉行,並設置爲逗號進行分割,獲得以下字符串對象

select concat_ws('!!',collect_list(bb.content)) as new_value from (select getJsonOut(concat_ws(',',key,value)) as content from test1) bb;

結果如圖:

三、使用UDF函數(JsonArray)將第2步中獲得的字符串放入數組對象,準備UDF函數

package com.laotou;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.json.JSONArray;
import org.json.JSONException;

/**
* create temporary function getJsonArray as 'com.laotou.HiveJson';
* @Author:
* @Date: 2019/8/9
*/
public class HiveJson extends UDF{
public static JSONArray evaluate(String jsonStr) throws JSONException {
String[] split = jsonStr.split("!!");
JSONArray jsonArray = new JSONArray();
jsonArray.put(split[0]);
jsonArray.put(split[1]);
jsonArray.put(split[2]);
jsonArray.put(split[3]);
return jsonArray;
}

}

 四、測試

select getJsonArray(new_value) from (select cast(concat_ws('!!',collect_list(bb.content)) as string) as new_value from (select getJsonOut(concat_ws(',',key,value)) as content from test1) bb) cc;
相關文章
相關標籤/搜索