我正在尝试编写Java MapReduce代码并不断收到错误:
Error: java.lang.NumberFormatException: For input string: "time"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at TokenizerMapper.map(Unknown Source)
at TokenizerMapper.map(Unknown Source)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:793)
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:1917)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)
有问题的映射器代码如下
public class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private final IntWritable one = new IntWritable(1);
private Text data = new Text();
String[] transactionData = new String[5];
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString(), ",");
while (itr.hasMoreTokens()) {
for (int i = 0; i < 5; i++) {
transactionData[i] = itr.nextToken();
}
Long timestamp = Long.parseLong(transactionData[2]);
Instant instant = Instant.ofEpochSecond(timestamp);
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.of("GMT"));
int year = ldt.getYear(); //e.g 2009
Month month = ldt.getMonth(); //e.g JANUARY
String time = month.toString() + ":" + Integer.toString(year);
data.set(time);
context.write(data, one);
}
}
}
应该在csv数据集的每一行中取第3个值,该值是一个数字,将其转换为字符串,将其转换为long,然后使用long查找月份和年份。
我要去哪里错了?谢谢。
编辑//示例条目:
0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098,00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048,1231469665,1,1
答案 0 :(得分:-1)
如果问题是标题,则只需替换导致问题的行:
Long timestamp = Long.parseLong(transactionData[2]);
与
Long timestamp = 0L;
try {
timestamp = Long.parseLong(transactionData[2]);
} catch (NumberFormatException e) {
return;
}
因此,如果该值无效,该方法将终止而不会崩溃。