HBase提供了java api來對HBase進行一系列的管理涉及到對錶的管理、數據的操做等。經常使用的API操做有:
java
一、 對錶的建立、刪除、顯示以及修改等,能夠用HBaseAdmin,一旦建立了表,那麼能夠經過HTable的實例來訪問表,每次能夠往表裏增長數據。
二、 插入數據
建立一個Put對象,在這個Put對象裏能夠指定要給哪一個列增長數據,以及當前的時間戳等值,而後經過調用HTable.put(Put)來提交操做,在這裏提請注意的是:在建立Put對象的時候,你必須指定一個行(Row)值,在構造Put對象的時候做爲參數傳入。
三、 獲取數據數據庫
要獲取數據,使用Get對象,Get對象同Put對象同樣有好幾個構造函數,一般在構造的時候傳入行值,表示取第幾行的數據,經過HTable.get(Get)來調用。 apache
四、 瀏覽每一行api
經過Scan能夠對錶中的行進行瀏覽,獲得每一行的信息,好比列名,時間戳等,Scan至關於一個遊標,經過next()來瀏覽下一個,經過調用HTable.getScanner(Scan)來返回一個ResultScanner對象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一個Result。Result是一個Key/Value的鏈表。 eclipse
五、 刪除
使用Delete來刪除記錄,經過調用HTable.delete(Delete)來執行刪除操做。(注:刪除這裏有些特別,也就是刪除並非立刻將數據從表中刪除。)
六、 鎖
新增、獲取、刪除在操做過程當中會對所操做的行加一個鎖,而瀏覽卻不會。
七、 簇的訪問
客戶端代碼經過ZooKeeper來訪問找到簇,也就是說ZooKeeper quorum將被使用,那麼相關的類(包)應該在客戶端的類(classes)目錄下,即客戶端必定要找到文件hbase-site.xml。 函數
新建一個類:oop
package com.jhl.hbase; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; 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.util.Bytes; public class OperateTable { private static Configuration conf = null; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "master");// 使用eclipse時必須添加這個,不然沒法定位 conf.set("hbase.zookeeper.property.clientPort", "2181"); } // 建立數據庫表 @SuppressWarnings("deprecation") public static void createTable(String tableName, String[] columnFamilys) throws Exception { HBaseAdmin hadmin = new HBaseAdmin(conf); if (hadmin.tableExists(tableName)) { System.out.println("表已存在"); System.exit(0); } else { // 新建一個 表的描述 HTableDescriptor tableDesc = new HTableDescriptor(tableName); // 在描述裏添加列族 for (String columnFamily : columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } // 根據配置好的描述建表 hadmin.createTable(tableDesc); System.out.println("建立表成功!"); } } // 刪除數據庫表 public static void deleteTable(String tableName) throws Exception { HBaseAdmin hadmin = new HBaseAdmin(conf); if (hadmin.tableExists(tableName)) { // 關閉表 hadmin.disableTable(tableName); hadmin.deleteTable(tableName); System.out.println("刪除表成功!"); } else { System.out.println("刪除的表不存在"); System.exit(0); } } // 添加一條數據 public static void addRow(String tableName, String row, String columnFamily, String column, String value) throws Exception { HTable hTable = new HTable(conf, tableName); Put put = new Put(Bytes.toBytes(row)); // 參數出分別:列族、列、值 put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); hTable.put(put); } // 刪除一條數據 public static void delRow(String tableName, String row) throws Exception { HTable hTable = new HTable(conf, tableName); Delete delete = new Delete(Bytes.toBytes(row)); hTable.delete(delete); } // 刪除多條數據 public static void delMultiRows(String tableName, String[] rows) throws Exception { HTable hTable = new HTable(conf, tableName); List<Delete> list = new ArrayList<Delete>(); for (String row : rows) { Delete delete = new Delete(Bytes.toBytes(row)); list.add(delete); } hTable.delete(list); } // get row public static void getRow(String tableName, String row) throws Exception { HTable hTable = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(row)); Result result = hTable.get(get); for (KeyValue rowkv : result.raw()) { System.out.print("Row Name: " + new String(rowkv.getRow()) + " "); System.out.print("Timestamp: " + rowkv.getTimestamp() + " "); System.out.print("column Family: " + new String(rowkv.getFamily()) + " "); System.out.print("Row Name: " + new String(rowkv.getQualifier()) + " "); System.out.println("Value: " + new String(rowkv.getValue()) + " "); } } // get filter row public static void getFilterRow(String tableName, String row) throws Exception { HTable hTable = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(row)); ByteArrayComparable qualifierComparator = new SubstringComparator("course"); Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, qualifierComparator); get.setFilter(filter); Result result = hTable.get(get); System.out.println("result= " + result); for (KeyValue rowkv : result.raw()) { System.out.print("Row Name: " + new String(rowkv.getRow()) + " "); System.out.print("Timestamp: " + rowkv.getTimestamp() + " "); System.out.print("column Family: " + new String(rowkv.getFamily()) + " "); System.out.print("Row Name: " + new String(rowkv.getQualifier()) + " "); System.out.println("Value: " + new String(rowkv.getValue()) + " "); } } // get all records public static void getAllRows(String tableName) throws Exception { HTable hTable = new HTable(conf, tableName); Scan scan = new Scan(); ResultScanner results = hTable.getScanner(scan); for (Result result : results) { for (KeyValue rowKV : result.raw()) { System.out.print("Row Name: " + new String(rowKV.getRow()) + " "); System.out.print("Timestamp: " + rowKV.getTimestamp() + " "); System.out.print("column Family: " + new String(rowKV.getFamily()) + " "); System.out.print("Row Name: " + new String(rowKV.getQualifier()) + " "); System.out.println("Value: " + new String(rowKV.getValue()) + " "); } } } public static void main(String[] args) { String tableName = "score"; String[] columnFamilys = { "info", "course" }; try { // OperateTable.createTable(tableName, columnFamilys); // OperateTable.deleteTable(tableName); // 添加第一行數據 // OperateTable.addRow(tableName, "tht", "info", "age", "20"); // OperateTable.addRow(tableName, "tht", "info", "sex", "boy"); // OperateTable.addRow(tableName, "tht", "course", "china", "97"); // OperateTable.addRow(tableName, "tht", "course", "math", "128"); // OperateTable.addRow(tableName, "tht", "course", "english", "85"); // 添加第二行數據 // OperateTable.addRow(tableName, "xiaoxue", "info", "age", "19"); // OperateTable.addRow(tableName, "xiaoxue", "info", "sex", "boy"); // OperateTable.addRow(tableName, "xiaoxue", "course", "china", "90"); // OperateTable.addRow(tableName, "xiaoxue", "course", "math", "120"); // OperateTable.addRow(tableName, "xiaoxue", "course", "english", "90"); // 添加第三行數據 // OperateTable.addRow(tableName, "qingqing", "info", "age", "18"); // OperateTable.addRow(tableName, "qingqing", "info", "sex", "girl"); // OperateTable.addRow(tableName, "qingqing", "course", "china", "100"); // OperateTable.addRow(tableName, "qingqing", "course", "math","100"); // OperateTable.addRow(tableName, "qingqing", "course", "english","99"); // OperateTable.getRow(tableName, "xiaoxue"); // OperateTable.getAllRows(tableName); // OperateTable.delRow(tableName, "tht"); String[] rows = { "xiaoxue", "qingqing" }; OperateTable.delMultiRows(tableName, rows); OperateTable.getFilterRow(tableName, "tht"); } catch (Exception e) { e.printStackTrace(); } } }