1. 数据源信息

{"student": {"name":"king","age":11,"sex":"M"},"sub_score":[{"subject":"语文","score":80},{"subject":"数学","score":80},{"subject":"英语","score":80}]}
{"student": {"name":"king1","age":11,"sex":"M"},"sub_score":[{"subject":"语文","score":81},{"subject":"数学","score":80},{"subject":"英语","score":80}]}
{"student": {"name":"king2","age":12,"sex":"M"},"sub_score":[{"subject":"语文","score":82},{"subject":"数学","score":80},{"subject":"英语","score":80}]}
{"student": {"name":"king3","age":13,"sex":"M"},"sub_score":[{"subject":"语文","score":83},{"subject":"数学","score":80},{"subject":"英语","score":80}]}
{"student": {"name":"king4","age":14,"sex":"M"},"sub_score":[{"subject":"语文","score":84},{"subject":"数学","score":80},{"subject":"英语","score":80}]}
{"student": {"name":"king5","age":15,"sex":"M"},"sub_score":[{"subject":"语文","score":85},{"subject":"数学","score":80},{"subject":"英语","score":80}]}
{"student": {"name":"king5","age":16,"sex":"M"},"sub_score":[{"subject":"语文","score":86},{"subject":"数学","score":80},{"subject":"英语","score":80}]}
{"student": {"name":"king5","age":17,"sex":"M"},"sub_score":[{"subject":"语文","score":87},{"subject":"数学","score":80},{"subject":"英语","score":80}]}

2. 创建hive表

分析数据源,由于是json格式,

student字段使用map结构,sub_score字段使用array嵌套map的格式,

这样使用的好处是如果数据源中只要第一层字段不会改变,都不会有任何影响,兼容性较强。

创建表语句如下, 注意使用下面这个json包,这样解析json出错时不至于程序挂掉。

下载地址:

https://github.com/rcongiu/Hive-JSON-Serde

http://www.congiu.net/hive-json-serde/

create external table if not exists dw_stg.stu_score(
student map<string,string> comment "学生信息",
sub_score array<map<string,string>> comment '成绩表'
)
comment "学生成绩表"
row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
stored as textfile;

对于解析异常时报错的处理,可以加上一下属性:

ALTER TABLE dw_stg.stu_score SET SERDEPROPERTIES ( "ignore.malformed.json" = "true");

3. 上传数据

将score.txt数据上传到hive表stu_score目录:

hdfs dfs -put score.txt hdfs://dwtest-name1:9000/user/hive/warehouse/dw_stg.db/stu_score/

4. 数据查询

1)普通查询

2)查询单个学生的成绩

3)行转列explode ★★★

select explode(sub_score) from stu_score where student['name'] = 'king1';

4)更高级的写法:行转列lateral view .... explode ★★★

当使用explode时,不支持使用其他字段,如下会报错

所以使用另外一种用法

select student['name'],score['subject'],score['score']
from stu_score
lateral view explode(sub_score) sc as score
where student['name'] = 'king1';

5)保留null字段值 。格式 lateral view outer explode(field) 

如果数据源中学生分数为空时,在查询时可能就不会显示出来。比如下面的数据中,小明没有成绩。

使用4)中的查询显示如下:

此时,如果希望将小明也显示出来,则可以使用 lateral view outer explode(field) 格式。

select student['name'],score
from stu_score
lateral view outer explode(sub_score) sc as score

或者下面

通过3)、4)、5)步骤基本可以实现所有字段的任意查询和使用了。

hive中array嵌套map以及行转列的使用的更多相关文章

  1. Javascript中Array.prototype.map()详解

    map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...

  2. pandas中获取数据框的行、列数

    获取数据框的行.列数 # 获取行数 df.shape[0] # 获取行数 len(df) # 获取列数 df.shape[1]

  3. hive中同列多行数据组合的方法以及array to string要点(行转列)

    1. 同列多行数据组合成一个字段cell的方法, top N 问题的hive方案 如下: hive 列转行 to json与to array list set等复杂结构,hive topN的提取的窗口 ...

  4. java中Array/List/Map/Object与Json互相转换详解

    http://blog.csdn.net/xiaomu709421487/article/details/51456705 JSON(JavaScript Object Notation): 是一种轻 ...

  5. js中Array的map()函数,其中的回调函数还能这么用

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>菜鸟教程 ...

  6. java中Array/List/Map/Object与Json互相转换详解(转载)

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...

  7. js中array(数组).map

    使用前 使用后 代码:

  8. MySql 行转列 存储过程实现

    同学们在使用mysql的过程中,会遇到一个行转列的问题,就是把多条数据转化成一条数据 用多列显示. 方法1. 实现方式用下面的存储过程,表名对应的修改就行. BEGIN declare current ...

  9. oracle行转列、列转行、连续日期数字实现方式及mybatis下实现方式

    转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9977591.html 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...

随机推荐

  1. [Grunt] grunt.template

    /** * Created by Answer1215 on 11/15/2014. */ module.exports = function(grunt){ grunt.initConfig({ f ...

  2. 谋哥:《App自推广》开篇之回到远古人类

    [谋哥每天一干货.第六十八篇] 这两天帮谋天团的杨整体验他的App--"闪聊"的内測新版,改版后这款App命名为"美丫",一款致力于打造国内首款专注于女性社交的 ...

  3. PHP超过三十秒怎么办Maximum execution time of 30 seconds exceeded

    1 如图所示, Maximum execution time of 30 seconds exceeded 2 在php.ini文件中查找"max_execution_time"把 ...

  4. Poj-1274-The Perfect Stall-匈牙利算法

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19174   Accepted: 869 ...

  5. thunder 更改迅雷默认播放器

      更改迅雷的边下边播默认播放器 CreateTime--2017年10月31日08:33:57 Author:Marydon 1.找到迅雷的安装目录,如:D:\SoftWares\PortableE ...

  6. 本地ubuntu下pycharm 如何利用远程开发环境时显示图片

    最近使用pycharm远程开发tensorflow,每次在想显示图像时,苦于不知怎么操作,就通过保存后再看结果,使得调试很不方便.今天打算解决这个问题,收获也是很多啊. 我首先参考了这两篇博客: ht ...

  7. 一个Keygen,参考参考

    看到一个Keygen,我觉得还可以,参考参考 //////////////////////////////////////////////////////////////////// //// key ...

  8. numpy.argmin 使用

    https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.argmin.html numpy.argmin(a, axis=N ...

  9. unity, 设置帧率上限

    用unity做了个demo,把所有开销大的特效都去了,在真机上运行仍然卡.显示帧率来看,最高到30.原来unity在ios设备上帧率默认限制为不超过30. 可以通过Application.target ...

  10. 取得COM对象的UUID并以string输出

    IID tmp = __uuidof(ClassLibrary1::Class1); OLECHAR * buf; StringFromIID(tmp, &buf); 好像要#include ...