HBase提供了對HBase進行一系列的管理涉及到對錶的管理、數據的操作java api。
常用的API操作有:
1、 對錶的創建、刪除、顯示以及修改等,可以用HBaseAdmin,一旦創建了表,那麼可以通過HTable的實例來訪問表,每次可以往表裏增加數據。
2、 插入數據
創建一個Put對象,在這個Put對象裏可以指定要給哪個列增加數據,以及當前的時間戳等值,然後通過調用HTable.put(Put)來提交操作,子猴在這裏提請注意的是:在創建Put對象的時候,你必須指定一個行(Row)值,在構造Put對象的時候作爲參數傳入。
3、 獲取數據
要獲取數據,使用Get對象,Get對象同Put對象一樣有好幾個構造函數,通常在構造的時候傳入行值,表示取第幾行的數據,通過HTable.get(Get)來調用。
4、 瀏覽每一行
通過Scan可以對錶中的行進行瀏覽,得到每一行的信息,比如列名,時間戳等,Scan相當於一個遊標,通過next()來瀏覽下一個,通過調用HTable.getScanner(Scan)來返回一個ResultScanner對象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一個Result。Result是一個
KeyValue的鏈表。
5、 刪除
使用Delete來刪除記錄,通過調用HTable.delete(Delete)來執行刪除操作。(注:刪除這裏有些特別,也就是刪除並不是馬上將數據從表中刪除。)
6、 鎖
新增、獲取、刪除在操作過程中會對所操作的行加一個鎖,而瀏覽卻不會。
7、 簇的訪問
客戶端代碼通過ZooKeeper來訪問找到簇,也就是說ZooKeeper quorum將被使用,那麼相關的類(包)應該在客戶端的類(classes)目錄下,即客戶端一定要找到文件hbase-site.xml。
- import java.io.IOException;
- 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.MasterNotRunningException;
- import org.apache.hadoop.hbase.ZooKeeperConnectionException;
- 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.Result;
- import org.apache.hadoop.hbase.client.ResultScanner;
- import org.apache.hadoop.hbase.client.Scan;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.util.Bytes;
- public class HBaseTest {
- private static Configuration conf = null;
- /**
- * 初始化配置
- */
- static {
- Configuration HBASE_CONFIG = new Configuration();
- //與hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同
- HBASE_CONFIG.set("hbase.zookeeper.quorum", "10.1.1.1");
- //與hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同
- HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
- conf = HBaseConfiguration.create(HBASE_CONFIG);
- }
- /**
- * 創建一張表
- */
- public static void creatTable(String tableName, String[] familys) throws Exception {
- HBaseAdmin admin = new HBaseAdmin(conf);
- if (admin.tableExists(tableName)) {
- System.out.println("table already exists!");
- } else {
- HTableDescriptor tableDesc = new HTableDescriptor(tableName);
- for(int i=0; i<familys.length; i++){
- tableDesc.addFamily(new HColumnDescriptor(familys[i]));
- }
- admin.createTable(tableDesc);
- System.out.println("create table " + tableName + " ok.");
- }
- }
- /**
- * 刪除表
- */
- public static void deleteTable(String tableName) throws Exception {
- try {
- HBaseAdmin admin = new HBaseAdmin(conf);
- admin.disableTable(tableName);
- admin.deleteTable(tableName);
- System.out.println("delete table " + tableName + " ok.");
- } catch (MasterNotRunningException e) {
- e.printStackTrace();
- } catch (ZooKeeperConnectionException e) {
- e.printStackTrace();
- }
- }
- /**
- * 插入一行記錄
- */
- public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value)
- throws Exception{
- try {
- HTable table = new HTable(conf, tableName);
- Put put = new Put(Bytes.toBytes(rowKey));
- put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
- table.put(put);
- System.out.println("insert recored " + rowKey + " to table " + tableName +" ok.");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 刪除一行記錄
- */
- public static void delRecord (String tableName, String rowKey) throws IOException{
- HTable table = new HTable(conf, tableName);
- 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 tableName, String rowKey) throws IOException{
- HTable table = new HTable(conf, tableName);
- Get get = new Get(rowKey.getBytes());
- Result rs = table.get(get);
- for(KeyValue kv : rs.raw()){
- System.out.print(new String(kv.getRow()) + " " );
- System.out.print(new String(kv.getFamily()) + ":" );
- System.out.print(new String(kv.getQualifier()) + " " );
- System.out.print(kv.getTimestamp() + " " );
- System.out.println(new String(kv.getValue()));
- }
- }
- /**
- * 顯示所有數據
- */
- public static void getAllRecord (String tableName) {
- try{
- HTable table = new HTable(conf, tableName);
- Scan s = new Scan();
- ResultScanner ss = table.getScanner(s);
- for(Result r:ss){
- for(KeyValue kv : r.raw()){
- System.out.print(new String(kv.getRow()) + " ");
- System.out.print(new String(kv.getFamily()) + ":");
- System.out.print(new String(kv.getQualifier()) + " ");
- System.out.print(kv.getTimestamp() + " ");
- System.out.println(new String(kv.getValue()));
- }
- }
- } catch (IOException e){
- e.printStackTrace();
- }
- }
- public static void main (String [] agrs) {
- try {
- String tablename = "scores";
- String[] familys = {"grade", "course"};
- HBaseTest.creatTable(tablename, familys);
- //add record zkb
- HBaseTest.addRecord(tablename,"zkb","grade","","5");
- HBaseTest.addRecord(tablename,"zkb","course","","90");
- HBaseTest.addRecord(tablename,"zkb","course","math","97");
- HBaseTest.addRecord(tablename,"zkb","course","art","87");
- //add record baoniu
- HBaseTest.addRecord(tablename,"baoniu","grade","","4");
- HBaseTest.addRecord(tablename,"baoniu","course","math","89");
- System.out.println("===========get one record========");
- HBaseTest.getOneRecord(tablename, "zkb");
- System.out.println("===========show all record========");
- HBaseTest.getAllRecord(tablename);
- System.out.println("===========del one record========");
- HBaseTest.delRecord(tablename, "baoniu");
- HBaseTest.getAllRecord(tablename);
- System.out.println("===========show all record========");
- HBaseTest.getAllRecord(tablename);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }