您好,登錄后才能下訂單哦!
這篇文章主要介紹了hbase developer API 1.22版是怎么樣的,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
示例代碼
package com.woozoom.hbase.test;import java.io.IOException;import java.util.*;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;public class BaseOperation {private static final String TABLE_NAME = "demo_table";public static Configuration conf = null;public HTable table = null;public HBaseAdmin admin = null;static {conf = HBaseConfiguration.create(); }/** * 創建一張表 */ public static void creatTable(String strTN, String[] familys)throws Exception {Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); TableName tn = TableName.valueOf(strTN);if (admin.tableExists(tn)) { System.out.println("table already exists!"); } else { HTableDescriptor tableDesc = new HTableDescriptor(tn);for (String colFamily : familys) { tableDesc.addFamily(new HColumnDescriptor(colFamily)); } admin.createTable(tableDesc); System.out.println("create table " + strTN + " ok."); } }/** * 刪除表 */ public static void deleteTable(String strTN) throws Exception {try {Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); TableName tn = TableName.valueOf(strTN);admin.disableTable(tn); admin.deleteTable(tn); System.out.println("delete table " + strTN + " ok."); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } }/** * 插入一行記錄 */ public static void addRecord(String strTN, String rowKey, String family, String qualifier, String value) throws Exception {try {Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn);Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); System.out.println("insert recored " + rowKey + " to table " + strTN + " ok."); } catch (IOException e) { e.printStackTrace(); } }/** * 刪除一行記錄 */ public static void delRecord(String strTN, String rowKey)throws IOException {Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn);List list = new ArrayList(); Delete del = new Delete(rowKey.getBytes()); list.add(del); table.delete(list); System.out.println("del recored " + rowKey + " ok."); }/** * 查找一行記錄 */ public static void getOneRecord(String strTN, String rowKey)throws IOException { Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn); Get get = new Get(rowKey.getBytes()); Result rs = table.get(get);for (Cell cell : rs.listCells()) { KeyValue kv = (KeyValue)cell; System.out.println("### " + new String(kv.getKey()) + " ###"); System.out.println("### " + new String(Arrays.copyOfRange(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyOffset() + cell.getFamilyLength())) + " ###"); System.out.println("### " + new String(Arrays.copyOfRange(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierOffset() + cell.getQualifierLength())) + " ###"); System.out.println("### " + new String(Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueOffset() + cell.getValueLength())) + " ###"); System.out.println("### " + cell.getTimestamp() + " ###"); }NavigableMap<byte[], NavigableMap<byte[], byte[]>> nm = rs.getNoVersionMap();for(byte[] family : nm.keySet()){ System.out.println(new String(family)); NavigableMap<byte[], byte[]> nmSon = nm.get(family);for (byte[] qualifier : nmSon.keySet()){ System.out.println(new String(qualifier));byte[] value = nmSon.get(qualifier); System.out.println(new String(value)); } } }/** * 顯示所有數據 */ public static void getAllRecord(String strTN) {try { Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn); Scan s = new Scan(); ResultScanner ss = table.getScanner(s);for (Result r : ss) {for (Cell cell : r.listCells()) { KeyValue kv = (KeyValue)cell; System.out.print(new String(kv.getKey()) + " "); System.out.print(new String(Arrays.copyOfRange(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyOffset() + kv.getFamilyLength())) + ":"); System.out.print(new String(Arrays.copyOfRange(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierOffset() + kv.getQualifierLength())) + " "); System.out.print(kv.getTimestamp() + " "); System.out.println(new String(Arrays.copyOfRange(kv.getValueArray(), kv.getValueOffset(), kv.getValueOffset() + kv.getValueLength()))); } }} catch (IOException e) { e.printStackTrace(); } }public static void main(String[] agrs) {try { String tablename = "scores1"; String[] familys = {"grade", "course"}; BaseOperation.creatTable(tablename, familys);// add record zkb BaseOperation.addRecord(tablename, "zkb", "grade", "", "5"); BaseOperation.addRecord(tablename, "zkb", "course", "", "90"); BaseOperation.addRecord(tablename, "zkb", "course", "math", "97"); BaseOperation.addRecord(tablename, "zkb", "course", "art", "87");// add record baoniu BaseOperation.addRecord(tablename, "baoniu", "grade", "", "4"); BaseOperation .addRecord(tablename, "baoniu", "course", "math", "89"); System.out.println("===========get one record========"); BaseOperation.getOneRecord(tablename, "zkb"); System.out.println("===========show all record========"); BaseOperation.getAllRecord(tablename); System.out.println("===========del one record========"); BaseOperation.delRecord(tablename, "baoniu"); BaseOperation.getAllRecord(tablename); System.out.println("===========show all record========"); BaseOperation.getAllRecord(tablename); } catch (Exception e) { e.printStackTrace(); } } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“hbase developer API 1.22版是怎么樣的”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。