10

私はhadoopが初めてです。Hdfs から入力を取得し、リデューサーの出力を Hbase に書き込む MapReduce ジョブがあります。良い例が見つかりませんでした。

コードは次のとおりです。この例を実行する際のエラーは、マップ内の型の不一致です。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 日編集。大学のプロジェクトは 2 年前に成功裏に終了しました。ジョブ構成(ドライバー部分)については、正解にチェックを入れてください。

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

HBase でデータを BulkLoad する最善かつ最速HFileOutputFormatの方法は、CompliteBulkLoadユーティリティを使用することです。

ここにサンプルコードがあります:

これが役立つことを願っています:)

于 2013-12-02T15:54:01.473 に答える
1

HDFS バージョンが機能する理由がわかりません。通常、ジョブの入力形式を設定する必要があり、FileInputFormat は抽象クラスです。おそらく、いくつかの行を省略しましたか?そのような

job.setInputFormatClass(TextInputFormat.class);
于 2011-01-12T00:58:50.890 に答える
0
 public void map(LongWritable key, Text value, 
 Context context)throws IOException, 
 InterruptedException {

これをimmutableBytesWritable, に変更しintwritableます。

よくわかりません..うまくいくことを願っています

于 2013-04-17T11:43:53.383 に答える