【问题标题】:Java Heap Space error while executing Mapreduce执行 Mapreduce 时出现 Java 堆空间错误
【发布时间】:2016-06-15 01:04:17
【问题描述】:

我正在尝试在 Hadoop 中找到中位数。作业失败并出现以下错误:

16/03/02 02:46:13 INFO mapreduce.Job: Task Id : attempt_1456904182817_0001_r_000412_0, Status : FAILED
Error: Java heap space

我浏览了很多解决类似问题的帖子,但没有任何效果。还得到了帮助:

Error: Java heap space

我尝试了以下可能的解决方案:

  1. 按照上述帖子中的建议增加 Java 堆大小。
  2. 通过更改以下属性来增加容器的大小:

    yarn.scheduler.minimum-allocation-mb 到 yarn-site.xml 中的 1024

  3. 将减速器的数量增加到更大的值,如下所示:

    job.setNumReduceTasks(1000);

但是,以上方法对我没有任何帮助。因此,我发布这个。 我知道中值不适合 Hadoop,但任何人都可以提供任何可能有帮助的解决方案。

    java version "1.8.0_60"
    Hadoop version is 2.x

我有一个 10 节点集群,每个节点上有 8 GB RAM,每个节点上有 80 GB 硬盘。

这是完整的代码:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.math3.stat.descriptive.rank.Median;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;



public class median_all_keys {


    //Mapper
    public static class map1 extends Mapper<LongWritable,Text,Text,DoubleWritable>{public void map(LongWritable key, Text value, Context context)
            throws IOException,InterruptedException{
        String[] line= value.toString().split(",");
        double col1=Double.parseDouble(line[6]);
        double col2=Double.parseDouble(line[7]);
        context.write(new Text("Key0"+"_"+line[0]+"_"+"Index:6"), new DoubleWritable(col1));
        context.write(new Text("Key0"+"_"+line[0]+"_"+"Index:7"), new DoubleWritable(col2));
        context.write(new Text("Key1"+"_"+line[1]+"_"+"Index:6"), new DoubleWritable(col1));
        context.write(new Text("Key1"+"_"+line[1]+"_"+"Index:7"), new DoubleWritable(col2));
        context.write(new Text("Key2"+"_"+line[2]+"_"+"Index:6"), new DoubleWritable(col1));
        context.write(new Text("Key2"+"_"+line[2]+"_"+"Index:7"), new DoubleWritable(col2));
        context.write(new Text("Key0"+"_"+line[0] +","+"key1"+"_"+ line[1]+"_"+"Index:6"), new DoubleWritable(col1));
        context.write(new Text("Key0"+"_"+line[0] +","+"key1"+"_"+ line[1]+"_"+"Index:7"), new DoubleWritable(col2));
        context.write(new Text("Key1"+"_"+line[1] +","+"key2"+"_"+ line[2]+"_"+"Index:6"), new DoubleWritable(col1));
        context.write(new Text("Key1"+"_"+line[1] +","+"key2"+"_"+ line[2]+"_"+"Index:7"), new DoubleWritable(col2));
        context.write(new Text("Key0"+"_"+line[0] +","+"key2"+"_"+ line[2]+"_"+"Index:6"), new DoubleWritable(col1));
        context.write(new Text("Key0"+"_"+line[0] +","+"key2"+"_"+ line[2]+"_"+"Index:7"), new DoubleWritable(col2));
        context.write(new Text("Key0"+"_"+line[0] +","+"key1"+"_"+ line[1]+","+"key2"+"_"+line[2]+"_"+"Index:6"),new DoubleWritable(col1));
        context.write(new Text("Key0"+"_"+line[0] +","+"key1"+"_"+ line[1]+","+"key2"+"_"+line[2]+"_"+"Index:7"),new DoubleWritable(col2));         
    }
}

//Reducer
    public static class sum_reduce extends Reducer<Text,DoubleWritable,Text,DoubleWritable>{
    //  HashMap<String,List<Float>> median_map = new HashMap<String,List<Float>>(); 
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public void reduce(Text key,Iterable<DoubleWritable> value, Context context)
        throws IOException,InterruptedException{
            List<Double> values = new ArrayList<>();
            for (DoubleWritable val: value){
                values.add(val.get());
                }
            double res = calculate(values);
            context.write(key, new DoubleWritable(res));


        }

        public static double calculate(List<Double> values) {
              DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
              for (Double value : values) {
               descriptiveStatistics.addValue(value);
              }
              return descriptiveStatistics.getPercentile(50);
             }
    }



    public static void main(String[] args) throws Exception {
        Configuration conf= new Configuration();
        Job job = new Job(conf,"Sum for all keys");
        //Driver
        job.setJarByClass(median_all_keys.class);
        //Mapper
        job.setMapperClass(map1.class);
        //Reducer
        job.setReducerClass(sum_reduce.class);
        //job.setCombinerClass(TestCombiner.class);
        //Output key class for Mapper
        job.setMapOutputKeyClass(Text.class);
        //Output value class for Mapper
        job.setMapOutputValueClass(DoubleWritable.class);
        //Output key class for Reducer
        job.setOutputKeyClass(Text.class);
        job.setNumReduceTasks(1000);
        //Output value class from Reducer
        job.setOutputValueClass(DoubleWritable.class);
        //Input Format class
        job.setInputFormatClass(TextInputFormat.class);
        //Final Output Format class
        job.setOutputFormatClass(TextOutputFormat.class);
        //Path variable
        Path path = new Path(args[1]);
        //input/output path
        FileInputFormat.addInputPath(job, new Path(args[0]));
         FileOutputFormat.setOutputPath(job, new Path(args[1]));

         path.getFileSystem(conf).delete(path);
         //exiting the job
         System.exit(job.waitForCompletion(true) ? 0 : 1);


    }

}

【问题讨论】:

  • 我发现硬件规格没有问题。如果您可以发布您的代码,有人可以提供帮助。确保你没有使用任何 Java Collection Objects 来在 mappers/reducers 中存储一些数据,这可能会导致 Java Heap。
  • @srikanth 我已经添加了代码。

标签: java hadoop mapreduce


【解决方案1】:

尝试重用可写对象:创建一个 DoubleWritable 类变量并使用 .set() 为其设置值,而不是每次都创建新对象。

在 reducer 中使用数组也是不必要的,将值直接发送到您的 DescriptiveStatistics 对象。

【讨论】:

  • Removing array list from Reducer 实际上解决了这个问题。非常感谢!
【解决方案2】:

根据article 检查 YARN、Map 和 Reduce 任务的内存设置。

根据您的输入数据集大小设置内存参数。

关键参数:

纱线

yarn.scheduler.minimum-allocation-mb
yarn.scheduler.maximum-allocation-mb
yarn.nodemanager.vmem-pmem-ratio
yarn.nodemanager.resource.memory.mb

映射内存

mapreduce.map.java.opts
mapreduce.map.memory.mb

减少内存

mapreduce.reduce.java.opts
mapreduce.reduce.memory.mb

应用大师

yarn.app.mapreduce.am.command-opts
yarn.app.mapreduce.am.resource.mb

查看相关的 SE 问题:

What is the relation between 'mapreduce.map.memory.mb' and 'mapred.map.child.java.opts' in Apache Hadoop YARN?

【讨论】:

  • 嗨@ravindra ..我检查了所有设置并尝试再次运行,但失败并出现同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多