Package com.bi.net;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.hbase.*;
Import org.apache.hadoop.hbase.client.*;
Import Org.apache.hadoop.hbase.filter.CompareFilter;
Import org.apache.hadoop.hbase.filter.FilterList;
Import Org.apache.hadoop.hbase.filter.PrefixFilter;
Import Org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
Import Org.junit.Before;
Import Org.junit.Test;
Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Random;
/** * Created by Dengni on 2016/6/18.
* * Public class Hbasejdbc {public static Configuration conf = null;
public static TableName tab = tablename.valueof ("Hbaseinword". GetBytes ());
public static TableName Hbaseinword = Tablename.valueof ("Hbaseinword");
public static Random RA = new Random ();
/** * 1 CREATE TABLE requires insert (put) of the Hbaseadmin class * 2 data, update, select by or scan are operated via the Htable class * @Before PubLIC void Init () {conf = Hbaseconfiguration.create ();
Conf.set ("Hbase.zookeeper.quorum", "bigdataspark:2181,bigdatacloud:2181,bigdatahadoop:2181"); @Test public void Createnewtableonhbase () throws IOException {hbaseadmin hbaseadmin = new Hbaseadmin (
conf);
if (hbaseadmin.tableexists (tab)) {Hbaseadmin.disabletable (tab);
hbaseadmin.deletetable (tab);
} htabledescriptor Tabledesc = new htabledescriptor (tab);
Hcolumndescriptor Coldesc = new Hcolumndescriptor ("Cfone");
Coldesc.setblockcacheenabled (TRUE);
Coldesc.setblocksize (1280);
Coldesc.setmaxversions (16);
Tabledesc.addfamily (COLDESC);
Hbaseadmin.createtable (TABLEDESC); Base (main):015:0> list cmd Check if the table is been create on HBase} @Test public void Insertintoh
Baseinword () throws IOException {htable table = new htable (conf, Hbaseinword); ListList = new arraylist<put> ();
for (int i = 999 i >= 0; i--) {Put putvalue = new put (string.valueof (i). GetBytes ());
Putvalue.add ("Cfone". GetBytes (), "name". GetBytes (), "Zl". GetBytes ());
Putvalue.add ("Cfone". GetBytes (), "Age". GetBytes (), string.valueof (i% 102). GetBytes ());
List.add (Putvalue);
} table.put (list); @Test public void Searchfromhbaseinwordbyget () throws IOException {htable table = new htable (conf, HB
Aseinword);
Get get = new Get ("999". GetBytes ());
Result result = Table.get (get);
Cell cell = Result.getcolumnlatestcell ("Cfone". GetBytes (), "name". GetBytes ());
byte[] bytes = Cellutil.clonevalue (cell);
System.out.println (New String (bytes, "UTF-8"));
@Test public void Searchbyscan () throws IOException {htable table = new htable (conf, Hbaseinword); Scan Scan = new Scan ("900". GetBytes (), "999". GetBytes ());
Resultscanner result = Table.getscanner (scan);
Iterator<result> iterator = Result.iterator ();
while (Iterator.hasnext ()) {Result result1 = Iterator.next ();
Cell cell = Result1.getcolumnlatestcell ("Cfone". GetBytes (), "name". GetBytes ());
byte[] Value = cellutil.clonevalue (cell);
byte[] row = Cellutil.clonerow (cell);
System.out.println (New string (Row, "UTF-8") + "" + new String (value, "UTF-8")); @Test public void Deleteonerow () throws IOException {htable table = new htable (conf, hbaseinwo
RD);
Delete delete = new Delete ("988". GetBytes ());
Table.delete (delete);
}/** * A more complicated example/@Test public void Insertput () throws IOException {
htable table1 = new htable (conf, Hbaseinword);
list<put> list = new arraylist<put> ();
for (int i = 0; i < 10000; i++) { Put on = new put (Generaterowkey ("1531187321"). GetBytes ());
Put.add ("Cfone". GetBytes (), "addr". GetBytes (), "Shanghai". GetBytes ());
Put.add ("Cfone". GetBytes (), "type". GetBytes (), String.valueof (Ra.nextint (2)). GetBytes ());
Put.add ("Cfone". GetBytes (), "Phone". GetBytes (), Generatephone ("153"). GetBytes ());
Put.add ("Cfone". GetBytes (), "Time". GetBytes (), string.valueof (Ra.nextint). GetBytes ());
List.add (Put);
} table1.put (list);
private String Generaterowkey (string s) {StringBuffer sb = new StringBuffer (s); Sb.append ("_2016"). Append (String.Format ("%02d", Ra.nextint (+1)). Append (String.Format ("%02d", Ra.nextint ( +1)). Append (String.Format ("%02d", Ra.nextint)). Append (String.Format ("%02d", Ra.next
INT)). Append (String.Format ("%02d", Ra.nextint (60));
return sb.tostring (); } private String GeneratePhone (String p) {StringBuffer sb = new StringBuffer (p);
Sb.append (String.Format ("%08d", Ra.nextint (999999999));
return sb.tostring (); @Test public void Searchbyscaninterval () throws IOException {htable table = new Htable (conf,hbaseinwo
RD);
Scan Scan = new Scan ("1531187321_20160200000000". GetBytes (), "1531187321_20160301000000". GetBytes ());
Resultscanner scanner = Table.getscanner (scan);
Iterator<result> iterator = Scanner.iterator ();
while (Iterator.hasnext ()) {result result = Iterator.next ();
Cell column = Result.getcolumnlatestcell ("Cfone". GetBytes (), "Phone". GetBytes ());
byte[] row = cellutil.clonerow (column);
byte[] Value = cellutil.clonevalue (column);
System.out.println (new string (row) + "" + new String (value)); @Test public void Findbycontition () throws IOException {htable table = new Htable (conf, Hbaseinword);
Scan Scan = new Scan ();
filterlist filterlist = new filterlist (FilterList.Operator.MUST_PASS_ALL);
Prefixfilter prefixfilter = new Prefixfilter ("1531187321_201610". GetBytes ()); Singlecolumnvaluefilter singlecolumnvaluefilter = new Singlecolumnvaluefilter ("Cfone". GetBytes (), "type". GetBytes ()
, CompareFilter.CompareOp.EQUAL, "1". GetBytes ());
Filterlist.addfilter (Prefixfilter);
Filterlist.addfilter (Singlecolumnvaluefilter);
Scan.setfilter (filterlist);
Resultscanner scanner = Table.getscanner (scan);
Iterator<result> iterator = Scanner.iterator ();
while (Iterator.hasnext ()) {result result = Iterator.next ();
Cell Columnlatestcell = Result.getcolumnlatestcell ("Cfone". GetBytes (), "Phone". GetBytes ());
byte[] va = cellutil.clonevalue (Columnlatestcell);
byte[] row = Cellutil.clonerow (Columnlatestcell); System.out.println (new String (ROW) +"" +new String (VA);
}
}
}