運行Eclipse,建立一個新的Java工程“HBaseClient”,右鍵項目根目錄,選擇 “Properties”->“Java Build Path”->“Library”->“Add External JARs”,將HBase解壓後根目錄下的hbase-0.94.1-security.jar、hbase-0.94.1-security-tests.jar和lib子目錄下所有jar 包添加到本工程的Classpath下。
HBase提供了java api來對HBase進行一系列的管理涉及到對錶的管理、資料的操作等。常用的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 {
conf = HBaseConfiguration.create();
}
/**
* 建立一張表
*/
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();
}
}
}
結果顯示為:
create table scores ok.
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored baoniu to table scores ok.
insert recored baoniu to table scores ok.
===========get one record========
zkb course: 1345450733304 90
zkb course:art 1345450733323 87
zkb course:math 1345450733316 97
zkb grade: 1345450733294 5
===========show all record========
baoniu course:math 1345450733333 89
baoniu grade: 1345450733328 4
zkb course: 1345450733304 90
zkb course:art 1345450733323 87
zkb course:math 1345450733316 97
zkb grade: 1345450733294 5
===========del one record========
del recored baoniu ok.
zkb course: 1345450733304 90
zkb course:art 1345450733323 87
zkb course:math 1345450733316 97
zkb grade: 1345450733294 5
===========show all record========
zkb course: 1345450733304 90
zkb course:art 1345450733323 87
zkb course:math 1345450733316 97
zkb grade: 1345450733294 5
HBase 的詳細介紹:請點這裡
HBase 的:請點這裡
Hadoop+HBase搭建雲端儲存總結 PDF
HBase 結點之間時間不一致造成regionserver啟動失敗
Hadoop+ZooKeeper+HBase叢集配置
Hadoop叢集安裝&HBase實驗環境搭建
基於Hadoop叢集的HBase叢集的配置 ‘
Hadoop安裝部署筆記之-HBase完全分布模式安裝
單機版搭建HBase環境圖文教程詳解