這篇文章將為大家詳細(xì)講解有關(guān)HBase基本API操作之CRUD-Util怎么用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)來電聯(lián)系:18980820575,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)頁制作領(lǐng)域10多年,包括成都高空作業(yè)車租賃等多個(gè)領(lǐng)域擁有豐富的網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),選擇成都創(chuàng)新互聯(lián),為企業(yè)錦上添花!
一:創(chuàng)建HBaseUtil。
public class HBaseUtil { private static Configuration conf; private static Connection con; //初始化聯(lián)接 static{ //獲得配置文件對(duì)象: conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.226.129"); try { //獲得連接對(duì)象: con = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } //獲得連接: public static Connection getCon(){ if( con == null || con.isClosed() ){ try { con = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } return con; } //關(guān)閉連接: public static void closeCon(){ if( con != null ){ try { con.close(); } catch (IOException e) { e.printStackTrace(); } } } //創(chuàng)建表: public static void createTable(String tableName,String...FamilyColumn ){ TableName tn = TableName.valueOf(tableName); try { Admin admin = getCon().getAdmin(); HTableDescriptor htd = new HTableDescriptor(tn); for(String fc : FamilyColumn){ HColumnDescriptor hcd = new HColumnDescriptor(fc); htd.addFamily(hcd); } admin.createTable(htd); admin.close(); } catch (IOException e) { e.printStackTrace(); } } //刪除表: public static void dropTable(String tableName){ TableName tn = TableName.valueOf(tableName); try { Admin admin = con.getAdmin(); admin.disableTable(tn); admin.deleteTable(tn); admin.close(); } catch (IOException e) { e.printStackTrace(); } } //插入或更新數(shù)據(jù) public static boolean insert(String tableName,String rowKey,String family,String qualifier,String value){ try { Table table = con.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); return true; } catch (IOException e) { e.printStackTrace(); }finally{ // HBaseUtil.closeCon(); } return false; } //刪除數(shù)據(jù)記錄 public static boolean delete(String tableName,String rowKey,String family,String qualifier){ try { Table table = con.getTable(TableName.valueOf(tableName)); Delete del = new Delete( Bytes.toBytes(rowKey)); if( qualifier != null ){ del.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); }else if( family != null ){ del.addFamily( Bytes.toBytes(family) ); } table.delete(del); return true; } catch (IOException e) { e.printStackTrace(); }finally{ //HBaseUtil.closeCon(); } return false; } //刪除整行的數(shù)據(jù)記錄 public static boolean delete(String tableName,String rowKey){ return delete(tableName, rowKey, null, null); } //刪除某行某列的數(shù)據(jù)記錄 public static boolean delete(String tableName, String rowKey, String family){ return delete(tableName, rowKey, family, null); } //數(shù)據(jù)讀取 //取到一個(gè)值 public static String byGet(String tableName,String rowKey,String family,String qualifier){ try { Table table = con.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); Result result = table.get(get); return Bytes.toString(CellUtil.cloneValue( result.listCells().get(0))); } catch (IOException e) { e.printStackTrace(); } return null; } //取到一個(gè)族列的值 public static MapbyGet(String tableName,String rowKey, String family ){ Map map = null; try { Table table = getCon().getTable( TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addFamily( Bytes.toBytes( family )); Result result = table.get(get); List list = result.listCells(); map = (Map | ) (list.size() > 0 ? new HashMap () : result); for( Cell cell : list ){ map.put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); } } catch (IOException e) { e.printStackTrace(); } return map; } //取到多個(gè)列族的值 public static Map > byGet(String tableName,String rowKey){ Map > maps = null; try { Table table = con.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); List list = result.listCells(); maps = (Map | >) (list.size() >0 ? new HashMap >() : result); for( Cell cell : list){ String familyName = Bytes.toString(CellUtil.cloneFamily(cell)); if( maps.get(familyName) == null ){ maps.put(familyName, new HashMap () ); } maps.get(familyName).put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); } } catch (IOException e) { e.printStackTrace(); } return maps; } }
二:單元測試類 TestHBaseJUnit。
public class TestHBaseJUnit { //創(chuàng)建表并列出所有的表: @Test public void testCreateTable() throws IOException { //創(chuàng)建兩張表: person 與 student HBaseUtil.createTable("person", "famcolumn1","famcolumn2","famcolumn3"); HBaseUtil.closeCon(); HBaseUtil.createTable("student", "famcolumn1","famcolumn2","famcolumn3"); HBaseUtil.closeCon(); //列出所有表: Admin admin = HBaseUtil.getCon().getAdmin(); TableName[] tables = admin.listTableNames(); for (TableName tableName : tables) { System.out.println( "tableName: " + tableName ); } } ////判斷數(shù)據(jù)表是否存在。 @Test public void testTableIsExists() throws IOException{ TableName tn = TableName.valueOf("person"); //創(chuàng)建表名對(duì)象 Admin admin = HBaseUtil.getCon().getAdmin(); boolean isExists = admin.tableExists(tn); System.out.println( "person is Exists: "+ isExists ); } //刪除表 @Test public void testDropTable() throws IOException{ Admin admin = HBaseUtil.getCon().getAdmin(); TableName tn = TableName.valueOf("student"); System.out.println( admin.isTableDisabled(tn) ); HBaseUtil.dropTable("student"); admin = HBaseUtil.getCon().getAdmin(); //判斷數(shù)據(jù)表是否還存在。 boolean isExists = admin.tableExists(tn); System.out.println( "student is Exists: "+ isExists ); } //對(duì)表插入數(shù)據(jù)記錄 @Test public void testInsert() throws TableNotFoundException, IOException{ HBaseUtil.insert("person", "row1", "famcolumn1", "name", "Berg"); HBaseUtil.insert("person", "row1", "famcolumn1", "age", "22"); HBaseUtil.insert("person", "row1", "famcolumn1", "sex", "male"); HBaseUtil.insert("person", "row1", "famcolumn2", "name", "BergBerg"); HBaseUtil.insert("person", "row1", "famcolumn2", "age", "21"); HBaseUtil.insert("person", "row1", "famcolumn2", "sex", "male"); HBaseUtil.insert("person", "row1", "famcolumn3", "name", "BergBergBerg"); HBaseUtil.insert("person", "row1", "famcolumn3", "age", "23"); HBaseUtil.insert("person", "row1", "famcolumn3", "sex", "famale"); Admin admin = HBaseUtil.getCon().getAdmin(); TableName tn = TableName.valueOf("person"); System.out.println( admin.getTableDescriptor(tn) ); } //取到一個(gè)值 @Test public void testByGet1(){ String result = HBaseUtil.byGet("person", "row1", "famcolumn1", "name"); System.out.println( " result: " + result ); } //取到一個(gè)族列的值 @Test public void testByGet2(){ Mapresult = HBaseUtil.byGet("person", "row1", "famcolumn1"); System.out.println( " result: " + result ); } //取到多個(gè)列族的值 @Test public void testByGet3(){ Map > result = HBaseUtil.byGet("person", "row1"); System.out.println( " result: " + result ); } //刪除數(shù)據(jù)記錄 @Test public void testDelete1(){ HBaseUtil.delete("person", "row1", "famcolumn3", "age"); //刪除數(shù)據(jù)后: Map > result = HBaseUtil.byGet("person", "row1"); System.out.println( " result: " + result ); } //刪除某列數(shù)據(jù) @Test public void testTelete2(){ HBaseUtil.delete("person", "row1", "famcolumn3"); //刪除數(shù)據(jù)后: Map > result = HBaseUtil.byGet("person", "row1"); System.out.println( " result: " + result ); } //刪除整行的數(shù)據(jù) @Test public void testTelete3(){ HBaseUtil.delete("person", "row1"); //刪除數(shù)據(jù)后: Map > result = HBaseUtil.byGet("person", "row1"); System.out.println( " result: " + result ); } }
關(guān)于“HBase基本API操作之CRUD-Util怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。