【问题标题】:Hadoop mapreduce - reducer not runningHadoop mapreduce - 减速器未运行
【发布时间】:2016-07-02 14:49:33
【问题描述】:

我正在尝试将批量加载 map-reduce 自定义到 HBase 中,但遇到了 reducer 问题。一开始我以为我没有写好reducer,但是在reducer中抛出运行时异常并看到代码工作时,我意识到reducer根本没有运行。 到目前为止,我认为这个问题的一些常见答案没有任何问题;

  1. 我的配置有 mapoutput 和 output 分开
  2. 我的 reducer 和 mapper 已覆盖
  3. 我有 Iterable,我的 reducer 输入是(可写,放),所以...

这是我的代码:

司机

public int run(String[] args) throws Exception {
    int result=0;
    String outputPath = args[1];
    Configuration configuration = getConf();
    configuration.set("data.seperator", DATA_SEPERATOR);
    configuration.set("hbase.table.name",TABLE_NAME);
    configuration.set("COLUMN_FAMILY_1",COLUMN_FAMILY_1);
    Job job = new Job(configuration);
    job.setJarByClass(HBaseBulkLoadDriver.class);
    job.setJobName("Bulk Loading HBase Table::"+TABLE_NAME);
    job.setInputFormatClass(TextInputFormat.class);
    job.setMapOutputKeyClass(ImmutableBytesWritable.class);
    job.setMapperClass(HBaseBulkLoadMapper.class);
    job.setReducerClass(HBaseBulkLoadReducer.class);
    job.setOutputKeyClass(ImmutableBytesWritable.class);
    job.setOutputValueClass(Put.class);
    FileInputFormat.addInputPaths(job, args[0]);
    FileSystem.getLocal(getConf()).delete(new Path(outputPath), true);
    FileOutputFormat.setOutputPath(job, new Path(outputPath));
    job.setMapOutputValueClass(Put.class);
    job.setNumReduceTasks(1);
    HFileOutputFormat.configureIncrementalLoad(job, new HTable(configuration,TABLE_NAME));
    job.waitForCompletion(true);

映射器

public class HBaseBulkLoadMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
    private String hbaseTable;
    private String dataSeperator;
    private String columnFamily1;
    private ImmutableBytesWritable hbaseTableName;

    public void setup(Context context) {
        Configuration configuration = context.getConfiguration();
        hbaseTable = configuration.get("hbase.table.name");
        dataSeperator = configuration.get("data.seperator");
        columnFamily1 = configuration.get("COLUMN_FAMILY_1");
        hbaseTableName = new ImmutableBytesWritable(Bytes.toBytes(hbaseTable));
    }
        @Override
    public void map(LongWritable key, Text value, Context context) {
        try {
            String[] values = value.toString().split(dataSeperator);
            String rowKey = values[0];
            Put put = new Put(Bytes.toBytes(rowKey));
            BUNCH OF ADDS;
            context.write(new ImmutableBytesWritable(Bytes.toBytes(rowKey)), put);
        } catch(Exception exception) {
            exception.printStackTrace();
        }
    }
}

减速器

public class HBaseBulkLoadReducer extends Reducer<ImmutableBytesWritable, Put, ImmutableBytesWritable, Put> {
      @Override
      protected void reduce(
          ImmutableBytesWritable row,
          Iterable<Put> puts,
          Reducer<ImmutableBytesWritable, Put,
                  ImmutableBytesWritable, Put>.Context context)
          throws java.io.IOException, InterruptedException
      {
        TreeMap<String,KeyValue> map = new TreeMap<String,KeyValue>();
        int count =0;
        Append nkv;
        byte[] tmp= "".getBytes();
        Put pp = new Put(tmp);
    try{
        for (Put p : puts) {
              byte[] r =  "".getBytes();
              //KeyValue kv = new KeyValue(r);
              if (count!=0){
              r = p.getRow();
              pp.add(new KeyValue(r));
              //KeyValue k = map.get(row.toString());
              //nkv = new Append(k.getRowArray());
              //nkv=nkv.add(kv);
              //map.put(row.toString(), k.clone());
              //context.write(row,nkv);
              //tmp=ArrayUtils.addAll(tmp,kv.getValueArray());
              //map.put(row.toString(),new KeyValue(kv.getRowArray(),kv.getFamilyArray(),kv.getQualifierArray(),tmp));
              count++;
              throw new RuntimeException();
              }
              else{
              r = p.getRow();
              pp = new Put(row.toString().getBytes());
              pp.add(new KeyValue(r));
              //tmp=kv.clone().getValueArray();
              //nkv = new Append(kv.getRowArray());
              //map.put(row.toString(), kv.clone());
              count++;
              throw new RuntimeException();
          }
     }
      context.write(row,pp);
      }catch(Exception e) { e.printStackTrace();}
     }

}

好吧,我知道 reducer 有点乱,但问题是,它在 if 和 else 子句上都有 runtimeException,正如你所见,批量加载成功,所以我很确定 reducer 没有运行——我没有确定为什么。所有三个文件都打包在同一个目录中,仅供参考。

【问题讨论】:

  • 你为什么隐含throw new RuntimeException();
  • 他试图查看该块是否已执行...“但是在减速器中抛出运行时异常并看到代码正常工作时,我意识到减速器根本没有运行”
  • 我认为reducer应该基于job.setNumReduceTasks(1)运行,但是如果Iterable&lt;Put&gt; puts为空,则不会进入reducer的for循环,也不会抛出这些异常
  • 你确定map阶段执行成功了吗?
  • 我唯一能想到的是他们for 块从未被输入,这意味着Iterable&lt;Put&gt; puts 可能是空的。

标签: java hadoop mapreduce reducers


【解决方案1】:

找出问题所在。 configureincrementalload 根据输出值将reducer类设置为putsort或keyvaluesort,所以如果我想使用自定义reducer类我必须在configureincrementalload之后设置它。之后我可以看到减速器在运行。只是回答我自己的问题,以便它可以帮助遇到同样问题的人。

HFileOutputFormat.configureIncrementalLoad(job, new HTable(configuration,TABLE_NAME));
job.setReducerClass(HBaseBulkLoadReducer.class);
job.waitForCompletion(true);

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2011-07-25
    相关资源
    最近更新 更多