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. Android安装包相关知识汇总 (编译过程图给力)

    转自: https://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=208008519&idx=1&sn=278b7793699 ...

  2. Android MVP 构架初试

    目前讨论MVP MVVM 的架构也来越多,这种构架也很适合Android.研究MVP记录如下 源码地址RxMVP分支Tag02 原有的MVC构架 刚开始接触Android的时候会觉得Android的整 ...

  3. Cocos2d-x 2.x 升级为 3.x 常见变化纪录

    1.去CC 之前2.0的CC**,把CC都去掉,主要的元素都是保留的 2.0 CCSprite  CCCallFunc CCNode .. 3.0 Sprite CallFunc Node .. 2. ...

  4. 【Android实战】----基于Retrofit实现多图片/文件、图文上传

    本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...

  5. iOS XMPPFramework 环境配置

    iOS XMPPFramework 环境配置 1:下载 iOS XMPPFramework https://github.com/robbiehanson/XMPPFramework 2:下载解压zi ...

  6. 简单测试Demo:如何用Java压缩文件夹和文件

    一.直接贴出测试代码 package com.jason.zip; import java.io.File; import java.io.FileInputStream; import java.i ...

  7. Android下文件的压缩和解压(Zip格式)

    Zip文件结构 ZIP文件结构如下图所示, File Entry表示一个文件实体,一个压缩文件中有多个文件实体. 文件实体由一个头部和文件数据组,Central Directory由多个File he ...

  8. Window上python开发--4.Django的用户登录模块User

    Android系统开发交流群:484966421 OSHome. 微信公众号:oshome2015 在搭建站点和web的应用程序时,用户的登录和管理是差点儿是每一个站点都必备的. 今天主要从一个实例了 ...

  9. QtGui.QInputDialog

    The QtGui.QInputDialog provides a simple convenience dialog to get a single value from the user. The ...

  10. linux sheel重复执行上条命令

    Linux系统下Shell重复执行上条命令的 4 种方法: 1.使用上方向键,并回车执行. 2.按 !! 并回车执行. 3.输入 !-1 并回车执行. 4.按 Ctrl+P 并回车执行.