首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NumberFormatException错误

NumberFormatException错误
EN

Stack Overflow用户
提问于 2012-07-20 05:22:44
回答 2查看 1.9K关注 0票数 0

我正在通过Cygwin使用Hadoop运行Mapreduce作业。我收到以下NumberFormatException错误...是否知道如何在try & catch之外修复此问题,因为我认为try & catch绕过了错误,但不允许我获得预期的结果。

错误:

代码语言:javascript
复制
    12/07/19 17:10:31 INFO mapred.JobClient: Task Id : attempt_201207190234_0010_m_000000_2, Status : FAILED
java.lang.NumberFormatException: For input string: "MAX"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:449)
        at java.lang.Integer.parseInt(Integer.java:499)
        at Node.<init>(Node.java:48)
        at GraphJob$SearchMapper.map(GraphJob.java:18)

这是我的Mapper和Reduce..

代码语言:javascript
复制
public class GraphJob {

public static class SearchMapper extends Mapper<Object, Text, Text, Text>{
    public void map(Object key, Text value, Context context)
                    throws IOException, InterruptedException{
        try{
        Node inNode = new Node(value.toString());
        if(inNode.getColor() == Node.Color.GRAY) {
            for(String neighbor : inNode.getEdges()) {
                Node adjacentNode = new Node();

                adjacentNode.setId(neighbor);
                int distance = inNode.getDistance() + 1;
                adjacentNode.setDistance(distance);
                if (distance < 2){
                    adjacentNode.setColor(Node.Color.GRAY);
                }
                else{
                    adjacentNode.setColor(Node.Color.BLACK);
                }
                adjacentNode.setParent(inNode.getId());
                context.write(new Text(adjacentNode.getId()), adjacentNode.getNodeInfo());
            }

            inNode.setColor(Node.Color.BLACK);
        }

        context.write(new Text(inNode.getId()), inNode.getNodeInfo());
        }catch(Exception e){

        }
    }
}

public static class SearchReducer extends Reducer<Text, Text, Text, Text> {
    public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        Node outNode = new Node();
        outNode.setId(key.toString());

        for (Text value : values) {
            Node inNode = new Node(key.toString() + "\t" + value.toString());

            if(inNode.getEdges().size() >0) {
                outNode.setEdges(inNode.getEdges());
            }
            if (inNode.getDistance() < outNode.getDistance()) {
                outNode.setDistance(inNode.getDistance());

                outNode.setParent(inNode.getParent());
            }
            if (inNode.getColor().ordinal() > outNode.getColor().ordinal()) {
                outNode.setColor(inNode.getColor());
            }
        }
        context.write(key, new Text(outNode.getNodeInfo()));

    }

}

public static void main(String[] args) throws Exception {

    Configuration conf = new Configuration();
    String[] otherargs = new GenericOptionsParser(conf, args).getRemainingArgs();
    Job job = new Job(conf, "GraphJob");
    job.setJarByClass(GraphJob.class);
    job.setMapperClass(SearchMapper.class);
    job.setReducerClass(SearchReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);
    FileInputFormat.addInputPath(job, new Path(otherargs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherargs[1]));
    System.out.println("start the job");
    System.exit(job.waitForCompletion(true) ? 0 : 1);
    }


}

这是我的文本文件的一个示例。

代码语言:javascript
复制
1   2,7|0|GRAY|NULL
2   3,6,8|MAX|WHITE|NULL
3   2,4,5,6,8|MAX|WHITE|NULL
4   3,5,9|MAX|WHITE|NULL
5   3,4,8|MAX|WHITE|NULL
6   2,3|MAX|WHITE|NULL
7   1,8|MAX|WHITE|NULL
8   2,3,5,7,9|MAX|WHITE|NULL
9   4,8|MAX|WHITE|NULL

输出文件应为

代码语言:javascript
复制
1   2,7|0|BLACK|NULL
2   3,6,8|1|GRAY|1
7   1,8|GRAY|1
3   2,4,5,6,8|MAX|WHITE|NULL
4   3,5,9|MAX|WHITE|NULL
5   3,4,8|MAX|WHITE|NULL
6   2,3|MAX|WHITE|NULL
8   2,3,5,7,9|MAX|WHITE|NULL
9   4,8|MAX|WHITE|NULL

但是使用try & catch ..。为了停止这个错误..我只得到..。

代码语言:javascript
复制
1   2,7,|0|BLACK|NULL
2   |1|GRAY|1
7   |1|GRAY|1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-20 06:15:49

你能在你的输入文件中把MAX改成Integer.MAX_VALUE吗?例如:

代码语言:javascript
复制
1   2,7|0|GRAY|NULL
2   3,6,8|Integer.MAX_VALUE|WHITE|NULL
3   2,4,5,6,8|Integer.MAX_VALUE|WHITE|NULL
4   3,5,9|Integer.MAX_VALUE|WHITE|NULL
5   3,4,8|Integer.MAX_VALUE|WHITE|NULL
6   2,3|Integer.MAX_VALUE|WHITE|NULL
7   1,8|Integer.MAX_VALUE|WHITE|NULL
8   2,3,5,7,9|Integer.MAX_VALUE|WHITE|NULL
9   4,8|Integer.MAX_VALUE|WHITE|NULL

您的输出文件应反映新值。

票数 0
EN

Stack Overflow用户

发布于 2012-07-20 05:38:06

代码语言:javascript
复制
java.lang.NumberFormatException: For input string: "MAX"

您正尝试在代码中将字符串转换为整数值。在调用作业之前,请确保您设置的值为int。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11569523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档