关注表里添加一条信息,那么粉丝表里也添加一条对应信息
package hbase_mr;
import HbaseApi.HbaseUtilTool;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
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;
import java.io.IOException;
public class IndexOBserver extends BaseRegionObserver {
@Override
public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put,
WALEdit edit, Durability durability) throws IOException {
String rowkey = Bytes.toString(put.getRow());
String[] s = rowkey.split("_");
String newRowkey=s[1]+"_"+s[0];
Put newPut = new Put(Bytes.toBytes(newRowkey));
CellScanner cellScanner = put.cellScanner();
Boolean flag=false;
while (cellScanner.advance()){
Cell current = cellScanner.current();
String columnName = new String(CellUtil.cloneQualifier(current));
String columnValue = new String(CellUtil.cloneValue(current));
if(columnName.equals("from")){
newPut.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("to"),Bytes.toBytes(columnValue));
flag=true;
}else if(columnName.equals("to")){
newPut.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("from"),Bytes.toBytes(columnValue));
flag=true;
}
if(flag){
Table table = HbaseUtilTool.getTable("ns1:t_fensi");
table.put(newPut);
}
super.prePut(e, put, edit, durability);
}
}
}