1. 如何使用java API操作Hbase
一般情況下,我們使用Linux的shell命令,就可以非常輕松的操作Hbase,例如一些建表,建列簇,插值,顯示所有表,統計數量等等,但有時為了提高靈活性,我們也需要使用編程語言來操作Hbase,當然Hbase通過Thrift介面提供了對大多數主流編程語言的支持,例如C++,PHP,Python,Ruby等等,那麼本篇,散仙給出的例子是基於Java原生的API操作Hbase,相比其他的一些編程語言,使用Java操作Hbase,會更加高效一些,因為Hbase本身就是使用Java語言編寫的。轉載
下面,散仙給出源碼,以供參考:
package com.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;
/**
* @author
*
* **/
public class Test {
static Configuration conf=null;
static{
conf=HBaseConfiguration.create();//hbase的配置信息
conf.set("hbase.zookeeper.quorum", "10.2.143.5"); //zookeeper的地址
}
public static void main(String[] args)throws Exception {
Test t=new Test();
//t.createTable("temp", new String[]{"name","age"});
//t.insertRow("temp", "2", "age", "myage", "100");
// t.getOneDataByRowKey("temp", "2");
t.showAll("temp");
}
/***
* 創建一張表
* 並指定列簇
* */
public void createTable(String tableName,String cols[])throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);//客戶端管理工具類
if(admin.tableExists(tableName)){
System.out.println("此表已經存在.......");
}else{
HTableDescriptor table=new HTableDescriptor(tableName);
for(String c:cols){
HColumnDescriptor col=new HColumnDescriptor(c);//列簇名
table.addFamily(col);//添加到此表中
}
admin.createTable(table);//創建一個表
admin.close();
System.out.println("創建表成功!");
}
}
/**
* 添加數據,
* 建議使用批量添加
* @param tableName 表名
* @param row 行號
* @param columnFamily 列簇
* @param column 列
* @param value 具體的值
*
* **/
public void insertRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(row));
// 參數出分別:列族、列、值
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);
table.close();//關閉
System.out.println("插入一條數據成功!");
}
/**
* 刪除一條數據
* @param tableName 表名
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Delete d=new Delete(Bytes.toBytes(rowkey));
h.delete(d);//刪除一條數據
h.close();
}
/**
* 刪除多條數據
* @param tableName 表名
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey[])throws Exception{
HTable h=new HTable(conf, tableName);
List<Delete> list=new ArrayList<Delete>();
for(String k:rowkey){
Delete d=new Delete(Bytes.toBytes(k));
list.add(d);
}
h.delete(list);//刪除
h.close();//釋放資源
}
/**
* 得到一條數據
*
* @param tableName 表名
* @param rowkey 行號
* ***/
public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Get g=new Get(Bytes.toBytes(rowkey));
Result r=h.get(g);
for(KeyValue k:r.raw()){
System.out.println("行號: "+Bytes.toStringBinary(k.getRow()));
System.out.println("時間戳: "+k.getTimestamp());
System.out.println("列簇: "+Bytes.toStringBinary(k.getFamily()));
System.out.println("列: "+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
// System.out.println("值: "+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println("值: "+ss);
//}
}
h.close();
}
/**
* 掃描所有數據或特定數據
* @param tableName
* **/
public void showAll(String tableName)throws Exception{
HTable h=new HTable(conf, tableName);
Scan scan=new Scan();
//掃描特定區間
//Scan scan=new Scan(Bytes.toBytes("開始行號"),Bytes.toBytes("結束行號"));
ResultScanner scanner=h.getScanner(scan);
for(Result r:scanner){
System.out.println("==================================");
for(KeyValue k:r.raw()){
System.out.println("行號: "+Bytes.toStringBinary(k.getRow()));
System.out.println("時間戳: "+k.getTimestamp());
System.out.println("列簇: "+Bytes.toStringBinary(k.getFamily()));
System.out.println("列: "+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
// System.out.println("值: "+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println("值: "+ss);
//}
}
}
h.close();
}
}
顯示所有數據的列印輸出如下:
==================================
行號: 1
時間戳: 1385597699287
列簇: name
列: myname
值:
==================================
行號: 2
時間戳: 1385598393306
列簇: age
列: myage
值: 100
行號: 2
時間戳: 1385597723900
列簇: name
列: myname
值:
2. hbase命令行方式插入一條數據到某個表中使用什麼命令
命令:disable
'tableName'
--disable表。註:修改表結構時,必須要先disable表。
命令:enable
'tableName'
--使表可用
命令:drop
'tableName'
--刪除表
3. java怎樣給hbase插入數據
hbase 是動態列的,直接加就可以了,不用事先定義的啊
例如:(代碼沒有調試過,具體可看hbase的例子)
Table table = connection.getTable(TableName.valueOf(<表名>));
Put put = new Put(Bytes.toBytes(<主鍵字元串>));
put.addColumn(FieldFamily, Bytes.toBytes(<欄位名1>), Bytes.toBytes(<插入的值1>));
put.addColumn(FieldFamily, Bytes.toBytes(<欄位名2>), Bytes.toBytes(<插入的值2>));
.........
table.put(put);