Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileStatus;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path;ImportOrg.apache.hadoop.hbase.*;Importorg.apache.hadoop.hbase.client.*;ImportOrg.apache.hadoop.hbase.filter.Filter;ImportOrg.apache.hadoop.hbase.filter.PrefixFilter;Importorg.apache.hadoop.hbase.util.Bytes;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;Importjava.io.IOException;/*** Created by Administrator on 2017/8/16.*/ Public classtesthbase {Configuration Configuration=NULL; @Before Public voidSetUp ()throwsIOException {Configuration=hbaseconfiguration.create (); } @Test Public voidTestcreatetable ()throwsIOException {String tableName= "Jasontest"; Hbaseadmin Admin=Newhbaseadmin (configuration); Htabledescriptor desc=NewHtabledescriptor (tableName); Desc.addfamily (NewHcolumndescriptor ("Basic")); if(Admin.tableexists (tableName)) {System.out.println ("Table Exist"); }Else{admin.createtable (DESC); System.out.println ("CREATE table successfully."); } admin.close (); } @Test Public voidTestgetbyrow ()throwsIOException {String tableName= "User"; htable Table=Newhtable (configuration,tablename); byte[] Row1 = Bytes.tobytes ("1000"); Get Get=NewGet (ROW1); Result result=Table.get (GET); Cell[] Cells=Result.rawcells (); for(Cell cell:cells) {System.out.println (//Bytes.tostring (cellutil.clonefamily (cell)) + ":"//+ bytes.tostring (Cellutil.clonequalifier (cell)) + "-"//+ bytes.tostring (Cellutil.clonevalue (cell))// ); } table.close (); } @Test Public voidTestgetcolumn ()throwsIOException {String tableName= "User"; htable Table=Newhtable (configuration,tablename); Get Get=NewGet (Bytes.tobytes ("1000")); Get.addcolumn (Bytes.tobytes ("Info"), Bytes.tobytes ("name"))); Get.addcolumn (Bytes.tobytes ("Info"), Bytes.tobytes ("Age")); Result result=Table.get (GET); Cell[] Cells=Result.rawcells (); for(Cell cell:cells) {System.out.println ("Column Family is" + bytes.tostring (cellutil.clonefamily (cell)) + "|"//+ "Column is" + bytes.tostring (Cellutil.clonequalifier (cell)) + "|"//+ "Value is" +bytes.tostring (Cellutil.clonevalue (cell))); } table.close (); } @Test Public voidTestscantable ()throwsIOException {String tableName= "User"; htable Table=Newhtable (configuration,tablename); Scan Scan=NewScan ();//Scan.setstartrow (bytes.tobytes ("1001"));//Scan.setstoprow (bytes.tobytes ("1001"));//Scan.addcolumn (bytes.tobytes ("info"), Bytes.tobytes ("name"));Filter filer =NewPrefixfilter (Bytes.tobytes ("1000")); Scan.setfilter (filer); Resultscanner Resultscanner=Table.getscanner (scan); for(Result result:resultscanner) {cell[] cells=Result.rawcells (); for(Cell cell:cells) {System.out.println (//Bytes.tostring (Cellutil.clonerow (cell)) + ":"//+ bytes.tostring (cellutil.clonefamily (cell)) + ":"//+ bytes.tostring (Cellutil.clonequalifier (cell)) + "-"//+ bytes.tostring (Cellutil.clonevalue (cell))// ); }} table.close (); } @Test Public voidTestput ()throwsIOException {String tableName= "User"; htable Table=Newhtable (configuration,tablename); Put put=NewPut (Bytes.tobytes ("1004")); Put.add (Bytes.tobytes ("Info"), Bytes.tobytes ("name"), Bytes.tobytes ("Jason Zheng")); Put.add (Bytes.tobytes ("Info"), Bytes.tobytes ("Age"), Bytes.tobytes (28)); Table.put (Put); Table.close (); } @Test Public voidTestdelete ()throwsIOException {String tableName= "User"; htable Table=Newhtable (configuration,tablename); Delete Delete=NewDelete (Bytes.tobytes ("1004")); Delete.deletecolumns (Bytes.tobytes ("Info"), Bytes.tobytes ("name"))); Delete.deletecolumns (Bytes.tobytes ("Info"), Bytes.tobytes ("Age"));//delete.deletefamily (bytes.tobytes ("info"));Table.delete (delete); Table.close (); }}
HBase Java API Example