一、将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. PyQt(Python+Qt)学习随笔:QTreeView树形视图的allColumnsShowFocus属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的allColumnsShowFocus属性用于控制是否使视图中的所有列显 ...

  2. python接口测试自动化框架-发送邮件,邮箱报错: 535 Error, authentication failed

    1.无意中把腾讯企业邮箱设置为安全登录,接口测试自动化发送邮件,不能被正常接收.错误信息为:535 Error, authentication failed. 原因:认证安全登录后,原来新的邮箱代码传 ...

  3. 小程序editor篇-基本使用图片上传

    今天小程序项目内,要弄一个editor,富文本编辑功能,支持图文并茂,前几天刚好看了小程序的demo应用,刚好看到editor这个东东,那就安排! 官网示例git地址 大概看了下文档,拉下官方示例,看 ...

  4. 【复习笔记】重习 AC 自动机

    发现已经忘了许多....于是复习一下 基础要点概况 AC 自动机基于 Trie 树 的结构,即构建 AC 自动机前需要先建 Trie. 一个状态中除了转移 \(\delta\) 之外还有失配指针 \( ...

  5. mysql创建表分区

    MySQL创建表分区 create table erp_bill_index( id int primary key auto_increment, addtime datetime ); inser ...

  6. 面试官:小伙子,你给我简单说一下RocketMQ 整合 Spring Boot吧

    前言 在使用SpringBoot的starter集成包时,要特别注意版本.因为SpringBoot集成RocketMQ的starter依赖是由Spring社区提供的,目前正在快速迭代的过程当中,不同版 ...

  7. STM32 GPIO输入输出(基于HAL库)

    一.基础认识 GPIO全名为General Purpose Input Output,即通用输入输出.有时候简称为"IO口".通用,说明它是常见的.输入输出,就是说既能当输入口使用 ...

  8. 20201126-1 txt文件筛选与读写【】

    Exercise 1import os # 设置文件夹路径为'工作文件夹',获取文件夹下的所有文件和文件夹名称 path = './工作文件夹/' files_list = os.listdir(pa ...

  9. github拉去代码慢的处理方式(最简单)

    https://github.com/xxx/xxxx 替换成 https://github.com.cnpmjs.org/xxx/xxxx 再去拉取,速度快很多,亲测可用

  10. 磁盘inode节点被占满的解决方法

    问题: Linux服务器,查看日志发现程序无法继续写文件,但是用df -h查看磁盘容量还有剩余. 排查思路:怀疑是机器的inode节点被占满,使用df -i查看磁盘inode节点使用情况,果然是ino ...