我正在编写一个mampreduce来查找特定时期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);
}
}
以下是例外:
5/06/06 05:19:09 INFO Configuration.deprecation:mapred.skip.on是 弃用。相反,请使用mapreduce.job.skiprecords 2000-01-03 104.8751115 / 06/06 05:19:09 INFO mapred.LocalJobRunner:减少任务执行者完成。
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) 在 org.apache.hadoop.mapred.LocalJobRunner $ Job.run(LocalJobRunner.java:529) 引起:java.lang.ArrayIndexOutOfBoundsException:1 at 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)at at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627) 在org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389)at org.apache.hadoop.mapred.LocalJobRunner $工作$ ReduceTaskRunnable.run(LocalJobRunner.java:319) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471) 在java.util.concurrent.FutureTask.run(FutureTask.java:262)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 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:地图100%减少0%15/06/06 05:19:10信息 mapreduce.Job:作业job_local790515175_0001失败,状态为FAILED 由于:NA 15/06/06 05:19:10 INFO mapreduce.Job:计数器:33档 系统计数器FILE:读取的字节数= 550953 FILE:Number of 写入的字节数= 244837 FILE:读取操作数= 0 FILE: 大读操作数= 0 FILE:写操作数= 0 Map-Reduce Framework Map输入记录= 8692 Map输出 records = 1256 Map output bytes = 35609实现了地图输出 bytes = 1012输入拆分字节= 136组合输入记录= 1256 组合输出记录= 46减少输入组= 0减少随机播放 bytes = 1012减少输入记录= 0减少输出记录= 0 溢出记录= 46个混洗映射= 1个失败的随机数= 0合并 映射输出= 1经过的GC时间(ms)= 45 CPU花费的时间(ms)= 0 物理内存(字节)snapshot = 0虚拟内存(字节) snapshot = 0已提交的总堆使用量(字节)= 165613568随机播放 错误BAD_ID = 0 CONNECTION = 0 IO_ERROR = 0 WRONG_LENGTH = 0 WRONG_MAP = 0 WRONG_REDUCE = 0文件输入格式计数字节 读= 550760文件输出格式计数字节写
10 = 0
答案 0 :(得分:1)
你的代码会抛出一个ArrayIndexOutOfBoundsException,如堆栈跟踪第117行所示。
我怀疑
float close_float = Float.valueOf(temp_present[1].trim());
如果temp_present中只有一个值。检查你的价值观。