1. Ensure that Hadoop and hbase services are up and running
2. Put the Hbase-site.xml,core-site.xml,hdfs-site.xml configuration file in the SRC directory of the Java project
3. Introduction of related dependency packages
4. Java client test access to HBase cluster
Package Com.hbase.test;import Java.util.arraylist;import Java.util.list;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.path;import Org.apache.hadoop.hbase.hbaseconfiguration;import Org.apache.hadoop.hbase.hcolumndescriptor;import Org.apache.hadoop.hbase.htabledescriptor;import Org.apache.hadoop.hbase.keyvalue;import Org.apache.hadoop.hbase.client.delete;import Org.apache.hadoop.hbase.client.get;import Org.apache.hadoop.hbase.client.hbaseadmin;import Org.apache.hadoop.hbase.client.htable;import Org.apache.hadoop.hbase.client.put;import Org.apache.hadoop.hbase.client.result;import Org.apache.hadoop.hbase.client.resultscanner;import Org.apache.hadoop.hbase.client.scan;import Org.apache.hadoop.hbase.filter.comparefilter;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; Import Org.apache.hadoop.hbase.filter.filter;import Org.apache.hadoop.hbase.filter.filterlist;import Org.apache.hadoop.hbase.filter.regexstringcomparator;import org.apache.hadoop.hbase.fiLter. Rowfilter;import Org.apache.hadoop.hbase.filter.singlecolumnvaluefilter;public class TestHBase {/** * @param args */ public static configuration config;/** * initialization parameter configuration information */static {configuration = Hbaseconfiguration.create ();//configuration file loading Way One//configuration.set ("Hbase.master", "10.58.51.78:60000");//configuration.set ("Hbase.zookeeper.quorum", " S1sf001,s1sf002,s1sf003,s1sf004,s1sf005 ");//configuration file load mode two string FilePath =" Hbase-site.xml "; Path PATH = new Path (filePath); Configuration.addresource (path);} /** * CREATE table based on given table name * @param tableName table name * @throws Exception */public static void CreateTable (String tableName) throws Exce ption {hbaseadmin admin = new Hbaseadmin (configuration), if (Admin.tableexists (TableName)) {admin.disabletable ( TableName); admin.deletetable (TableName); SYSTEM.OUT.PRINTLN ("Table already exists, first delete ..."); Htabledescriptor tabledescriptor = new Htabledescriptor (tableName); tabledescriptor.addfamily (New Hcolumndescriptor ( "CF1")); admin.createtable (Tabledescriptor); Admin.close ();} /** * Initialize Data * @Param tableName * @throws Exception */public static void InitData (String tableName) throws exception{htable table = new HT Able (configuration, tableName); for (int i=10;i<22;i++) {String II = string.valueof (i); Put put = new put (Ii.getbytes ());p Ut.add ("Cf1". GetBytes (), "Column1". GetBytes (), "The first column". GetBytes ()); Put.add ("Cf1". GetBytes (), "Column2". GetBytes (), "The second column". GetBytes ());p Ut.add ("Cf1". GetBytes (), "Column3" . GetBytes (), "The third column". GetBytes ()); Table.put (put);} Table.close ();} /** * Delete a row of data * @param tableName table name * @param rowKey rowKey * @throws Exception */public static void DeleteRow (String tablen Ame,string RowKey) throws exception{htable table = new Htable (configuration,tablename);D elete Delete = new Delete (RowKey. GetBytes ()); Table.delete (delete);} /** * Delete Rowkey list * @param tableName * @param rowkeys * @throws Exception */public static void Deleterowkeys (String tablen Ame, list<string> Rowkeys) throws exception{htable table = new Htable (Configuration, TableName); list<delete> deletes = new arraylist<delete> (); for (String rowkey:rowkeys) {Delete delete = new Delete ( Rowkey.getbytes ());d Eletes.add (delete);} Table.delete (deletes); Table.close ();} /** * gets all column values according to RowKey * @param tableName * @param rowKey * @throws Exception */public static void get (String tableName , String RowKey) throws exception{htable table = new htable (configuration, tableName); Get get = new Get (Rowkey.getbytes ()); Result result = Table.get (get), for (KeyValue Kv:result.raw ()) {System.out.println ("cf=" +new String (kv.getfamily ()) + ", Columnname= "+new string (Kv.getqualifier ()) +", value= "+new string (Kv.getvalue ()));}} /** * Bulk Enquiry * @param tableName * @param startrow * @param stoprow * @throws Exception * Select Column1,column2,column3 from test_table where ID between ... and */public static void scan (string tableName, String StartRow, String stoprow) throws E xception {htable table = new htable (configuration, tableName); Scan scan = new scan (); Scan.addcolumn ("Cf1 ". GetBytes ()," Column1 ". GetBytes ()), Scan.addcolumn (" Cf1 ". GetBytes ()," Column2 ". GetBytes ()); Scan.addcolumn (" Cf1 ". GetBytes ()," Column3 ". GetBytes ());//rowkey>=a && Rowkey<bscan.setstartrow (startrow.getbytes ()) ; Scan.setstoprow (Stoprow.getbytes ()); Resultscanner scanner = Table.getscanner (scan); for (Result Result:scanner) {for (KeyValue KeyValue:result.raw ()) { System.out.println (New String (keyvalue.getfamily ()) + ":" +new string (Keyvalue.getqualifier ()) + "=" +new string ( Keyvalue.getvalue ()));}}} /** * Single condition query: Test singlecolumnvaluefilter filter * @param tableName * @param columnvalue * @throws Exception * Less <less_or_e QUAL <=equal =not_equal <>greater_or_equal >=greater >no_op NO operation */public static void TestSingleCo Lumnvaluefilter (String tableName, String columnvalue) throws exception{htable table = new Htable (Configuration, TableName); Scan scan = new Scan (), Scan.addcolumn ("Cf1". GetBytes (), "Column1". GetBytes ()); Scan.addcolumn ("Cf1". GetBytes (), " Column2 ". GetBytes ()); Scan.addcolumn ("Cf1". GetBytes (), "Column3". GetBytes ());//Based on Rowkey query//rowfilter filter = new RowFilter ( Compareop.equal, New Regexstringcomparator ("1"));//query filter filter = new Singlecolumnvaluefilter ("CF1") through column. GetBytes (), "Column1". GetBytes (), comparefilter.compareop.greater,columnvalue.getbytes ()); Scan.setfilter (filter) ; Resultscanner scanner = Table.getscanner (scan); for (Result Result:scanner) {for (KeyValue KeyValue:result.raw ()) { System.out.println ("+new" string (Keyvalue.getrow ()) + "line," "+new string (keyvalue.getfamily ()) +": "+new string ( Keyvalue.getqualifier ()) + "=" +new String (Keyvalue.getvalue ()));} System.out.println ();}} /** * Fuzzy Match Rowkey * @param tableName * @param rowkeyregex * @throws Exception */public static void Fuzzyquerybyrowkey (Strin G TableName, String Rowkeyregex) throws exception{htable table = new htable (configuration, tableName); RowFilter filter = new RowFilter (compareop.equal, New Regexstringcomparator (Rowkeyregex));//prefixfilter filter = new Pr EfixFilter (Rowkeyregex.getbytes ()); Scan scan = new Scan (), Scan.addcolumn ("Cf1". GetBytes (), "Column1". GetBytes ()); Scan.addcolumn ("Cf1". GetBytes (), " Column2 ". GetBytes ()); Scan.addcolumn (" Cf1 ". GetBytes ()," Column3 ". GetBytes ()); Scan.setfilter (filter); Resultscanner scanner = Table.getscanner (scan); int num=0;for (Result result:scanner) {num + +; System.out.println ("RowKey:" +new String (Result.getrow ())); for (KeyValue KeyValue:result.raw ()) {System.out.println (New String (keyvalue.getfamily ()) + ":" +new string (Keyvalue.getqualifier ()) + "=" +new string (Keyvalue.getvalue ()));} System.out.println ();} SYSTEM.OUT.PRINTLN (num);} /** * Combination Criteria Query * @param tableName * @param column1 * @param column2 * @param column3 * @throws Exception */public static Voi D multiconditionquery (String tableName, String column1, String column2,string column3) throws exception{htable table = new Htable (configuration, tableName); Scan scan = new Scan (), Scan.addcolumn ("Cf1". GetBytes (), "Column1". GetBytes ()); Scan.addcolumn ("Cf1". GetBytes (), "Column2". GetBytes ()), Scan.addcolumn ("Cf1". GetBytes (), "Column3". GetBytes ()); Singlecolumnvaluefilter filter1 = new Singlecolumnvaluefilter ("Cf1". GetBytes (), "Column1". GetBytes (), Comparefilter.compareop.equal,column1.getbytes ()); Singlecolumnvaluefilter filter2 = new Singlecolumnvaluefilter ("Cf1". GetBytes (), "Column2". GetBytes (), Comparefilter.compareop.equal,column2.getbytes ()); Singlecolumnvaluefilter Filter3 = new Singlecolumnvaluefilter ("Cf1". GetBytes (), "Column3". GetBytes (), Comparefilter.compareop.equal,column3.getbytes ()); FilterList Filterall = new FilterList () Filterall.addfilter (filter1);//With SQL query in (?,?) The same effect filterlist filterlist = new filterlist (FilterList.Operator.MUST_PASS_ONE); Filterlist.addfilter (Filter2); Filterlist.addfilter (Filter3); Filterall.addfilter (filterlist); Scan.setfilter (Filterall); Resultscanner scanner = Table.getscanner (scan); for (Result Result:scanner) {System.out.println ("RowKey:" +new String ( Result.getrow ())); for (KeyValue KeyValue:result.raw ()) {System.ouT.println (New String (keyvalue.getfamily ()) + ":" +new string (Keyvalue.getqualifier ()) + "=" +new string ( Keyvalue.getvalue ()));} System.out.println ();}} public static void Main (string[] args) throws exception{//TODO auto-generated method stubcreatetable ("Panguoyuan_test_ Table-1 "); InitData (" panguoyuan_test_table-1 ");//deleterow (" Test_table "," 1 ");//list List = new ArrayList ();// List.add ("1"),//list.add ("2"),//deleterowkeys ("test_table", list),//get ("test_table", "1"),//scan ("test_table", " 1 "," 4 ");//testsinglecolumnvaluefilter (" Test_table "," 11111 ");//fuzzyquerybyrowkey (" Test_table "," 1 ");// Multiconditionquery ("test_table", "The first column", "The second column", "The third column");//fuzzyquerybyrowkey (" Test_table "," 1 ");//fuzzyquerybycolumn (" Test_table "," ee* ");}}
Javaapi access to hadoop2.2ha under configuration access Hbase0.96.2