hbase二级索引Observer

使用必须是线性安全:

package coprocessor;

import java.io.IOException;

import org.apache.hadoop.hbase.CoprocessorEnvironment;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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;
import org.apache.hadoop.hbase.util.Bytes;

public class AISObserver extends BaseRegionObserver {

	private HTablePool pool = null;
	HTableInterface table1 = null;
	private final byte[] tablename = Bytes.toBytes("zchx_ais_index");
	private final byte[] family = Bytes.toBytes("info");
	private final byte[] q = Bytes.toBytes("index");
	
	public void start(CoprocessorEnvironment env) throws IOException {
//		this.ShipAtable_index = new HTable(env.getConfiguration(), "zchx_ais_index");
		 pool = new HTablePool(env.getConfiguration(), 10);
		 table1 = pool.getTable(tablename);
		 table1.setAutoFlush(false);
	}

	@Override
	public void prePut(ObserverContext<RegionCoprocessorEnvironment> e,
			Put put, WALEdit edit, boolean writeToWAL) throws IOException {
		byte[] row = put.getRow();
		String rowkey = Bytes.toString(row);
		if(rowkey.length()==24){
			StringBuilder sb = new StringBuilder();
			sb.append(rowkey.substring(10, 24));
			String type = rowkey.substring(9,10);
			sb.append(rowkey.substring(0, 9));
			Put secPut = new Put(Bytes.toBytes(sb.toString()));
			secPut.add(family,q,row);
			if(type.equals("a")){
				table1.put(secPut);
			}
			
//			this.table.flushCommits();
			sb = null;
		}
		
	}

	@Override
	public void stop(CoprocessorEnvironment env) throws IOException {
		pool.close();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值