json导入Hive,并整理成大宽表

2024-05-15 01:58

本文主要是介绍json导入Hive,并整理成大宽表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 1 创建表
    • 2 导入Hive
    • 3 json_tuple查询数据
    • 4 整理成大宽表

首先我们有如下图的json数据,我们需要把这份数据先导入到Hive,然后在整理成结构化的数据,这样我们就可以根据需求查询对应的数据了
在这里插入图片描述

1 创建表

首先先要创建一个表

create table rating(json string);

2 导入Hive

然后把数据导入到hive中

load data local inpath '/home/hadoopadmin/rating.json' into table rating;

查看数据,已经导入到hive中
在这里插入图片描述

3 json_tuple查询数据

但是上面的数据格式不是我们想要的,我们想要的数据格式为下面这种结构:

movieratetimeuserid
119359783007601

hive中有个json_tuple函数,官方语法:

json_tuple(string jsonStr,string k1,...,string kn)
#jsonStr:一个json字符串
#k1...kn:json字符串中的key

我们举个例子:

select json_tuple(
'{"movie":"1193","rate":"5","time":"978300760","userid":"1"}',
'movie','rate','time','userid');
OK
#结果
c0      c1      c2      c3
1193    5       978300760       1

上面结果中别名我们需要改下

select json_tuple(
'{"movie":"1193","rate":"5","time":"978300760","userid":"1"}',
'movie','rate','time','userid') as (movie, rate, time, user_id);
OK
#结果
movie   rate    time    user_id
1193    5       978300760       1

那么下面,我们只要把上面的json字符串改成表的字段json,然后从rating表中查询即可

select 
json_tuple(json,'movie','rate','time','userid') as (movie, rate, time, user_id) 
from rating limit 10 ;

如下图,我们通过json_tuple函数,把json数据结构,改成了结构化数据格式
在这里插入图片描述

4 整理成大宽表

上面的数据正常不会满足我们的需求,假如我们需要查询某个时间的信息,还需要其他的一些信心,例如下面这个格式,也就是常说的大宽表:

movieratetimeuseridyearmonthdayhourminutets
1193597830076012011116122001-01-01 06:12:40

目前我们有的参数是一个字符串的time,我们先要把time字符串转换成整数,然后再把整数转换成时间格式,这两个转换用到下面两个函数:
函数官网:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions

#把表达式转成想要的类型
cast(expr as <type>)
#把bigint的时间类型,转成想要的格式
from_unixtime(bigint unixtime[, string format])

针对上面2个函数,我们举个例子:

select cast('978300760' as bigint);

在这里插入图片描述

select from_unixtime(cast('978300760' as bigint));

在这里插入图片描述
后面我们只需要,把第三步的查询结果作为自查询,然后用上面2个函数去解析time字段就ok了

select movie,rate,time,user_id,
from_unixtime(cast(time as bigint)) as ts
from
(
select 
json_tuple(json,'movie','rate','time','userid') as (movie, rate, time, user_id) 
from rating  
) t
limit 10;

在这里插入图片描述
再通过下面这些函数,获取ts对应的年、月、日、时、分,就完成了大宽表
在这里插入图片描述

select movie,rate,time,user_id,
from_unixtime(cast(time as bigint)) as ts,
year(from_unixtime(cast(time as bigint))) as year,
month(from_unixtime(cast(time as bigint))) as month,
day(from_unixtime(cast(time as bigint))) as day,
hour(from_unixtime(cast(time as bigint))) as hour,
minute(from_unixtime(cast(time as bigint))) as minute
from
(
select 
json_tuple(json,'movie','rate','time','userid') as (movie, rate, time, user_id) 
from rating  
) t
limit 10;

如下图,查询出我们想要的大宽表了
在这里插入图片描述
但是只是查询出来还不行,我们不能每次要查一个需求的时候,我就用一下上面那么一大坨sql,我们可以把查询出来的大宽表再生成一张表,然后针对这个表做一些业务的处理:

create table rating_width
as
select movie,rate,time,user_id,
from_unixtime(cast(time as bigint)) as ts,
year(from_unixtime(cast(time as bigint))) as year,
month(from_unixtime(cast(time as bigint))) as month,
day(from_unixtime(cast(time as bigint))) as day,
hour(from_unixtime(cast(time as bigint))) as hour,
minute(from_unixtime(cast(time as bigint))) as minute
from
(
select 
json_tuple(json,'movie','rate','time','userid') as (movie, rate, time, user_id) 
from rating  
) t;

生成大宽表之后,我们查询看一下:

select * from rating_width limit 10;

如下图,已经生成我们需要的大宽表了
在这里插入图片描述

这篇关于json导入Hive,并整理成大宽表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/990485

相关文章

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

rtmp流媒体编程相关整理2013(crtmpserver,rtmpdump,x264,faac)

转自:http://blog.163.com/zhujiatc@126/blog/static/1834638201392335213119/ 相关资料在线版(不定时更新,其实也不会很多,也许一两个月也不会改) http://www.zhujiatc.esy.es/crtmpserver/index.htm 去年在这进行rtmp相关整理,其实内容早有了,只是整理一下看着方

笔记整理—内核!启动!—kernel部分(2)从汇编阶段到start_kernel

kernel起始与ENTRY(stext),和uboot一样,都是从汇编阶段开始的,因为对于kernel而言,还没进行栈的维护,所以无法使用c语言。_HEAD定义了后面代码属于段名为.head .text的段。         内核起始部分代码被解压代码调用,前面关于uboot的文章中有提到过(eg:zImage)。uboot启动是无条件的,只要代码的位置对,上电就工作,kern

JavaScript整理笔记

JavaScript笔记 JavaScriptJavaScript简介快速入门JavaScript用法基础语法注释关键字显示数据输出innerHTML innerText属性返回值的区别调试 数据类型和变量数据类型数字(Number)字符串(String)布尔值(Boolean)null(空值)和undefined(未定义)数组(Array)对象(Object)函数(Function) 变量

php中json_decode()和json_encode()

1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行编码 说明 mixed json_decode ( string $json [, bool $assoc ] ) 接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 参数 json

struts2中的json返回指定的多个参数

要返回指定的多个参数,就必须在struts.xml中的配置如下: <action name="goodsType_*" class="goodsTypeAction" method="{1}"> <!-- 查询商品类别信息==分页 --> <result type="json" name="goodsType_findPgae"> <!--在这一行进行指定,其中lis是一个List集合,但

如何导入sun.misc.BASE64Encoder和sum.misc.BASE64Decoder

右击项目名--->Build Path--->Configure Build Path...--->java Build Path--->Access rules:1 rule defined,added to all librar...   --->Edit --->Add...

mysql中导入txt文件数据的操作指令

1 表tt的格式:    CREATE TABLE `tt` (   `ind` int NOT NULL auto_increment,   `name` char(100) default NULL,   PRIMARY KEY  (`ind`)  )   2 文件d.txt的内容示例:  1,a  2,b  3,c

关于回调函数和钩子函数基础知识的整理

回调函数:Callback Function 什么是回调函数? 首先做一个形象的比喻:   你有一个任务,但是有一部分你不会做,或者说不愿做,所以我来帮你做这部分,你做你其它的任务工作或者等着我的消息,但是当我完成的时候我要通知你我做好了,你可以用了,我怎么通知你呢?你给我一部手机,让我做完后给你打电话,我就打给你了,你拿到我的成果加到你的工作中,继续完成其它的工作.这就叫回叫,手机