gpt4 book ai didi

java - MapReduce程序中的Java ArrayIndexOutOfBound异常

转载 作者:行者123 更新时间:2023-12-02 22:08:30 25 4
gpt4 key购买 nike

我写了一个模版来查找某特定时期from_date和To date的股票开盘和收盘价的最大值,最小值。

    public class StockByDate extends Configured implements Tool {

public static class Map extends Mapper<LongWritable, Text, Text, Text> {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
public static Date fromdate;
public static Date todate;
{
try {
fromdate = df.parse("2000-01-01");
todate = df.parse("2005-01-01");
} catch (ParseException f) {
f.printStackTrace();
}

}

public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// Date, Open, High,Low, Close, Volume, Adj Close
// 2010-10-18,40.66,41.74,40.44,41.49,10620000, 41.49

try {
String[] tokens = value.toString().split(",");

String date = tokens[0].trim();

// String open_close =
// tokens[1].concat(",").concat(tokens[4].trim());

String open_close = tokens[1] + ";" + tokens[4];

Date givendate;

givendate = df.parse(date);

if (givendate.after(fromdate) && givendate.before(todate)) {

context.write(new Text(date), new Text(open_close));
System.out.println(open_close);

}

} catch (ParseException e) {

e.printStackTrace();
}




}
}

public static class Reduce extends Reducer<Text, Text, Text, Text> {

public static float maxopen = (float) 0;
public static float minopen = Float.MAX_VALUE;
public Text maxopenkey;
public Text minopenkey;

public static float maxclose = (float) 0;
public static float minclose = Float.MAX_VALUE;
public Text maxclosekey;
public Text minclosekey;

public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {

boolean maxopenfound = false;

boolean minopenfound = false;

boolean maxclosefound = false;

boolean minclosefound = false;

for (Text val : values) {

System.out.println(key.toString() + " " + val.toString());

String[] temp_present = val.toString().split(";");
System.out.println(key.toString() + " " + val.toString());
float open_float = Float.valueOf(temp_present[0].trim());

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

if (open_float >= maxopen) {
maxopen = open_float;
maxopenkey = key;
maxopenfound = true;
}
if (open_float <= minopen) {
minopen = open_float;
minopenkey = key;
minopenfound = true;
}

/*
* if(close_float >= maxclose){ maxclose = close_float;
* maxclosekey=key; maxclosefound = true; } if(close_float <=
* minclose){ minclose = close_float; minclosekey=key;
* minclosefound = true; }
*/

/*
* if (it.hasNext()){
*
* String[] temp_next = it.next().toString().split(","); float
* open_float_next = Float.valueOf(temp_next[0].trim());
*
* if(open_float <= open_float_next){ minopen = open_float;
* minopenkey = key; minopenfound = true; }
*
* }
*/

// float presentOpenValue = Float.valueOf(temp[0]);

// float presentMaxCloseValue = Float.parseFloat(temp[1]);


}

// con text.write(key, new Text(String.valueOf(maxopen)));

if (maxopenfound) {
Text newmax = new Text();
newmax.set(String.valueOf(maxopen));
context.write(maxopenkey, newmax);
}

if (minopenfound) {
Text newmin = new Text();
newmin.set(String.valueOf(minopen));
context.write(minopenkey, newmin);
}



}
}

public int run(String[] args) throws Exception {
Job job = Job.getInstance(getConf(), "StockByDate");
job.setJarByClass(StockByDate.class);
job.setJobName("StockByDate");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean success = job.waitForCompletion(true);
return success ? 0 : 1;
}

public static void main(String[] args) throws Exception {
int ret = ToolRunner.run(new StockByDate(), args);
System.exit(ret);
}

}

以下是异常(exception):

5/06/06 05:19:09 INFO Configuration.deprecation: mapred.skip.on is deprecated. Instead, use mapreduce.job.skiprecords 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 WARN mapred.LocalJobRunner: job_local790515175_0001 java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1 at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462) at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:529) Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 at org.wordcompute.stock.StockByDate$Reduce.reduce(StockByDate.java:117) at org.wordcompute.stock.StockByDate$Reduce.reduce(StockByDate.java:1) at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:171) at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627) at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389) at org.apache.hadoop.mapred.LocalJobRunner$Job$ReduceTaskRunnable.run(LocalJobRunner.java:319) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.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_local790515175_0001 failed with state FAILED due to: NA 15/06/06 05:19:10 INFO mapreduce.Job: Counters: 33 File System Counters FILE: Number of bytes read=550953 FILE: Number of bytes written=244837 FILE: Number of read operations=0 FILE: Number of large read operations=0 FILE: Number of write operations=0 Map-Reduce Framework Map input records=8692 Map output records=1256 Map output bytes=35609 Map output materialized bytes=1012 Input split bytes=136 Combine input records=1256 Combine output records=46 Reduce input groups=0 Reduce shuffle bytes=1012 Reduce input records=0 Reduce output records=0 Spilled Records=46 Shuffled Maps =1 Failed Shuffles=0 Merged Map outputs=1 GC time elapsed (ms)=45 CPU time spent (ms)=0 Physical memory (bytes) snapshot=0 Virtual memory (bytes) snapshot=0 Total committed heap usage (bytes)=165613568 Shuffle Errors BAD_ID=0 CONNECTION=0 IO_ERROR=0 WRONG_LENGTH=0 WRONG_MAP=0 WRONG_REDUCE=0 File Input Format Counters Bytes Read=550760 File Output Format Counters Bytes Writ



十= 0

最佳答案

好了,您的代码将引发ArrayIndexOutOfBoundsException,如堆栈跟踪的第117行所示。

我猜测

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

如果temp_present中只有一个值。检查您的值。

关于java - MapReduce程序中的Java ArrayIndexOutOfBound异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30683054/

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