Hadoop - 每个节点的字数

Hadoop - word count per node

我正在 Hadoop 中实现 WordCount.java 的自定义版本,我有兴趣输出每个节点的字数。

例如,给定文本:

FindMe FindMe ..... .... .... .. more big text ... FindMe FindMe FindMe

FindMe node01: 2
FindMe node02: 3

这是我的 Mapper

的片段
String searchString = "FindMe";
while (itr.hasMoreTokens()) {
  String token = itr.nextToken();
  if (token.equals(searchString)) {
    word.set(token);
    context.write(word, one);
  }
}

这段代码输出

FindMe n

其中 n 是所有输入中出现的总次数。

如何输出每个节点的计数以及该节点的某种标识符,就像我上面提供的示例一样?

您可以在映射器中输出字符串+主机名,这样您就可以统计每个节点的字数。

   java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
    String computerName = localMachine.getHostName();    
        String searchString = "FindMe";
        while (itr.hasMoreTokens()) {
          String token = itr.nextToken();
          if (token.equals(searchString)) {
            word.set(token+" "+computerName);
            context.write(word, one);
          }
        }