- package test;
- import java.io.IOException;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
- import org.apache.hadoop.hbase.mapreduce.TableReducer;
- import org.apache.hadoop.io.Text;
- public class ReducerClass extends TableReducer<Text,Text,ImmutableBytesWritable>{
- public void reduce(Text key,Iterable<Text> values,Context context){
- String k = key.toString();
- StringBuffer str=null;
- for(Text value: values){
- str.append(value.toString());
- }
- String v = new String(str);
- Put putrow = new Put(k.getBytes());
- putrow.add("fam1".getBytes(), "name".getBytes(), v.getBytes());
- }
- }
由上面可知ReducerClass继承TableReduce,在hadoop里面ReducerClass继承Reducer类。它的原型为:TableReducer<KeyIn,Values,KeyOut>可以看出,HBase里面是读出的Key类型是ImmutableBytesWritable。
Map,Reduce,以及Job的配置分离,比较清晰,mahout也是采用这种构架。
- package test;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.util.Tool;
- public class Driver extends Configured implements Tool{
- @Override
- public static void run(String[] arg0) throws Exception {
- // TODO Auto-generated method stub
- Configuration conf = HBaseConfiguration.create();
- conf.set("hbase.zookeeper.quorum.", "localhost");
- Job job = new Job(conf,"Hbase");
- job.setJarByClass(TxtHbase.class);
- Path in = new Path(arg0[0]);
- job.setInputFormatClass(TextInputFormat.class);
- FileInputFormat.addInputPath(job, in);
- job.setMapperClass(MapperClass.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(Text.class);
- TableMapReduceUtil.initTableReducerJob("table", ReducerClass.class, job);
- job.waitForCompletion(true);
- }
- }
D