HBase的基本api操作及簡要說明
阿新 • • 發佈:2019-02-16
hbase的jar包
HBase-0.98.6原始碼和安裝包下載
這裡用的是hbase-0.98.6-hadoop2-bin.tar.gz ,直接下載解壓後將對應的lib下的包匯入專案即可,這裡只是測試多一點,實際專案需要自己剔除一些不需要的jar包
hbase的java api
因為在程式碼裡新增了註釋,就不一一說明,下面直接上程式碼
import java.io.IOException;
import java.util.Map.Entry;
import java.util.NavigableMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HBaseDemo {
Configuration conf =null;
HBaseAdmin admin =null;
private long timestamp = HConstants.LATEST_TIMESTAMP;
static final long LATEST_TIMESTAMP = Long.MAX_VALUE;
@Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
//初始化操作開啟admin,ddl命令需要一個HBaseAdmin例項,所有的操作都需要HBaseConfiguration例項
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hddata1,hddata2,hddata3");
admin =new HBaseAdmin(conf);
}
@Test
public void createTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
HTableDescriptor htb = new HTableDescriptor(TableName.valueOf("useraa"));
HColumnDescriptor htc_info =new HColumnDescriptor("info");
HColumnDescriptor htc_data =new HColumnDescriptor("data");
htc_info.setMaxVersions(5);
htb.addFamily(htc_info);
htb.addFamily(htc_data);
admin.createTable(htb);
//admin.close(); //放到了單元測試的@after方法中
}
@Test
public void dropTable()throws Exception{
admin.disableTable("useraa");
admin.deleteTable("useraa");
}
@Test
public void alterTable() throws IOException{
HColumnDescriptor htc = new HColumnDescriptor("testColumn");
//新增一個列簇testColumn,其他屬性都為預設值
admin.addColumn("useraa", htc);
}
/**
* 資料的插入,可以用批量插入,批量插入的時候都放進put裡面提交即可
* */
@Test
public void putRow() throws IOException{
HTable table =new HTable(conf, TableName.valueOf("people"));
Put put= new Put(Bytes.toBytes("Java00001"));
//同時處理兩個cell,put會把其他型別轉換成byte[]
put.add(Bytes.toBytes("data"), Bytes.toBytes("size"),this.timestamp, Bytes.toBytes("data2"));
put.add(Bytes.toBytes("data"), Bytes.toBytes("nation"),this.timestamp, Bytes.toBytes("中國"));
table.put(put);
//table.flushCommits(); //一個可選的選項,顯式提交將資料重新整理到遠端伺服器端
table.close();
}
@Test
public void deleteCol() throws IOException{ //刪除資料,其實是刪除資料的標記,只有在重新整理之後資料才會消失
HTable ht = new HTable(conf, "people");
Delete del = new Delete(Bytes.toBytes("rk0001"));
del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("hello"));// 刪除 data:hello列
del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));//刪除info:name列
ht.delete(del); //提交刪除資訊
ht.close();
//如果沒有成功會有一個delete的List<Delete>,可以通過delete.size檢視未成功的,為成功的包括未掃描到的
System.out.println(del.size());
}
/**
* 資料的查詢,按照key查詢、全表掃描,這裡未實現Filter
* */
@SuppressWarnings("deprecation")
@Test
public void getRowKey() throws IOException{
HTable ht=new HTable(conf, "people");
Get get=new Get(Bytes.toBytes("Java00001"));
get.setMaxVersions(5);
Result rs =ht.get(get);
System.out.println("-----------方法一 keyValue獲取資料---------------");
for(KeyValue kv : rs.list()){ //keyvalue獲取資料
String family = new String(kv.getFamilyArray(),kv.getFamilyOffset(),kv.getFamilyLength());
System.out.println(family+":"+family);
System.out.println(kv.getTimestamp()+" "+new String(kv.getValue(),"utf-8")); //都轉換為utf-8,中文會亂碼
}
// NavigableMap<列簇,NavigableMap<列名,Navigable<時間戳,列值>>>
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map=rs.getMap();
System.out.println("------------方法二 NavigableMap獲取資料---------------");
for(byte[] family:map.keySet()){
NavigableMap<byte[], NavigableMap<Long, byte[]>> familyMap = map.get(family);//列簇作為key獲取其中的列相關資料
for(byte[] column:familyMap.keySet()){ //根據列名循壞
System.out.println(new String(family)+":"+new String(column));
NavigableMap<Long, byte[]> valuesMap = familyMap.get(column);
for(Entry<Long, byte[]> s:valuesMap.entrySet()){ //獲取列對應的不同版本資料,預設最新的一個
System.out.println(s.getKey() +" "+new String(s.getValue(),"utf-8"));
}
}
}
System.out.println(new String(rs.getValue(Bytes.toBytes("info"), Bytes.toBytes("size"))));
ht.close();
}
@SuppressWarnings("deprecation")
@Test
public void scanRow() throws IOException{
HTable ht=new HTable(conf, "people");
Scan scan = new Scan();
scan.setSmall(true);
ResultScanner scanner = ht.getScanner(scan);
System.out.println("-----------已經廢棄的方法list和raw-----");
for(Result r : scanner){
System.out.println("------"+new String(r.getRow())+"--------");
for(KeyValue kv : r.list()){
String family = new String(kv.getFamily());
System.out.println(family+":"+new String(kv.getQualifier()));
System.out.println(kv.getTimestamp()+" "+new String(kv.getValue(),"utf-8"));
}
System.out.println("-----------cell[]進行掃描-----");
for(Result rs : scanner){
for(Cell cell:rs.rawCells()){
String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String family= Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
String column= Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(row+"\n---"+family+":"+column+"\n "+value);
}
}
}
}
@After
public void end() throws IOException{
admin.close();
}
}
進階
以上只是一些簡單的增刪改查,那麼更深入的可以對hbase的過濾器的使用、rowkey的設計、hbase結合mapreduce的實現,比如對hdfs上的檔案進行更新、讀取等操作