一、将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数组输出的更多相关文章

  1. Hive的join表连接查询的一些注意事项

    Hive支持的表连接查询的语法: join_table: table_reference JOIN table_factor [join_condition] | table_reference {L ...

  2. SQL Server 的表数据简单操作(表数据查询)

    --表数据查询----数据的基本查询-- --数据简单的查询--select * | 字段名[,字段名2, ...] from 数据表名 [where 条件表达式] 例: use 商品管理数据库 go ...

  3. 10Oracle Database 数据表数据查询

    Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...

  4. MySQL多表数据查询(DQL)

    数据准备: /* ------------------------------------创建班级表------------------------------------ */ CREATE TAB ...

  5. MySQL单表数据查询(DQL)

    数据准备工作: CREATE TABLE student( sid INT PRIMARY KEY AUTO_INCREMENT, sname ), age TINYINT, city ), scor ...

  6. MySQL学习总结(五)表数据查询

    查询数据记录,是指从数据库对象表中获取所要查询的数据记录,该操作可以说是数据最基本的操作之一,也是使用频率最高.最重要的数据操作. 1.单表数据记录查询 1.1.简单数据查询 SELECT field ...

  7. mybatis多表关联查询之resultMap单个对象

    resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...

  8. SpringBoot之封装json对象返回json数据

    /** * @description:封装json对象,所有返回结果都使用它 **/ public class Result<T> { private int code;// 业务自定义状 ...

  9. Hive(五)【DQL数据查询】

    目录 一. 基本查询 1.1 算数运算符 1.2 常用聚合函数 1.3 limit 1.4 where 1.5 比较运算符(between|in|is null) 1.6 LIKE和RLIKE 1.7 ...

随机推荐

  1. 老猿Python部分代码样例

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 PyQt编程实战:通过eventFilter监视QScrollArea的widget()的Paint ...

  2. 代码审计系列题目CTFD部署(上)

    关于简单部署题目请参考:https://www.cnblogs.com/Cl0ud/p/13783325.html 如果需要进行较复杂部署,可参考本篇 PHP代码审计系列题目的部署,较之前的部署方案, ...

  3. 分布式计算框架-MapReduce 基本原理(MP用于分布式计算)

    hadoop最主要的2个基本的内容要了解.上次了解了一下HDFS,本章节主要是了解了MapReduce的一些基本原理. MapReduce文件系统:它是一种编程模型,用于大规模数据集(大于1TB)的并 ...

  4. Dell R740 使用U盘安装 CentOS7.4 出现Warning:dracut-initqueue timeout - starting timeout scripts解决办法

    使用使用UltraISO软碟通刻录U盘,然后在Dell R740服务器安装CentOS7.4会出现如下错误: 解决办法: 1.使用blkid确认U盘的盘符,截图如下: 2.按F11键重启 3.进入启动 ...

  5. 题解 CF504E 【Misha and LCP on Tree】

    PullShit 倍增和树剖的差距!!! 一个 TLE, 一个 luogu 最优解第三!!! 放个对比图(上面倍增,下面轻重链剖分): 不过这是两只 log 非正解... Solution \(LCP ...

  6. 零基础学习python 你该怎么做

    本人文科生,回顾自己近 2 年的Python 自学经历,有一些学习心得和避坑经验分享给大家,让大家在学习 Python 的过程中少走一些弯路!减少遇到不必要的学习困难! 首先,最开始最大的困难应该就是 ...

  7. Springboot 项目 无法读取resources下的mapper文件夹的.xml文件

    之前学习的时候遇到的一个问题 org.springframework.beans.factory.BeanCreationException: Error creating bean with nam ...

  8. 详解汇编语言B和LDR指令与相对跳转和绝对跳转的关系

    @ 目录 为什么要有相对跳转和绝对跳转? 在程序中只有相对跳转/绝对跳转是否可以? B(BL)和LDR指令具体怎么执行的? B(BL)和LDR跳转范围是如何规定的? 为什么要有相对跳转和绝对跳转? 顺 ...

  9. 手把手教你配置KVM服务器

    1 Ubuntu系统安装 1.1 制作启动盘 准备一个U盘,将其清空后,去官网下载Ubuntu18.04系统的iso镜像文件,并将其拷进U盘.然后下载一个UltralOS软碟通工具,完成安装后打开软碟 ...

  10. ThreadLocal使用总结

    一.ThreadLocal基本使用 1.1使用场景 不同的线程对全局变量进行修改,使用ThreadLocal后会为每个线程创建一个副本,而不会影响其他线程的副本,确保了线程的安全 private st ...