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. KRBTabControl(中文)Windows选项卡控件

    本文阐述了如何在C#使自定义Windows选项卡控件. Download demo project - 82.4 KB Download source - 252 KB 介绍 本文讨论如何使用.NET ...

  2. 【python】如何安装requests

    在cmd窗口输入pip install requests 即可,如下 C:\Users\horn1\Desktop\python\4>pip install requestsCollecting ...

  3. 搜集整理一些Cron表达式例子

    1.cronExpression配置说明 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / ...

  4. C-IDE使用指南

    HI  您好: 亲爱的学员,本文章是基于C-IDE的入口使用指南,您能够查看demo项目来了解C-IDE详细操作哦~ 如有疑问您可提交反馈来咨询,或扫描下方二维码增加官方微信群.我们会认真对待且具体回 ...

  5. Linux阅读笔记(一)

    1.关机命令 shutdown -h now             马上关机 shutdown -r now              马上重新启动 reboot                   ...

  6. Linux 监测 常用测试工具

    fio [global]bs=16kdirect=1rw=readioengine=libaioiodepth=6write_bw_logruntime=60[test]filename=/data/ ...

  7. test推荐到极客头条002

    test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002te ...

  8. 不能使用控制器“XXXController”的单个实例处理多个请求。如果正在使用自定义控制器工厂,请确保它为每个请求创建该控制器的新实例。

    原因:应用@{Html.RenderAction("aaa","XXX");}时路径路由和动作控制器不能是相同的,不然会的错. 比如:http://localh ...

  9. mmap 函数

    头文件:#include <unistd.h>    #include <sys/mman.h> 定义函数:void *mmap(void *start, size_t len ...

  10. Eclipse+PyDev 安装和配置(转)

    Python开发有很多工具,其中Eclipse+Pydev 是最常见的一种.本文简单介绍Windows下Eclipse+PyDev 安装和配置. Eclipse 是一种基于 Java 的可扩展开源开发 ...