如何使用pig脚本获得两个epoch时间值之间的毫秒数

nvbavucw  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(472)

游戏|开始时间|结束时间

1 | 1235000140| 1235002457
2 | 1235000377| 1235003300
3 | 1235000414| 1235056128
1 | 1235000414| 1235056128
2 | 1235000377| 1235003300
这里我想得到两个epoch时间字段之间的毫秒,begintime和endtime。然后计算每场比赛的平均时间。

tct7dpnv

tct7dpnv1#

games = load 'games.txt' using PigStorage('|') as (gameid: int, begin_time: long, end_time:long);

dump games; 
(1,1235000140,1235002457)
(2,1235000377,1235003300)
(3,1235000414,1235056128)
(1,1235000414,1235056128)
(2,1235000377,1235003300)

第一步:计算时差

difference = foreach games generate gameid, end_time - begin_time as time_lapse;

dump difference;
(1,2317)
(2,2923)
(3,55714)
(1,55714)
(2,2923)

第二步:将游戏id上的数据分组

game_group = group difference by gameid;

dump game_group;
(1,{(1,55714),(1,2317)})
(2,{(2,2923),(2,2923)})
(3,{(3,55714)})

第三步:然后是平均值

average = foreach game_group generate group, AVG(difference.time_lapse);

dump average;
(1,29015.5)
(2,2923.0)
(3,55714.0)

相关问题