当前位置: 首页 > 知识库问答 >
问题:

Word.set()方法在map reduce hadoop中抛出空指针异常

房冥夜
2023-03-14

我对映射reduce编程是个新手,从简单的单词计数示例开始我的课程。然而,我正在尝试一种不同的方法。我的hdfs输入文件夹上有两个输入文件。我正在尝试生成类似于

anyword1 --> filename1     2
anyword2 --> filename2     3

我编写了一个映射器类来将单词和文件名连接在一起,但是当我在文本中设置键值时,它会抛出空指针异常。谁能帮我指点一下我哪里做错了。

public static class TokenizerMapper 
       extends Mapper<Object, Text, Text,IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = null;
    private String fileText = null;

    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      String fileName = ((FileSplit) context.getInputSplit()).getPath().getName();
      String modifiedWord ="";
      fileName = "-->"+fileName;
      System.out.println("filename before word-->"+fileName);
      while (itr.hasMoreTokens()) {
        modifiedWord = itr.nextToken().toString();//+fileName;
        modifiedWord = modifiedWord + fileName;
        System.out.println("modified word-->"+modifiedWord);
        word.set(modifiedWord);
        context.write(word, one);
        System.out.println("Mapper context-->"+word);
      }
    }
  }
[root@LinuxCentos7 hadoop]# hadoop jar /usr/local/mapreduceexample/WordCountEx3.jar /user/Siddharth/Input /user/Siddharth/output
17/06/09 23:32:29 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
17/06/09 23:32:32 INFO input.FileInputFormat: Total input paths to process : 2
17/06/09 23:32:32 INFO mapreduce.JobSubmitter: number of splits:2
17/06/09 23:32:32 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1497025644387_0011
17/06/09 23:32:33 INFO impl.YarnClientImpl: Submitted application application_1497025644387_0011
17/06/09 23:32:33 INFO mapreduce.Job: The url to track the job: http://LinuxCentos7:8088/proxy/application_1497025644387_0011/
17/06/09 23:32:33 INFO mapreduce.Job: Running job: job_1497025644387_0011
17/06/09 23:32:52 INFO mapreduce.Job: Job job_1497025644387_0011 running in uber mode : false
17/06/09 23:32:52 INFO mapreduce.Job:  map 0% reduce 0%
17/06/09 23:33:16 INFO mapreduce.Job:  map 100% reduce 0%
17/06/09 23:33:16 INFO mapreduce.Job: Task Id : attempt_1497025644387_0011_m_000000_0, Status : FAILED
Error: java.lang.NullPointerException
    at com.hadoop.WordCountEx3$TokenizerMapper.map(WordCountEx3.java:56)
    at com.hadoop.WordCountEx3$TokenizerMapper.map(WordCountEx3.java:1)
    at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
    at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:422)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698)
    at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

共有1个答案

戴嘉珍
2023-03-14

使用text实例初始化word变量:

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
private String fileText = null;

public void map(Object key, Text value, Context context)
        throws IOException, InterruptedException {
    ...
}
 类似资料:
  • 问题内容: 我正在android中做一个应用程序,因此我需要访问com.android.internal.telephony API。现在,我可以访问这些API了,但问题是,无论我在自己的类中调用Class Call.java方法的什么地方,都会抛出。您可以在http://hi- android.info/src/com/android/internal/telephony/Call.java.h

  • 首先,下面的代码片段是Google云项目应用程序的一部分,在我的本地客户机Raspberry Pi 1上运行。为了能够从连接到Pi的传感器向云发送数据,需要授权。所有需要的客户端机密都存储在src/main/resources中的“client_secrets.json”中。 项目层次结构 当试图使用客户端机密来授权时,下面的代码抛出一个NullPointerException。它是类“CmdLi

  • 我对spring boot和JPA相当陌生。我正在做我的学习目的的小项目。 实体类 有线索吗?

  • 公共视图onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){View v=inflater.inflate(r.layout.fragment_main,container,false); fragment.xml null

  • 问题内容: 在双头链表中,我使用了另一个链接,该链接通过copy构造函数复制到copy 。但是,当我遍历链接列表从后端插入时,它抛出了一个空指针异常。 } 问题答案: 问题似乎出在方法上。您有条件继续前进,直到node 不为null 为止: 当while循环结束时,将指向位置。然后在下一行: 现在为null,下一行: 正在尝试访问,因此是NPE的问题。您需要通过将while循环条件更改为以下内容来

  • 本文向大家介绍Java中避免空指针异常的方法,包括了Java中避免空指针异常的方法的使用技巧和注意事项,需要的朋友参考一下 没人会喜欢空指针异常!有什么方法可以避免它们吗?或许吧。。 本文将讨论到以下几种技术 1.Optional类型(Java 8中新引入的) 2.Objects类(Java 7中原有的) Java 8中的Optional类 它是什么? 1.Java 8中新引入的类型 2.它是作为