AVG 对分组数据抛出 ERROR 1046:Use 一个 Explicit Cast

AVG on grouped data throwing ERROR 1046:Use an Explicit Cast

我在 txt 文件中有 MAP 数据:

[age#27,height#5.8]
[age#25,height#5.3]
[age#27,height#5.10]
[age#25,height#5.1]

我想显示每个年龄组的平均身高。

这是LAOD声明:

records = LOAD '~/Documents/Pig_Map.txt' AS (details:map[]);
records: {details: map[]}

然后我根据年龄对数据进行分组:

group_data = GROUP records BY details#'age';
group_data: {group: bytearray,records: {(details: map[])}}

为了访问 details 我做了一个 FLATTEN 像这样(不确定我是否需要这一步):

flatten_records = FOREACH group_data GENERATE group,FLATTEN(records);
flatten_records: {group: bytearray,records::details: map[]}

DUMP flatten_records 这给我以下输出:

(25,[height#5.1,age#25])
(25,[height#5.3,age#25])
(27,[height#5.10,age#27])
(27,[height#5.8,age#27])

现在我想得到平均身高;我试过这个:

display_records = FOREACH flatten_records GENERATE group,AVG(records.details#'height');

错误是:

<line 10, column 57> Multiple matching functions for org.apache.pig.builtin.AVG with input schema: ({{(bytearray)}}, {{(double)}}). Please use an explicit cast.

请指教

你能试试这个吗?

records = LOAD '~/Documents/Pig_Map.txt' AS (details:map[]);
records1 = FOREACH records GENERATE details#'age' AS age,details#'height' AS height;
group_data = GROUP records1 BY age;
display_records = FOREACH group_data GENERATE group,AVG(records1.height);
dump display_records;

输出:

(25,5.199999999999999)
(27,5.449999999999999)