Implements a tool class that can export any number of columns of an hbase arbitrary table to any specified HDFs

Source: Internet
Author: User

 PackageHBase;Importjava.io.IOException;ImportJava.net.URI;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path;ImportOrg.apache.hadoop.hbase.Cell;ImportOrg.apache.hadoop.hbase.CellUtil;Importorg.apache.hadoop.hbase.HBaseConfiguration;ImportOrg.apache.hadoop.hbase.client.Result;ImportOrg.apache.hadoop.hbase.client.Scan;Importorg.apache.hadoop.hbase.io.ImmutableBytesWritable;ImportOrg.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;ImportOrg.apache.hadoop.hbase.mapreduce.TableMapper;ImportOrg.apache.hadoop.io.Text;ImportOrg.apache.hadoop.mapreduce.Job;ImportOrg.apache.hadoop.mapreduce.Mapper;ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;ImportOrg.apache.hadoop.mapreduce.lib.output.TextOutputFormat; Public classHbase2hdfsutils {/*** Args[0] Table name * args[1] column family, column name list, format---column family: column * args[2] Output path *@paramargs *@throwsException*/     Public Static voidMain (string[] args)throwsException {//get configuration information for HBase and get configuration information from the Hbase-site.xml file in the resources directoryConfiguration conf =hbaseconfiguration.create (); //set column family, column name information list parameter, format--column family: columnConf.set ("Familycolumnslist", args[1]); //Declare a clientJob Job = job.getinstance (conf, hbase2hdfsutils.class. Getsimplename ()); //executing a JAR package requires specifying the class nameJob.setjarbyclass (hbase2hdfsutils.class); //Specifies the information that is required to export the table in HBase, which is the input to the mapScan Scan =NewScan (); Tablemapreduceutil.inittablemapperjob (args[0], scan, mymapper.class, Text.class, Text.class, Job); //Set input configuration information: Key, type of valueJob.setmapperclass (Mymapper.class); Job.setmapoutputkeyclass (Text.class); Job.setmapoutputvalueclass (Text.class); //reduce is not required to export data from hbase to HDFs, so the number of tasks to set reduce is 0Job.setnumreducetasks (0); //Set configuration information for output: Key, type of value, and output pathJob.setoutputkeyclass (Text.class); Job.setoutputvalueclass (Text.class); Job.setoutputformatclass (Textoutputformat.class); //if the output directory exists, delete the output directoryPath PATH =NewPath (args[2]); FileSystem FS= Filesystem.get (NewURI (args[2]),NewConfiguration ()); if(fs.exists (path)) {Fs.delete (path),true); } fileoutputformat.setoutputpath (Job,NewPath (args[2])); Job.waitforcompletion (true); }    Static classMymapperextendsTablemapper<text, text>{Text K2=NewText (); Text v2=NewText (); @Overrideprotected voidmap (immutablebyteswritable key, Result value, Mapper<immutablebyteswritable, Result, Text, text>. Context context)throwsIOException, interruptedexception {k2.set (""); String V2text= ""; String familycolumnslist= Context.getconfiguration (). Get ("Familycolumnslist"); String[] splited= Familycolumnslist.split (","); String title= "";//title             for(String split:splited) {string[] column= Split.split (":"); //get values based on column family, columnCell cell = Value.getcolumnlatestcell (Column[0].getbytes (), column[1].getbytes ()); //determines if the column gets the cell is not empty, otherwise it will report a null pointer error                if(cell!=NULL) {title+=NewString (Cellutil.clonequalifier (cell)) + ":" +NewString (Cellutil.clonequalifier (cell)) + "\ T" ; V2text+=NewString (Cellutil.clonevalue (cell)) + "\ T" ; }} v2.set (title+ "\ n" +v2text);        Context.write (K2, V2); }    }}

Implements a tool class that can export any number of columns of an hbase arbitrary table to any specified HDFs

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.