0

映射器中的 (key,value) 示例:(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);

        }

我计算用户的登录和评论。现在我想更改计数:计算每次登录并查看用户是否写了评论。我怎样才能实现我的映射器或缩减器只搜索用户的一条评论并“忽略”所有其他评论(该用户的)?

编辑:

日志文件:

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;
 }
4

1 回答 1

0

如果我理解正确,您只想获取登录的唯一用户总数以及评论总数?

我建议在 Hadoop 中使用“聚合”reducer。

在您的映射器中,输出如下所示的行:

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

然后在此运行“聚合”reducer,你应该得到一个如下所示的输出:

unique_users    2
comments        5

我假设这是你想要的?

于 2013-03-04T20:29:22.210 回答