hadoop:值的不同计数(java)

ejk8hzay  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(559)

Map器中(键、值)的示例:(user,(logincount,commentcount))

public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {

            String tempString = value.toString();
            String[] stringData = tempString.split(",");

            String user = stringData[2];
            String activity = stringData[1];

            if (activity.matches("login")) {
                outCount.set(1,0);
            } 
            if (activity.matches("comment")) {
                outCount.set(0,1);
            }

            outUserID.set(userID);

            context.write(outUserID, outCount);

        }

我统计用户的登录和评论。现在我想更改计数:计数每次登录&看看用户是否写了评论。我怎样才能实现我的Map器或reducer只搜索用户的一条评论而“忽略”所有其他评论(这个用户的)?
编辑:
日志文件:

2013-01-01T16:50:56.056+0100,login,User14133,somedata,somedata
2013-01-01T16:55:56.056+0100,login,User14133,somedata,somedata
2013-01-01T05:20:44.044+0100,comment,User14133,somedata,somedata,{text: "something here"}
2013-01-01T05:24:44.044+0100,comment,User14133,somedata,somedata,{text: "something here"}
2013-01-01T20:50:13.013+0100,login,User76892,somedata,somedata

目前产量:

User14133   Logins: 2   Comments: 2
User76892   Logins: 1   Comments: 0

输入:

Mapper<LongWritable, Text, Text, UserCount>
Reducer<Text, UserCount, Text, UserCount>

public static class UserCount implements Writable {
        public UserCountTuple() {
            set(new IntWritable(0), new IntWritable(0));
        }

我的mapreduce统计用户的每一次登录和每一条评论,并对它们进行汇总。我想要实现的是这样的->输出:

User14133   Logins: 2      Comments: 0 or 1 (Did User wrote one comment?)*

 * In Mapper or Reducer (?)
 for every line in the log{
   if (user wrote comment){
     return 1;
     ignore all other comments from same user in this log;
   } else if (user didn't write anything) return 0;
 }
agxfikkp

agxfikkp1#

如果我理解正确,您只想得到登录的唯一用户的总数,以及评论的总数?
我建议在hadoop中使用“聚合”缩减器。
在Map器中,输出行如下所示:

UniqValueCount:unique_users      User14133
LongValueSum:comments            1
UniqValueCount:unique_users      User14133
LongValueSum:comments            1
UniqValueCount:unique_users      User14133
LongValueSum:comments            1
UniqValueCount:unique_users      User14133
LongValueSum:comments            1
UniqValueCount:unique_users      User76892
LongValueSum:comments            1

然后在此基础上运行“聚合”缩减器,您将得到如下输出:

unique_users    2
comments        5

我想这就是你想要的?

相关问题