HBase expands the MapReduce API to facilitate the MapReduce task to read and write htable data.
Package Taglib.customer;import java.io.IOException; Import org.apache.hadoop.conf.Configuration; Import org.apache.hadoop.hbase.HBaseConfiguration; Import Org.apache.hadoop.hbase.client.Put; 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.mapreduce.TableReducer; Import org.apache.hadoop.hbase.util.Bytes; Import org.apache.hadoop.io.IntWritable; Import Org.apache.hadoop.io.Text; Import Org.apache.hadoop.mapreduce.job;public class Mrhbase {public static void main (string[] args) throws IOException, C Lassnotfoundexception, Interruptedexception {//TODO auto-generated method Stubconfiguration conf = Hbaseconfiguration.create (); Conf.set ("Hbase.zookeeper.quorum", "192.168.58.101"); Job Job = new Job (conf, "examplesummary"); Job.setjarbyclass (Mrhbase.class); Class that contains mapper and reducer scan scan = new scan (); Scan.setcaching (500); 1 is the default in Scan, which would be a bad for MapReduce jobs scan.setcacheblocks (FALSE); Don ' t set to true for MR jobs//Set other scan attrs//scan.addcolumn (family, qualifier); Tablemapreduceutil.inittablemapperjob ("blog",//Input table scan, Scan instance to control CF and attribute selection Mymapper.class,//Mapper class Text.class,//Mapper output key Intwritable.class,//Mapper output value Job); Tablemapreduceutil.inittablereducerjob ("blog2",//Output table MYTABLEREDUCER.C Lass,//reducer class job); Job.setnumreducetasks (1); At least one, adjust as RequiRed Boolean B = Job.waitforcompletion (true); if (!b) {throw new IOException ("Error with job!"); }}public Static class Mymapper extends Tablemapper<text, intwritable> {private final intwritable one = New Intwritable (1); Private text text = new text (); public void Map (immutablebyteswritable row, Result value, Context context) throws IOException, Interruptedexception { String IP = bytes.tostring (Row.get ()); String url = new String (Value.getvalue (Bytes.tobytes ("article"), Bytes.tobytes ("title")); Text.set (ip+ "&" +url); Context.write (text, one); }}public Static class Mytablereducer extends Tablereducer<text, intwritable, immutablebyteswritable> { public void reduce (Text key, iterable<intwritable> values, context context) throws IOException, interruptedexcept ion {int sum = 0; for (intwritable VAl:values) {sum + = Val.get (); Put put = new put (key.getbytes ()); Put.add (Bytes.tobytes ("article"), Bytes.tobytes ("title"), Bytes.tobytes (string.valueof (sum))); Context.write (null, put); } } }
Hbase->mapreduce->hbase