10

我是hadoop的新手。我有一个 MapReduce 作业,它应该从 Hdfs 获取输入并将 reducer 的输出写入 Hbase。我没有找到任何好的例子。

这是代码,运行此示例的错误是 Map 中的类型不匹配,预期 ImmutableBytesWritable 收到 IntWritable。

映射器类

public static class AddValueMapper extends Mapper < LongWritable,
 Text, ImmutableBytesWritable, IntWritable > {  

  /* input <key, line number : value, full line>
   *  output <key, log key : value >*/  
public void map(LongWritable key, Text value, 
     Context context)throws IOException, 
     InterruptedException {
  byte[] key;
  int value, pos = 0;
  String line = value.toString();
  String p1 , p2 = null;
  pos = line.indexOf("=");

   //Key part
   p1 = line.substring(0, pos);
   p1 = p1.trim();
   key = Bytes.toBytes(p1);   

   //Value part
   p2 = line.substring(pos +1);
   p2 = p2.trim();
   value = Integer.parseInt(p2);

   context.write(new ImmutableBytesWritable(key),new IntWritable(value));
  }
}

减速机类

public static class AddValuesReducer extends TableReducer<
  ImmutableBytesWritable, IntWritable, ImmutableBytesWritable> {

  public void reduce(ImmutableBytesWritable key, Iterable<IntWritable> values, 
   Context context) throws IOException, InterruptedException {

         long total =0;
         // Loop values
         while(values.iterator().hasNext()){
           total += values.iterator().next().get();
         }
         // Put to HBase
         Put put = new Put(key.get());
         put.add(Bytes.toBytes("data"), Bytes.toBytes("total"),
           Bytes.toBytes(total));
         Bytes.toInt(key.get()), total));
            context.write(key, put);
        }
    }

我只在 HDFS 上做过类似的工作并且工作正常。

2013 年 6 月 18 日编辑。两年前,学院项目顺利完成。对于作业配置(驱动程序部分),请检查正确答案。

4

4 回答 4

6

这是解决您问题的代码



司机

HBaseConfiguration conf =  HBaseConfiguration.create();
Job job = new Job(conf,"JOB_NAME");
    job.setJarByClass(yourclass.class);
    job.setMapperClass(yourMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(Intwritable.class);
    FileInputFormat.setInputPaths(job, new Path(inputPath));
    TableMapReduceUtil.initTableReducerJob(TABLE,
            yourReducer.class, job);
    job.setReducerClass(yourReducer.class);
            job.waitForCompletion(true);


Mapper&Reducer

class yourMapper extends Mapper<LongWritable, Text, Text,IntWritable> {
//@overide map()
 }

class yourReducer
        extends
        TableReducer<Text, IntWritable, 
        ImmutableBytesWritable>
{
//@override reduce()
}

于 2013-06-18T12:16:44.870 回答
1

不确定为什么 HDFS 版本有效:通常您必须为作业设置输入格式,而 FileInputFormat 是一个抽象类。也许你遗漏了一些台词?如

job.setInputFormatClass(TextInputFormat.class);
于 2011-01-12T00:58:50.890 回答
1

在 HBase 中批量加载数据的最好和最快HFileOutputFormat的方法是使用和CompliteBulkLoad实用程序。

您将在此处找到示例代码:

希望这会有用:)

于 2013-12-02T15:54:01.473 回答
0
 public void map(LongWritable key, Text value, 
 Context context)throws IOException, 
 InterruptedException {

将其更改为immutableBytesWritable, intwritable

我不确定..希望它有效

于 2013-04-17T11:43:53.383 回答