我写一篇文章,是为了找出一只股票在某一特定时期的开盘价和收盘价的最大值和最小值。
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);
}
}
以下是例外情况:
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
1条答案
按热度按时间zysjyyx41#
您的代码抛出一个arrayindexoutofboundsexception,如堆栈跟踪第117行所示。
我怀疑
如果temp\u中只有一个值。检查你的值。