如何按照格式" a,b,c"的格式进行分组?从蜂巢表加载?

时间:2017-05-04 12:26:24

标签: hadoop hive apache-pig

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

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

配置单元中的位置列如下所示: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)})

我确定我需要更改我的加载声明或在分组之前添加一个步骤或两者都有,但我已经丢失了。请帮忙。正如您可能收集的那样,原始配置单元表是从带有分号作为分隔符的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");

1 个答案:

答案 0 :(得分:0)

假设评级是您在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;