首页 技术 正文
技术 2022年11月14日
0 收藏 916 点赞 3,184 浏览 2566 个字

一、将hive表数据查询出来转为json对象输出

1、将查询出来的数据转为一行一行,并指定分割符的数据

2、使用UDF函数,将每一行数据作为string传入UDF函数中转换为json再返回

1、准备数据

2、查询出来的数据转为一行一行,并指定分割符的数据

3、准备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!小学"));
}
}

二、将hive表数据查询出来转为json数组输出

思路:

1、使用UDF函数(见上面内容)将查询出来的每一条数据转成json对象

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

2、将第一步查询的结果进行列转行,并设置为逗号进行分割,得到如下字符串

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

结果如图:

3、使用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;
}

}

4、测试

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;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295