hive 将hive表数据查询出来转为json对象和json数组输出
一、将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;
hive 将hive表数据查询出来转为json对象和json数组输出的更多相关文章
- Hive的join表连接查询的一些注意事项
Hive支持的表连接查询的语法: join_table: table_reference JOIN table_factor [join_condition] | table_reference {L ...
- SQL Server 的表数据简单操作(表数据查询)
--表数据查询----数据的基本查询-- --数据简单的查询--select * | 字段名[,字段名2, ...] from 数据表名 [where 条件表达式] 例: use 商品管理数据库 go ...
- 10Oracle Database 数据表数据查询
Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...
- MySQL多表数据查询(DQL)
数据准备: /* ------------------------------------创建班级表------------------------------------ */ CREATE TAB ...
- MySQL单表数据查询(DQL)
数据准备工作: CREATE TABLE student( sid INT PRIMARY KEY AUTO_INCREMENT, sname ), age TINYINT, city ), scor ...
- MySQL学习总结(五)表数据查询
查询数据记录,是指从数据库对象表中获取所要查询的数据记录,该操作可以说是数据最基本的操作之一,也是使用频率最高.最重要的数据操作. 1.单表数据记录查询 1.1.简单数据查询 SELECT field ...
- mybatis多表关联查询之resultMap单个对象
resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...
- SpringBoot之封装json对象返回json数据
/** * @description:封装json对象,所有返回结果都使用它 **/ public class Result<T> { private int code;// 业务自定义状 ...
- Hive(五)【DQL数据查询】
目录 一. 基本查询 1.1 算数运算符 1.2 常用聚合函数 1.3 limit 1.4 where 1.5 比较运算符(between|in|is null) 1.6 LIKE和RLIKE 1.7 ...
随机推荐
- Python特殊序列\d能匹配哪些数字?
在缺省语言环境下,老猿对\d的匹配范围做了个测试,下面的数字包含半角数字.全角数字.中文数字,测试语句如下: >>> m=re.search(r'(\d*)(\D*)(\d*)',' ...
- [ASIS 2019]Unicorn shop
点击进去之后是一个购买独角兽的界面,有四种类型的独角兽,前三种的价格比较便宜,最后的独角兽价格比较贵. 我们先尝试购买前三种独角兽,输入id,然后price输入9 然后就告诉我商品错了,可能复现靶场这 ...
- jenkins+git部署环境,出现Failed to connect to repository : Command "git ls-remote -h http://gitlab.xxxxx.git HEAD" returned status code 128stdout: stderr: fatal: repository 'http://gitlab.xxxxx.git' not fou
1.部署jenkins+git源码管理的方式,源码管理报128stdout 源码管理出现如下错误: Failed to connect to repository : Command "gi ...
- js- 实现属性名的拼接 obj['name']
obj.name---->obj[name] 这两种调用方式一样,使用obj.name内部转换成 obj['name'], 使用obj['name']更快. obj['name'] 里面必须是 ...
- 最简 Spring AOP 源码分析!
前言 最近在研究 Spring 源码,Spring 最核心的功能就是 IOC 容器和 AOP.本文定位是以最简的方式,分析 Spring AOP 源码. 基本概念 上面的思维导图能够概括了 Sprin ...
- 题解-Railgun
题面 Railgun \(T\) 组测试数据,每次给定 \(n,k\),求(\(F(i)\) 为斐波那契数列第 \(i\) 项): \[\sum_{1\le x_i\le n(1\le i\le k) ...
- oracle 时间段查询
<select id="selectByRzrq" resultMap="BaseResultMap" parameterType="java. ...
- easyUI验证框赋值
下面来看看easyui的各种验证框赋值的方式: <input name="userId" id="userId" class="easyui-n ...
- css进阶 04-如何让一个元素水平垂直居中?
04-如何让一个元素水平垂直居中? #前言 老板的手机收到一个红包,为什么红包没居中? 如何让一个子元素在父容器里水平垂直居中?这个问题必考,在实战开发中,也应用得非常多. 你也许能顺手写出好几种实现 ...
- Spring Data JPA 整合Spring
1.1 Spring Data JPA 与 JPA和hibernate之间的关系 JPA是一套规范,内部是有接口和抽象类组成的.hibernate是一套成熟的ORM框架,而且Hibernate实现 ...