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);

        }

ユーザーのログインとコメントを数えます。今、私はカウントを変更したいと思います:すべてのログインをカウントし、ユーザーがコメントを書いたかどうかを確認します。マッパーまたはレデューサーがユーザーの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;
 }
4

1 に答える 1

0

私の理解が正しければ、ログインしたユニーク ユーザーの総数とコメントの総数を取得したいだけですか?

Hadoop で「集約」リデューサーを使用することをお勧めします。

マッパーで、次のような行を出力します。

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

これがあなたが望むものだと思いますか?

于 2013-03-04T20:29:22.210 に答える