gpt4 book ai didi

java - 错误 : java. lang.NumberFormatException:对于输入字符串: "time"

转载 作者:可可西里 更新时间:2023-11-01 16:21:26 24 4
gpt4 key购买 nike

我正在尝试编写 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 数据集的每一行中取第三个值,这是一个数字,将其转换为字符串,将其转换为长整数并使用长整数来查找月份和年份。

我哪里错了?谢谢。

编辑//示例条目:

0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098,00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048,1231469665,1,1

最佳答案

如果问题是标题,只需替换导致问题的行:

Long timestamp = Long.parseLong(transactionData[2]);

Long timestamp = 0L;
try {
timestamp = Long.parseLong(transactionData[2]);
} catch (NumberFormatException e) {
return;
}

因此,如果该值无效,该方法将终止而不会崩溃。

关于java - 错误 : java. lang.NumberFormatException:对于输入字符串: "time",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53470191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com