Package com.mr.test;
Import java.io.IOException;
Import Java.util.Iterator;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.fs.Path;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.KeyValue;
Import Org.apache.hadoop.hbase.client.Result;
Import Org.apache.hadoop.hbase.client.Scan;
Import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
Import Org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
Import Org.apache.hadoop.hbase.mapreduce.TableMapper;
Import org.apache.hadoop.hbase.util.Bytes;
Import org.apache.hadoop.io.IntWritable;
Import org.apache.hadoop.io.LongWritable;
Import Org.apache.hadoop.io.Text;
Import Org.apache.hadoop.mapred.MapReduceBase;
Import Org.apache.hadoop.mapred.OutputCollector;
Import Org.apache.hadoop.mapred.Reducer;
Import Org.apache.hadoop.mapred.Reporter;
Import Org.apache.hadoop.mapreduce.Job; Import Org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
Import Org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
Import Org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
Import Org.apache.log4j.Logger;
public class Mrhbase {private static Logger log = Logger.getlogger (Mrhbase.class);
public static String family = "Charactor";
public static String col = "hobby"; public static class Hmap extends Tablemapper<text, text> {@Override protected void map (Immutableb Yteswritable key, result value, context context) throws IOException, interruptedexception {//
KeyValue kv = value.getcolumnlatest (Family.getbytes (),//Col.getbytes ()); Context.write (new text (bytes.tostring (Kv.getkey)),//New text (Bytes.tostring (kv.getvalue
())));
byte[] v = value.getvalue (Family.getbytes (), col.getbytes ());
Byte[] r = Value.getrow (); Context.write (New text (bytes.tostring (v)), new text (Bytes.tostring (r))); } public static class Reduce extends Mapreducebase implements Reducer<text, text, text , text> {public void reduce (Text key, iterator<text> values, Outputcollector<text
, text> output, Reporter Reporter) throws IOException {while (Values.hasnext ()) {
Output.collect (Key, Values.next ()); }} public static void Main (string[] args) {Configuration conf = Hbaseconfigurat
Ion.create ();
try {Job Job = new Job (conf, "hbase test");
Job.setjarbyclass (Mrhbase.class);
Scan Scan = new Scan ();
Scan.addcolumn (Family.getbytes (), col.getbytes ());
Tablemapreduceutil.inittablemapperjob ("Person", scan, Hmap.class, Text.class, Text.class, Job); Job.setoutputformatclass (Textoutputformat.class);
Fileoutputformat.setoutputpath (Job, New Path (args[0));
Job.waitforcompletion (TRUE);
catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); }
}
}
Note: To add zookeeper to the Hadoop/lib directory, master&slaves
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/extra/