hive加载json数据和解析json

hive 对json数据的处理 hive 存储,解析,处理json数据 hive 处理json数据总体来说有两个方向的路走1、将json以字符串的方式整个入Hive表,然后通过使用UDF函数解析已经导入到hive中的数据,比如使用LATERAL VIEW json_tuple的方法,获取所需要的列名。2、在导入之前将json拆成各个字段,导入Hive表的数据是已经解析过得。这将需要使用第三方的SerDe。 测试数据为新浪微博测试... 阅读详情

 

2018年02月28日 13:27:58 阅读数:3472 标签: json  更多

个人分类: hive

事先说明,本人菜鸡一只,如果有说错的地方,还请大家指出批评,多多包涵~

一、

今天记录一下使用hive怎么加载json格式的数据,对于json格式,就不多做更多说明了,测试的数据是spark的example里面的people.json,数据很少,但是说明情况足矣。

先给出官网地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual%20DDL#LanguageManualDDL-JSON

数据是这样的:

 

{"name":"Michael"}

{"name":"Andy", "age":30}

{"name":"Justin", "age":19}

一个大括号是一个对象(一行数据),然后Michael这位小朋友age字段是缺失的。

1、接着开启hive,创建表(建表语句如下):

 create table spark_people_json( 

`name` string,

`age`   int)

ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'

STORED AS TEXTFILE;

2、但是这样直接使用JsonSerDe类,是会报错的,因为这个类并没有在初始化的时候加载到环境中

报错如下:

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Cannot validate serde: org.apache.hive.hcatalog.data.JsonSerDe

3、因此先执行

ADD JAR ${HIVE_HOME}/hcatalog/share/hcatalog/hive-hcatalog-core-0.13.1-cdh5.3.6.jar

注:${HIVE_HOME}是hive的根目录,自己对应替换,还有本文测试的hive是hive-0.13.1-cdh5.3.6这个版本的,不同版本这个jar可能会有点路径上的区别,但是理论上是能找得到的!实在不行可以在中央仓库找找:http://mvnrepository.com/

4、在环境中添加了jar之后,接着创建表,就可以成功创建了(hive加载不同的数据使用的分隔符可能是不同的类,可以研究下这些类是怎么写的,就可以写出符合自己需求的分割类,然后打成jar包上传服务器,add就可以了,不过大多数通用的数据类型都是有对应的别人写好的类的,例如csv/tsv的就叫org.apache.hadoop.hive.serde2.OpenCSVSerde)

5、创建好表之后,就load数据

load  data local inpath '/datas/people.json' into table spark_people_json;

6、最后进行select得到结果

7、字段缺失的值为NULL

 

二、

第二个是记录下如果只是某个字段为json,想要获取里面的某个值怎么操作?

有这么两个函数:get_json_object()和json_tuple()

1、get_json_object()

get_json_object函数第一个参数填写json对象变量,第二个参数使用$表示json变量标识,然后用 . 或 [] 读取对象或数组;

什么意思?

1、就是这样:

select get_json_object('{"shop":{"book":[{"price":43.3,"type":"art"},{"price":30,"type":"technology"}],"clothes":{"price":19.951,"type":"shirt"}},"name":"jane","age":"23"}', '$.shop.book[0].type')  

结果:

2、如果json简单,可以直接这样使用:

select get_json_object('{"name":"jack","server":"www.qq.com"}','$.server')

结果:

3、但是问题来了每次只能查一个字段

不信你可以试试:select get_json_object('{"name":"jack","server":"www.qq.com"}','$.server','$.name')

结果:

大概来说,意思是这个方法,只能接受两个参数,多的不行,那么就导致我们对同一个json数据想要查看多个值,只能多写几个get_json_object,比较麻烦,所以另一个方法就派上了用场。

4、json_tuple

使用方法:

select json_tuple('{"name":"jack","server":"www.qq.com"}','server','name')

结果:

5、但是缺点就是对于复杂的嵌套的json,就操作不了了(就是说使用不了".",“[]”这种符号来操作json对象),所以看情况选择这两个方法去使用。

OK,想记录的东西记录完了,祝大家春节快乐~

最后,本人菜鸡一个,如果有说的不对的地方,还望各位大神指点迷津!!

Hive Code2报错排查 大多数可能的code2报错一般是内存不够,所以加下面这个配置可以有效解决这个问题#取消小表加载至内存中但这个不一定是因为内存不够,其实很多错误都是报这种官方错误的,所以一定要去yarn上看日志。很多人看日志也找不到问题,因为并没有看到全部日志,翻到最底下,点击check here 才能看到完整日志。 阅读详情

相关推荐

【大数据入门核心技术-Hive】(十六)hive表加载csv格式数据或者json格式数据

三、hive加载CSV格式数据。

573

【Hive】hive加载json数据和解析json(一)

事先说明,本人菜鸡一只,如果有说错的地方,还请大家指出批评,多多包涵~ 本文有后续哦! 本文主要还是介绍hive中函数的解析json的函数get_json_object()和json_tuple()的使用,如果想通过serde直接加载hive数据,请看第二篇! 【Hive】hive加载json数据和解析json(二):https://blog.youkuaiyun.com/lsr40/articl...

lsr40的博客 6万+

python 携程登陆_Python 学习练习之用户登录接口

目的:用python实现简单用户注册,登陆,加黑功能,放个流程图吧。以下内容仅为练习 执行流程:[root@zabbix login]# python user_login.pyusername:tompassword:123456Are you want to register ? [y/n]yRegister successful,Please relogin ![root@zabbix lo...

weixin_28679635的博客 518

Hive load JSON数据类型

用以下文本举例: {"name":"Yin", "address":{"city":"Columbus","state":"Ohio"}} {"name":"Michael", "address":{"city":null, "state":"California"}} hdfs路径为:/user/maokaimin/jsontest/json.json 方法1: 建表 create external table maokaimin.jsontest (jsonObject string) lo

857技术社区 940

Web前端开发技术:实验14-16

实验14 1. 编写JavaScript程序实现“九九乘法口诀”表,如图14-40所示。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>九九乘法表</title> </head> <body> <h1>九九乘法表</h1> </body> <scri

KslgmrDry的博客 1万+

SPSS(二)SPSS实现多因素方差分析模型(图文教程+数据集)

SPSS(二)SPSS实现多因素方差分析模型 单因素方差分析上一篇博客https://blog.youkuaiyun.com/LuYi_WeiLin/article/details/89917656已经介绍完毕 这篇博客我们主要来学习多因素方差分析 多因素方差分析,就是同时考虑若干个控制因素的情况下,分别分析它们的改变是否造成观察变量的显著变动 (多个自变量,一个因变量)自变量类型以分类变量为主也可以...

可乐联盟 6万+

Apache Hive 加载JSON数据与JSON解析

首先Hive提供了三种方案(应用于不同的场景): 建表JsonSerDe 内置函数get_json_object 内置函数json_tuple 1.使用JsonSerDe创建表加载JSON文件 注意此种方式需要满足的条件: json文件必须是形如如下格式: 下面 xxx可以是对象,也可以是数组 { key1:xxxx, key2:xx, key3,xxx } 另外...

猫君之上的博客 595

NDVI空间分布变化Slope趋势分析与F检验(自用)

1.NDVI数据预处理2.一元线性回归分析3.Slope趋势分析与F检验

什么都学亿点点的博客 9020

【hive】hive加载json数据和解析json(二)

超久超久没写博客了,近来的都是比较杂的临时活,大家都在喊他的活很急,最要紧,挺令我心烦的,不过有活总比没活做好吧! 言归正传,本文讲的是hive解析json格式的数据 最近处理了这么一份数据,json格式的,其实我之前写了一篇博客: hive加载json数据和解析json(一):https://blog.youkuaiyun.com/lsr40/article/details/7939916...

lsr40的博客 3940

双层优化入门(1)—基本原理与求解方法(附matlab代码)

双层优化问题(Bilevel Programming Problems),也被称为双层规划,最早由Stackelberg与1934年在经济学相关研究中提出,因此也被称为Stackelberg问题。双层规划问题一般具有层次性、独立性、冲突性、优先性和自主性等特点。 本文介绍了双层优化的原理与求解方法,并提供了相应的matlab代码供参考学习。

配电网和matlab的博客 5万+

最新Docker GPU支持全解析:为什么nvidia-smi能用但PyTorch找不到CUDA?

本文深入解析了Docker容器中nvidia-smi命令可用但PyTorch无法调用CUDA的根本原因。关键在于默认配置仅暴露了GPU的utility管理能力,而CUDA计算所需的compute能力未被激活。通过正确设置NVIDIA_DRIVER_CAPABILITIES环境变量为compute,utility,并配合--gpus参数,即可解决此问题,确保AI训练等任务顺利进行。

convnet3designer的博客 242

hive:函数:get_json_object和json_tuple 操作json数据(hive加载json数据和解析json)

业务情景一: hive加载json数据到表中: linux本地创建文件people.json 数据为: {"name":"Michael"} {"name":"Andy", "age":30} {"name":"Justin", "age":19} 创建表: CREATE TABLE ods.spark_people_json ( `nam...

不花的花和尚的博客 7865

Windows Pychram部署Unet++过程

windows系统 在pycharm平台上配置并跑通Unet网络 这里我用的是B站课程里的资源《唐宇迪Unet图像分割语义分割实战》

ArcGis_Niu的博客 803

Hive函数:get_json_object和json_tuple 操作JSON数据(hive加载json数据和解析json)

业务情景一: hive加载json数据到表中: linux本地创建文件people.json 数据为: {"name":"Michael"} {"name":"Andy", "age":30} {"name":"Justin", "age":19} 创建表: CREATE TABLE ods.spark_people_json ( `name` ...

u012501054的博客 1232

【ESP32设备通信】-LoRa通信

在本文中,我们将使用 Arduino IDE 将 ESP32 与 LoRa SX1278 (Ra-02) 模块连接起来。 我们将创建一个真实的示例项目,通过 LoRa 无线打开/关闭 LED 进行演示。

视觉与物联智能 1891

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

大数据-230 离线数仓-ODS层的构建 Hive 2.3.7 JSON 解析实战:UDF 取数组 + explode 展开 vs JsonSerDe 建表加载

Hive 离线数仓中的 JSON 数据处理,覆盖三类最常见需求:1)从 JSON 串中取数组字段并在 SQL 中 explode 展开;2)从 JSON 串中同时解析普通字段(json_tuple)与数组字段(自定义 UDF);3)对“每行一个 JSON object”的原始文件,使用 JsonSerDe 直接建表加载。示例基于 Hive 2.3.7(hive-exec 2.3.7)与 Java UDF,实现输入(jsonStr、arrKey)输出 array,并配套 LATERAL VIEW

谢谢你的喜欢 我们一起无限进步 6096

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

大数据-229 离线数仓ODS 层实战:Hive 外部表分区加载与 JSON 解析(get_json_object/json_tuple)

离线数仓(2026)中 ODS(Operational Data Store)层的工程落地,给出 Hive 外部表(external table)+ 按天分区(partition dt)承接原始日志的最小闭环:建表 location 指向 HDFS 目录、通过 alter table add partition 管理分区,并用 Shell 脚本实现“传参日期/默认昨天”的装载流程。随后聚焦 Hive JSON 处理:get_json_object 适合单字段路径提取,json_tuple 适合一次取多个键(

谢谢你的喜欢 我们一起无限进步 9016

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

hive加载json数据解决方案

hive加载json数据解决方案

qq_38472451的博客 176

hive处理json数据_Hive Json 格式的数据处理(27)

Json 格式的数据处理Json 数据格式是我们比较常用的的一种数据格式,例如埋点数据、业务端的数据、前后端调用都采用的是这种数据格式,所以我们很有必要学习一下这种数据格式的处理方法准备数据cat json.data{"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}{"movie":"661","rate":"3","time...

weixin_42297810的博客 212

简单解析json

# json {"id":1,"age":18} # hive建表语句 create table json(name string); # 加载数据进 json 表 load data local inpath "/root/data/hive/a.txt" into table json; # 数据展示 +--------------------+ | json.name | ...

徐磊的博客 220

hive解析json格式数据

get_json_object函数使用 1)数据xjson [{"name":"大郎","sex":"男","age":"25"},{"name":"西门庆","sex":"男","age":"47"}] 2)取出第一个json对象 select get_json_object('[{"name":"大郎","sex":"男","age":"25"},{"name":"西门庆","sex":"男","age":"47"}]','$[0]'); 结果是:{"name":"大郎","sex":"男".

zcb_data的博客 1423

Hive优雅的处理Json数据

准备数据 1SOH{"name":"aaa","cource":"english","score":"90"},{"name":"bbb","cource":"chinese","score":"80"},{"name":"ccc","cource":"math","score":"70"} 2SOH{"name":"aaa","cource":"english","score":"90"}...

一名假的程序猿 376

hive json 获取KEY

如何实现“hive json 获取KEY” 一、整体流程 首先我们来看一下整个实现“hive json 获取KEY”的流程,可以通过以下表格展示: 步骤 操作 1 创建Hive表 2 加载JSON数据到Hive表 3 提取JSON中的KEY 二、具体操作步骤 1. 创建Hive表 首先,在Hiv...

weixin_40482767的博客 472

hive 导出json格式 文件,在HIVE表中加载JSON文件

I have a JSON file like below, which I want to load in a HIVE table with parsed format, what are possible options I can go for.If it is AVRO then I could have used directly AvroSerDe. But the source f...

weixin_39725885的博客 146

hive 到表之间加复制数据 和 hdfs上表导入本地数据 和本地数据导入到 hdfs上表中,和函数的使用

hive&gt; create table rat_json(line string) row format delimited;OKTime taken: 0.494 secondshive&gt; load data local inpath '/root/rating.json' into table rat_json;FAILED: SemanticException Line 1:23 ...

大JAVA解决方案 875
上一篇: hive中简单介绍分区表(partition table),含动态分区(dynamic partition)与静态分区(static partition)
下一篇: hive 之start hiveServer2 ,thriftServer失败
王树民
博客等级 码龄10年 912粉丝 560原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值