マッパーの(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);
}
ユーザーのログインとコメントを数えます。今、私はカウントを変更したいと思います:すべてのログインをカウントし、ユーザーがコメントを書いたかどうかを確認します。マッパーまたはレデューサーがユーザーの1つのコメントを検索し、(このユーザーの)他のすべてのコメントを「無視」するようにするにはどうすればよいですか?
編集:
ログファイル:
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;
}