Java 如何在Hadoop中将String对象转换为IntWritable Object

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19425949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:03:16  来源:igfitidea点击:

How to convert String object to IntWritable Object in Hadoop

javahadoop

提问by Sujana

I want to convert Stringobject to IntWritableObject in Hadoop. any process is available for conversion.

我想在 Hadoop 中将String对象转换为IntWritable对象。任何过程都可以进行转换。

回答by Stephen C

IntWritable value = new IntWritable(Integer.parseInt(someString))

... and deal with the possibility that parseIntmay throw a NumberFormatException.

... 并处理parseInt可能抛出NumberFormatException.



Someone asked:

有人问:

How to overcome this NumberFormatException ???

如何克服这个 NumberFormatException ???

The exception will occur if someStringdoes not represent an integer.

如果someString不代表整数,则会发生异常。

What do you do?

你做什么工作?

You catch it and handle it of course! How you handle it (i.e. what you should code your application to do) will depending on the context. We can't advise on that.

你当然抓住它并处理它!您如何处理它(即您应该编写应用程序来做什么)将取决于上下文。我们不能就此提出建议。

Java exceptions and exception handling are described in the Oracle Java Tutorials - here.

Java 异常和异常处理在 Oracle Java 教程 -此处进行了描述。