java - MapReduce程序中的Java ArrayIndexOutOfBound异常

标签 java hadoop mapreduce

我写了一个模版来查找某特定时期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/

相关文章:

hadoop - 使用Pig查找 pig 表中各列中存在的所有数据的最大值

hadoop - 访问在Ubuntu主机上运行的Cloudera VM上的RStudio服务器

hadoop - mapreduce作业失去连接,然后在hadoop示例 “calculating pi 3 3”中重新连接

scala - 在Spark中读取压缩的xml文件

hadoop - Sqoop导出到Teradata失败,并显示错误-任务尝试无法报告状态600秒钟。杀人

java - 如何使用 SAAJ 将 CDATA 添加到 SOAP 体部分

java - 如何避免在 Android(或任何 Java)项目中包含未使用的库?

java - JPA:单向多对一和级联删除

java - MIME4J 中基于事件的解析 - 如何从 InputStream 填充新消息?

java - 如何以及在何处在 xml 中定义此 ImageView?