Hive 选择数据到结构数组中 [英] Hive select data into an array of structs

查看:30
本文介绍了Hive 选择数据到结构数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Hive 中找出一种方法来从平面源中选择数据并输出到命名结构数组中.这是我正在寻找的示例...

I am trying to figure out a way in Hive to select data from a flat source and output into an array of named struct(s). Here is a example of what I am looking for...

样本数据:

house_id,first_name,last_name
1,bob,jones
1,jenny,jones
2,sally,johnson
3,john,smith
3,barb,smith

期望的输出:

1   [{"first_name":"bob","last_name":"jones"},{"first_name":"jenny","last_name":"jones"}]
2   [{"first_name":"sally","last_name":"johnson"}]
3   [{"first_name":"john","last_name":"smith"},{"first_name":"barb","last_name":"smith"}]

我尝试了 collect_list 和 collect_set 但它们只允许原始数据类型.关于我如何在 Hive 中处理这个问题的任何想法?

I tried collect_list and collect_set but they only allow primitive data types. Any thoughts of how I might go about this in Hive?

推荐答案

我会用这个 jar,它是 collect 更好的实现(并采用复杂的数据类型).

I would use this jar, it is a much better implementation of collect (and takes complex datatypes).

查询:

add jar /path/to/jar/brickhouse-0.7.1.jar;
create temporary function collect as 'brickhouse.udf.collect.CollectUDAF';

select house_id
  , collect(named_struct("first_name", first_name, "last_name", last_name))
from db.table
group by house_id

输出:

1   [{"first_name":"bob","last_name":"jones"}, {"first_name":"jenny","last_name":"jones"}]
2   [{"first_name":"sally","last_name":"johnson"}]
3   [{"first_name":"john","last_name":"smith"},{"first_name":"barb","last_name":"smith"}]

这篇关于Hive 选择数据到结构数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆