Hbase->mapreduce->hbase

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.