我绝对是新手,我敢打赌,这是一个简单的答案。我的代码如下所示:

A = load 'table1' USING org.apache.hive.hcatalog.pig.HCatLoader() as (userid1: chararray, location: chararray, age: int);

hive 中的位置列如下所示:city,state,country
这就是我在做什么:
B= GROUP A BY location;
C= FOREACH B GENERATE
    group as location,
    SUM(rating) as RatingSum,
    AVG(rating) as RatingAverage,
    MIN(rating) as RatingMin,
    MAX(rating) as RatingMax,
    COUNT(rating) as RecNum;

C不能工作,可能是因为这是B的输出:
(, ,,{(56072,, ,,,56072,1885171218,3),(104462,, ,,,104462,8486054060,7),(46927,, ,,47,46927,0749300523,0),(46927,, ,,47,46927,0749300515,0),(64139,, ,,,64139,8422665662,0),(112345,, ,,39,112345,0375727345,7),(151458,, ,,,151458,1551667959,0),(64139,, ,,,64139,8422676095,6)})
(ny, ,,{(175362,ny, ,,,175362,0446604844,10)})
(, usa,,{(223496,, usa,,,223496,0714838500,7)})
(gap, ,,{(211944,gap, ,,42,211944,044023722X,9),(211944,gap, ,,42,211944,1577486445,8),(211944,gap, ,,42,211944,0821767089,9),(211944,gap, ,,42,211944,0804106304,0),(211944,gap, ,,42,211944,0743412621,9),(211944,gap, ,,42,211944,0505521474,7),(211944,gap, ,,42,211944,0440236673,9),(211944,gap, ,,42,211944,0440225701,0),(211944,gap, ,,42,211944,044022165X,0),(211944,gap, ,,42,211944,0440214041,0),(211944,gap, ,,42,211944,0440213525,0),(211944,gap, ,,42,211944,044020111X,0),(211944,gap, ,,42,211944,0425151867,0),(211944,gap, ,,42,211944,0385472951,8),(211944,gap, ,,42,211944,0373832257,7),(211944,gap, ,,42,211944,0373471521,5),(211944,gap, ,,42,211944,0373291574,6),(211944,gap, ,,42,211944,0373291566,7),(211944,gap, ,,42,211944,0373201532,7),(211944,gap, ,,42,211944,0373151861,8),(211944,gap, ,,42,211944,158660242X,0)})
(n/a, ,,{(169489,n/a, ,,,169489,0618150730,6)})

我确定我需要更 retrofit 入语句或在分组之前或在两者之前添加一个步骤,但我迷路了。请帮忙。正如您可能会收集到的那样,原始的配置单元表是通过以分号作为分隔符的csv创建的。如果有帮助,这是我用来在hive中创建表的代码:
     create table table1 (UserID string, Location string, Age INT)
    ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
    WITH SERDEPROPERTIES ("separatorChar" = '\u0059') STORED AS TEXTFILE
 tblproperties ("skip.header.line.count"="1");

最佳答案

假设等级是您在A中加载的列之一,则必须在C中使用A.rating。

C= FOREACH B GENERATE
    group as location,
    SUM(A.rating) as RatingSum,
    AVG(A.rating) as RatingAverage,
    MIN(A.rating) as RatingMin,
    MAX(A.rating) as RatingMax,
    COUNT(A.rating) as RecNum;

关于hadoop - 如何对从配置单元表加载的 “a,b,c”格式的单个列进行分组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43782877/

10-11 07:43