在網(wǎng)瀏覽的時(shí)候 發(fā)現(xiàn)了這篇文章 很有用 就保留了下來(lái)
import java.io.IOException; import java.util.Iterator; import java.util.List; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver; import org.apache.hadoop.hbase.coprocessor.ObserverContext; import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; import org.apache.hadoop.hbase.regionserver.wal.WALEdit; public class postput_test extends BaseRegionObserver { @Override public void postPut(final ObserverContext<RegionCoprocessorEnvironment> e, final Put put, final WALEdit edit, final boolean writeToWAL) throws IOException { HTable table = new HTable("sunwg02"); List<KeyValue> kv = put.get("f1".getBytes(), "k1".getBytes()); Iterator<KeyValue> kvl = kv.iterator(); while(kvl.hasNext()) { KeyValue tmp = kvl.next(); Put tput = new Put(tmp.getValue()); tput.add("f1".getBytes(),"k1".getBytes(),tmp.getRow()); table.put(tput); } table.close(); }
hbase不是數(shù)據(jù)庫(kù),一些數(shù)據(jù)庫(kù)中基本的功能hbase并不具備.
二級(jí)索引就是其中很重要的一點(diǎn),在數(shù)據(jù)庫(kù)中索引是在平常不過(guò)的功能了.
而在hbase中,value上的索引只能靠自己來(lái)實(shí)現(xiàn).
hbase中最簡(jiǎn)單的二級(jí)索引的實(shí)現(xiàn)方式是通過(guò)另外一個(gè)hbase表來(lái)實(shí)現(xiàn).
下面通過(guò)postput方法,實(shí)現(xiàn)對(duì)表sunwg01的二級(jí)索引.
舉例說(shuō)下二級(jí)索引實(shí)現(xiàn):
表sunwg01的f1:k1有如下記錄
100 tom
101 mary
對(duì)于表sunwg01來(lái)說(shuō),可以通過(guò)100,101直接訪問(wèn)記錄,但是如果想要訪問(wèn)mary這條記錄,則只能全表遍歷
為了解決這個(gè)問(wèn)題,創(chuàng)建了表sunwg02
表sunwg02中的f1:k1有如下記錄
tom 100
mary 101
現(xiàn)在如果要查找mary這條記錄,可以先查表sunwg02中,找到mary的value的為101
下面通過(guò)postput方式實(shí)現(xiàn),在put源表的同時(shí)更新索引表的功能。
詳細(xì)代碼如下:
