2

我有以下数据:

================================================================
session_id                       screen_name  screen_launch_time
================================================================
990004916946605-1404157897784    screen1      1404157898275
990004916946605-1404157897784    screen2      1404157898337
990004947764274-1435162269418    screen1      1435162274044
990004947764274-1435162269418    screen3      1435162274081

我想使用一个array_agg函数来获取以下格式的数据:

=========================================================
session_id                      screen_flow         count
=========================================================
990004916946605-1404157897784   screen1->screen2    1
990004947764274-1435162269418   screen1->screen3    1

有没有人尝试编写一个UDAFpython脚本来实现array_agg函数中使用的逻辑?

请分享您的想法。

4

2 回答 2

3

只需按session_id、 concat分组screen_name,然后计算每组的记录。如果不想造砖房罐子,可以用collect_list()代替collect()(但我不推荐)。

查询

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

select session_id, screen_flow
  , count(*) count
from (
  select session_id
    , concat_ws('->', collect(screen_name)) screen_flow
  from db.table
  group by session_id ) x
group by session_id, screen_flow

输出

990004916946605-1404157897784   screen1->screen2    1
990004947764274-1435162269418   screen1->screen3    1
于 2015-09-18T22:08:35.373 回答
1

输入 :-

990004916946605-1404157897784,screen1,1404157898275
990004916946605-1404157897784,screen2,1404157898337
990004947764274-1435162269418,screen1,1435162274044
990004947764274-1435162269418,screen3,1435162274081

以下是猪风格的答案..

records = LOAD '/user/user/inputfiles/session_id.txt' USING PigStorage(',') AS (session_id:chararray,screen_name:chararray,screnn_launch_time:chararray);

rec_grped = GROUP records BY session_id;

rec_each =  FOREACH rec_grped 
                     {
                       rec_inner_each = FOREACH records GENERATE screen_name;

                       GENERATE group as session_id, REPLACE(BagToString(rec_inner_each),'_','-->') as screen_flow, 1 as cnt;
};

dump rec_each; 

输出 :-

990004916946605-1404157897784   screen1-->screen2    1
990004947764274-1435162269418   screen1-->screen3    1
于 2015-09-19T09:47:46.740 回答