上一篇記錄了HBase的shell中的基本操作,這一篇看看用java來實(shí)現(xiàn)那些相應(yīng)的操作。
首先進(jìn)入hadoop目錄和Hbase目錄话瞧,將偽分布式hadoop環(huán)境和Hbase啟動(dòng):
# 進(jìn)入hadoop目錄
sbin/start-all.sh
# 進(jìn)入hbase目錄
bin/start-hbase.sh
然后在eclipse中建立一個(gè)mapreduce工程,這時(shí)候相應(yīng)的工程中已經(jīng)包含了開發(fā)hadoop程序用到的一些jar包。
不過這些jar包并沒有包含hbase要用到的凳忙,因此我們需要自己導(dǎo)入组去。導(dǎo)入的方法是右擊項(xiàng)目工程只壳,選擇 Properties->Java Build Path->Libraries->Add External JARs, 選擇添加所需的 jar 包:
我們需要的jar包在hbase的安裝(解壓縮)目錄下的lib目錄中,比如我的就是在/usr/local/hbase-1.1.7/lib圾浅,進(jìn)入后就能看到:
由于目前不清楚各種包里面包含了些什么腹尖,為了防止報(bào)錯(cuò)柳恐,我把這個(gè)目錄中的所有jar包都給導(dǎo)入到了工程中。
接著還要指定HBase配置文件的位置热幔,因?yàn)閆ookeeper的集群的信息在HBase的配置文件中(雖然我用的是HBase自帶的一個(gè)Zookeeper實(shí)例)乐设。
如何指定呢?可以將HBase的配置文件復(fù)制一份到工程里。先在工程目錄下創(chuàng)建一個(gè)名為conf的目錄绎巨,再將HBase的配置文件 hbase-site.xml 復(fù)制到該目錄下近尚。接著,還是右擊項(xiàng)目工程认烁,選擇 Properties->Java Build Path->Libraries->Add Class Folder, 將剛剛增加的conf目錄選上:
然后就可以進(jìn)行代碼的編寫了肿男。
首先來創(chuàng)建一個(gè)表:
public class HelloHBase {
private static Configuration conf = HBaseConfiguration.create();
//create a table
public static void createTable(String tableName,String[] familys) throws Exception{
@SuppressWarnings("deprecation")
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
System.out.println("table"+tableName+"already exists!");
}else{
@SuppressWarnings("deprecation")
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+" success!");
}
}
public static void main(String[] args){
try{
String tablename = "testTableName";
String[] familys = {"family1","family2"};
HelloHBase.createTable(tablename, familys);
}catch(Exception e){
e.printStackTrace();
}
}
}
寫好后,運(yùn)行看看:
打印結(jié)果說明我們創(chuàng)建成功了却嗡,但如何查看呢舶沛?這時(shí)候需要進(jìn)入終端,進(jìn)入 HBase Shell查看結(jié)果:
可以看到窗价,剛才創(chuàng)建的testTableName顯示了出來如庭,說明執(zhí)行成功了。
接下來就繼續(xù)測(cè)試表更新的接口撼港,表更新有Put(插入或更新)坪它,Delete(刪除),Append(添加)帝牡,Increment(增長)等往毡。
插入數(shù)據(jù):
//插入數(shù)據(jù),其中rowKey為行靶溜,family為列族开瞭,qualifier為列名懒震,value為數(shù)據(jù)
public static void addData(String tableName,String rowKey,String family,String qualifier,String value) throws Exception{
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 success!");
}
刪除一行數(shù)據(jù):
public static void deleteRow(String tableName,String rowKey) throws IOException{
HTable table = new HTable(conf,tableName);
Delete deleteRow = new Delete(rowKey.getBytes(rowKey));
table.delete(deleteRow);
System.out.println("delete row "+rowKey+" success!");
}
刪除一行中某列的數(shù)據(jù):
public static void deleteColumn(String tableName,String rowKey,String familyName,String columnName) throws IOException{
HTable table = new HTable(conf,tableName);
Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
deleteColumn.deleteColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
table.delete(deleteColumn);
System.out.println("delete "+rowKey+":"+familyName+":"+columnName+" success!");
}
添加數(shù)據(jù)(Append):
public static void appendData(String tableName,String rowKey,String family,String qualifier,String value) throws IOException{
HTable table = new HTable(conf,tableName);
Append append = new Append(Bytes.toBytes(rowKey));
append.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
table.append(append);
System.out.println("append data success!");
}
增長數(shù)據(jù)(Increment):
public static void incrementData(String tableName,String rowKey,String family,String qualifier,long amount) throws IOException{
HTable table = new HTable(conf,tableName);
Increment increment = new Increment(Bytes.toBytes(rowKey));
increment.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), amount);
table.increment(increment);
System.out.println("increment data success!");
}
接下來看看數(shù)據(jù)的讀取。
讀取一行數(shù)據(jù):
public static void getOneRow(String tableName,String rowKey) throws IOException{
HTable table = new HTable(conf,tableName);
//
Get get = new Get(rowKey.getBytes());
Result result = table.get(get);
//
for(KeyValue kv:result.raw()){
System.out.print("row: "+new String(kv.getRow())+" ");
System.out.print("family: "+new String(kv.getFamily())+" ");
System.out.print("qualifier: "+new String(kv.getQualifier())+" ");
System.out.print("timestamp: "+kv.getTimestamp()+" ");
System.out.println("value: "+new String(kv.getValue()));
}
}
掃描一個(gè)區(qū)段的數(shù)據(jù):
public static void scanRows(String tableName,String startRow,String stopRow) throws IOException{
HTable table = new HTable(conf,tableName);
//
Scan s = new Scan(startRow.getBytes(),stopRow.getBytes());
ResultScanner ss = table.getScanner(s);
for(Result r:ss){
for(KeyValue kv:r.raw()){
System.out.print("row: "+new String(kv.getRow())+" ");
System.out.print("family: "+new String(kv.getFamily())+" ");
System.out.print("qualifier: "+new String(kv.getQualifier())+" ");
System.out.print("timestamp: "+kv.getTimestamp()+" ");
System.out.println("value: "+new String(kv.getValue()));
}
}
}
還可以使用Filter來過濾一些數(shù)據(jù):
public static void scanByFilter(String tableName,String family,String qualifier,String value) throws IOException{
HTable table = new HTable(conf,tableName);
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(qualifier), CompareOp.EQUAL, Bytes.toBytes(value));
scan.setFilter(filter);
ResultScanner result = table.getScanner(scan);
//
for(Result r:result){
for(KeyValue kv:r.raw()){
System.out.print("row: "+new String(kv.getRow())+" ");
System.out.print("family: "+new String(kv.getFamily())+" ");
System.out.print("qualifier: "+new String(kv.getQualifier())+" ");
System.out.print("timestamp: "+kv.getTimestamp()+" ");
System.out.println("value: "+new String(kv.getValue()));
}
}
}
HBase基本用法就是這樣嗤详,可以在eclipse中運(yùn)行代碼后進(jìn)入hbase shell中查看對(duì)應(yīng)的表个扰。
代碼基本上進(jìn)行的是增刪改查,這樣也就更能看到HBase扮演的角色就相當(dāng)于在HDFS文件系統(tǒng)下的數(shù)據(jù)庫葱色。