简体   繁体   English

java.lang.NumberFormatException:对于输入字符串:执行MapReduce时为“ 100”

[英]java.lang.NumberFormatException: For input string: “100” while executing MapReduce

I'm writing a MapReduce job for finding common friends on facebook. 我正在写一份MapReduce工作,以便在Facebook上找到共同的朋友。

This is the input for my mapper : 这是我的映射器的输入:

100, 200 300 400 500 600
200, 100 300 400
300, 100 200 400 500
400, 100 200 300
500, 100 300
600, 100

And this is part of my mapper code: 这是我的映射器代码的一部分:

map{
        String line = value.toString();
        String[] LineSplits = line.split(",");


        String[] friends = LineSplits[1].trim().split(" ");
        for(int i =0; i<friends.length;i++) {
            int friend2 = Integer.parseInt(friends[i]);
            System.out.println(friend2);
        }

        int friend1 = Integer.parseInt(LineSplits[0]);
        System.out.println(friend1);
}                                            

When I execute this,I am getting correct values in friend2 . 当我执行此操作时,我在friend2得到了正确的值。 ( Intege.parseInt is working fine here). Intege.parseInt在这里工作正常)。 The variable friend1 is supposed to get the value as '100'. 假定变量friend1的值为“ 100”。 But Integer.ParseInt is not working and I am getting an error like this: 但是Integer.ParseInt无法正常工作,并且出现如下错误:

java.lang.Exception: java.lang.NumberFormatException: For input string: "100"
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:354)
Caused by: java.lang.NumberFormatException: For input string: "100"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.hadoop.CFMapper.map(CFMapper.java:29)
    at com.hadoop.CFMapper.map(CFMapper.java:1)
    at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:50)
    at org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:430)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:366)
    at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:223)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

So I got stuck here. 所以我被困在这里。 Why am I getting a NumberFormatException for this? 为什么我会收到NumberFormatException? and how can I rectify this? 我该如何纠正?

Your file has an invalid Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF). 您的文件包含无效的Unicode字符“零宽度无中断空格”(U + FEFF)。 you need to get rid of that. 您需要摆脱它。

The character is not visible. 该字符不可见。 So it's understandable that you didn't realize that it was there. 因此,您没有意识到它在那里是可以理解的。 You probably copy pasted in there by mistake. 您可能会错误地复制粘贴到其中。 you'll have to see where you copied your data from. 您必须查看从何处复制数据。

I should mention that trimming your string in code won't work to get rid of that character. 我应该提一下,在代码中修剪字符串将无法摆脱该字符。 You really need to go in your input file and fix it from there. 您确实需要输入文件并从那里修复它。

You will find suggestions on how to get rid of the character in this thread . 您将在此线程中找到有关如何摆脱字符的建议。

Otherwise, if your file is not too big, why not start a fresh new file, and type in your values manually to be safe. 否则,如果文件不太大,为什么不启动一个新的新文件,然后为了安全起见手动输入值。 :) :)

Edit: As you mentioned in comment the length you are getting is 4 编辑:正如您在评论中提到的,您得到的长度是4

Maybe you can Trythis . 也许你可以试试看。

LineSplits[0].replace(u'\', '') and then try to parse and see what happens LineSplits[0].replace(u'\', '') ,然后尝试解析并查看会发生什么


If you look at the Integer.parseInt() jsl it says 如果您查看Integer.parseInt()jsl,它会说

 Throws: NumberFormatException - if the string does not contain a parsable integer. 

So what if when the string: "100" reaches the ENDOFLINE . 那么,如果string: "100"到达ENDOFLINE,该怎么办? It can have character like \\r\\n or \\n ie line termination or null if the end of the stream has been reached, Which is clearly not "parsable integer" . 它可以具有\\ r \\ n或\\ n之类的字符,即line termination如果已到达流的末尾,则为null ,这显然不是“可解析的整数” So you need to check for these before parsing. 因此,您需要在解析之前检查这些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:java.lang.NumberFormatException:对于输入字符串:“ 100” - Error: java.lang.NumberFormatException: For input string: “ 100” java.lang.NumberFormatException:对于输入字符串:&quot;&quot;,而不是将字符串转换为数字 - java.lang.NumberFormatException: For input string: "", while not converting a string into a number java.lang.NumberFormatException:对于输入字符串:“1” - java.lang.NumberFormatException: For input string: " 1" 错误java.lang.NumberFormatException:对于输入字符串:“” - Error java.lang.NumberFormatException: For input string: “” java.lang.NumberFormatException:对于输入字符串:“null” - java.lang.NumberFormatException: For input string: “null” java.lang.NumberFormatException:对于输入字符串:“” - java.lang.NumberFormatException: For input string: “” java.lang.NumberFormatException:对于输入字符串:“20110328094108069414” - java.lang.NumberFormatException: For input string: “20110328094108069414” java.lang.NumberFormatException:对于输入字符串:“” - java.lang.NumberFormatException: For input string: “ ” java.lang.NumberFormatException:用于输入字符串 - java.lang.NumberFormatException: For input string java.lang.NumberFormatException:对于输入字符串:“ 8999999999999995” - java.lang.NumberFormatException: For input string: “8999999999999995”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM