我的结构记录如下:
"event" : [ {"x":"1","y":"2"} , {"x":"5","y":"2"}]
"event" : [ {"random":"r", "pol" : "t", "a" : "b"} , {"x":"4","y":5"}]
"event" : [ {"random":"f", "pol" : "w", "a" : "r"} , {"x":"12","y":5"} , {"x":"6","y":"7"}]
我感兴趣的领域是x&年。对于每条记录,我需要提取具有最高x值的地图。
即。对于第一个活动,选择{"x":"5","y":"2"}
,第二个{"x":"4","y":5"}
和第三个{"x":"12","y":5"}
我知道我们可以使用UDF遍历数组中的每个映射并选择具有max x值的映射,但有没有办法可以在不编写UDF的情况下执行此操作?
答案 0 :(得分:-1)
REGISTER elephant-bird-core-4.3.jar;
REGISTER elephant-bird-hadoop-compat-4.5.jar;
REGISTER elephant-bird-pig-4.5.jar;
DEFINE JsonLoader com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad=true');
records = LOAD '$DATA_PATH' USING JsonLoader() AS (data: map[]);
events = FOREACH records GENERATE
FLATTEN(data#'event') AS event;
grouped_events = COGROUP events by event#'x', event#'y';
result = FOREACH grouped_events GENERATE
MAX(events.event#'x'),
MAX(events.event#'y');
-nestedLoad
选项有助于加载json数组,我们可以将其展平以分隔上述事件。