java arrayindexoutofbound异常

n3schb8v  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(343)

我写一篇文章,是为了找出一只股票在某一特定时期的开盘价和收盘价的最大值和最小值。

  1. public class StockByDate extends Configured implements Tool {
  2. public static class Map extends Mapper<LongWritable, Text, Text, Text> {
  3. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  4. public static Date fromdate;
  5. public static Date todate;
  6. {
  7. try {
  8. fromdate = df.parse("2000-01-01");
  9. todate = df.parse("2005-01-01");
  10. } catch (ParseException f) {
  11. f.printStackTrace();
  12. }
  13. }
  14. public void map(LongWritable key, Text value, Context context)
  15. throws IOException, InterruptedException {
  16. // Date, Open, High,Low, Close, Volume, Adj Close
  17. // 2010-10-18,40.66,41.74,40.44,41.49,10620000, 41.49
  18. try {
  19. String[] tokens = value.toString().split(",");
  20. String date = tokens[0].trim();
  21. // String open_close =
  22. // tokens[1].concat(",").concat(tokens[4].trim());
  23. String open_close = tokens[1] + ";" + tokens[4];
  24. Date givendate;
  25. givendate = df.parse(date);
  26. if (givendate.after(fromdate) && givendate.before(todate)) {
  27. context.write(new Text(date), new Text(open_close));
  28. System.out.println(open_close);
  29. }
  30. } catch (ParseException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. public static class Reduce extends Reducer<Text, Text, Text, Text> {
  36. public static float maxopen = (float) 0;
  37. public static float minopen = Float.MAX_VALUE;
  38. public Text maxopenkey;
  39. public Text minopenkey;
  40. public static float maxclose = (float) 0;
  41. public static float minclose = Float.MAX_VALUE;
  42. public Text maxclosekey;
  43. public Text minclosekey;
  44. public void reduce(Text key, Iterable<Text> values, Context context)
  45. throws IOException, InterruptedException {
  46. boolean maxopenfound = false;
  47. boolean minopenfound = false;
  48. boolean maxclosefound = false;
  49. boolean minclosefound = false;
  50. for (Text val : values) {
  51. System.out.println(key.toString() + " " + val.toString());
  52. String[] temp_present = val.toString().split(";");
  53. System.out.println(key.toString() + " " + val.toString());
  54. float open_float = Float.valueOf(temp_present[0].trim());
  55. float close_float = Float.valueOf(temp_present[1].trim());
  56. if (open_float >= maxopen) {
  57. maxopen = open_float;
  58. maxopenkey = key;
  59. maxopenfound = true;
  60. }
  61. if (open_float <= minopen) {
  62. minopen = open_float;
  63. minopenkey = key;
  64. minopenfound = true;
  65. }
  66. /*
  67. * if(close_float >= maxclose){ maxclose = close_float;
  68. * maxclosekey=key; maxclosefound = true; } if(close_float <=
  69. * minclose){ minclose = close_float; minclosekey=key;
  70. * minclosefound = true; }
  71. */
  72. /*
  73. * if (it.hasNext()){
  74. *
  75. * String[] temp_next = it.next().toString().split(","); float
  76. * open_float_next = Float.valueOf(temp_next[0].trim());
  77. *
  78. * if(open_float <= open_float_next){ minopen = open_float;
  79. * minopenkey = key; minopenfound = true; }
  80. *
  81. * }
  82. */
  83. // float presentOpenValue = Float.valueOf(temp[0]);
  84. // float presentMaxCloseValue = Float.parseFloat(temp[1]);
  85. }
  86. // con text.write(key, new Text(String.valueOf(maxopen)));
  87. if (maxopenfound) {
  88. Text newmax = new Text();
  89. newmax.set(String.valueOf(maxopen));
  90. context.write(maxopenkey, newmax);
  91. }
  92. if (minopenfound) {
  93. Text newmin = new Text();
  94. newmin.set(String.valueOf(minopen));
  95. context.write(minopenkey, newmin);
  96. }
  97. }
  98. }
  99. public int run(String[] args) throws Exception {
  100. Job job = Job.getInstance(getConf(), "StockByDate");
  101. job.setJarByClass(StockByDate.class);
  102. job.setJobName("StockByDate");
  103. job.setOutputKeyClass(Text.class);
  104. job.setOutputValueClass(Text.class);
  105. job.setMapperClass(Map.class);
  106. job.setCombinerClass(Reduce.class);
  107. job.setReducerClass(Reduce.class);
  108. job.setInputFormatClass(TextInputFormat.class);
  109. job.setOutputFormatClass(TextOutputFormat.class);
  110. FileInputFormat.setInputPaths(job, new Path(args[0]));
  111. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  112. boolean success = job.waitForCompletion(true);
  113. return success ? 0 : 1;
  114. }
  115. public static void main(String[] args) throws Exception {
  116. int ret = ToolRunner.run(new StockByDate(), args);
  117. System.exit(ret);
  118. }
  119. }

以下是例外情况:
5/06/06 05:19:09 info configuration.deprecation:mapred.skip.on已弃用。相反,请使用mapreduce.job.skiprecrds 2000-01-03 104.8751115/06/06 05:19:09 info mapred.localjobrunner:reduce task executor complete。
2000-01-03 104.87511 15/06/06 05:19:09警告mapred.localjobrunner:job\u local790515175\u 0001 java.lang.exception:java.lang.arrayindexoutofboundsexception:1位于org.apache.hadoop.mapred.localjobrunner$job.runtasks(localjobrunner)。java:462)在org.apache.hadoop.mapred.localjobrunner$job.run(localjobrunner。java:529)原因:java.lang.arrayindexoutofboundsexception:1位于org.wordcompute.stock.stockbydate$reduce.reduce(stockbydate)。java:117)在org.wordcompute.stock.stockbydate$reduce.reduce(stockbydate。java:1)在org.apache.hadoop.mapreduce.reducer.run(reducer。java:171)在org.apache.hadoop.mapred.reducetask.runnewreducer(reducetask。java:627)在org.apache.hadoop.mapred.reducetask.run(reducetask。java:389)在org.apache.hadoop.mapred.localjobrunner$job$reducetaskrunnable.run(localjobrunner)。java:319)在java.util.concurrent.executors$runnableadapter.call(executors。java:471)在java.util.concurrent.futuretask.run(futuretask。java:262)在java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor。java:1145)在java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor。java:615)在java.lang.thread.run(线程。java:745)15/06/06 05:19:10 info mapreduce.job:map 100%reduce 0%15/06/06 05:19:10 info mapreduce.job:job job\u local790515175\u 0001失败,状态为失败原因:na 15/06/06 05:19:10 info mapreduce.job:计数器:33文件系统计数器文件:读取的字节数=550953文件:写入的字节数=244837文件:读取操作数=0文件:大读取操作数=0文件:写入操作数=0 map reduce framework map input records=8692 map output records=1256 map output字节=35609Map输出物化字节=1012输入分割字节=136组合输入记录=1256组合输出记录=46减少输入组=0减少无序字节=1012减少输入记录=0减少输出记录=0溢出记录=46无序Map=1失败无序=0合并Map输出=1 gc所用时间(ms)=45 cpu所用时间(ms)=0物理内存(字节)快照=0虚拟内存(字节)快照=0总提交的堆使用率(字节)=165613568无序错误\u id=0连接=0 io \u错误=0错误\u长度=0错误\uMap=0错误\u缩减=0文件输入格式计数器字节读取=550760文件输出格式计数器字节写入
十=0

zysjyyx4

zysjyyx41#

您的代码抛出一个arrayindexoutofboundsexception,如堆栈跟踪第117行所示。
我怀疑

  1. float close_float = Float.valueOf(temp_present[1].trim());

如果temp\u中只有一个值。检查你的值。

相关问题