协处理器可以将一部分运算工作移动到数据的存放端
协处理器允许用户在region服务器上运行自己的代码, 更准确地说是让用户可以在region上进行具体操作并且可以使用类似RDBMS数据库中trigger的功能
1 配置文件式 hbase-site.xml
<property>
<name>hbase.coprocessor.region.classes</name>
<value>hbase.observer.RegionObserverExample</value>
</property>
2 命令行式
删除
alter 'test', METHOD => 'table_att_unset',NAME => 'coprocessor$1'
添加
alter 'test','coprocessor'=>'file:/download/hbasejars/hadoop.sort.jar|hbase.observer.RegionObserverExample|210002|'
3 java 代码式
Path path = new Path("/share/hbaseone/lzlib/lzhbase-0.0.1-SNAPSHOT.jar");
tableDesc.setValue("HelloworldRegionObserver", "file:"+path.toString()+"|"+hbase.observer.RegionObserverExample.class.getCanonicalName()+"|"+Coprocessor.PRIORITY_USER);
package hbase.helloworld; /** import java.io.IOException; import org.apache.hadoop.conf.Configuration; public class HBaseHelloworld { /** if (true == HBaseHelloworld.delete(tableName)) { } HBaseHelloworld.create(tableName, columnFamily); HBaseHelloworld.get(tableName, "row1"); HBaseHelloworld.scan(tableName); } catch (Exception e) { static Configuration conf = HBaseConfiguration.create(); public static void create(String tableName, String columnFamily) // FileSystem fs = FileSystem.get(conf); public static void put(String tablename, String row, String columnFamily, HTable table = new HTable(conf, tablename); put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), table.put(put); System.out.println("put '" + row + "', '" + columnFamily + ":" + column } public static void get(String tablename, String row) throws Exception { public static void scan(String tableName) throws Exception { HTable table = new HTable(conf, tableName); for (Result r : rs) { } public static boolean delete(String tableName) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); |
package hbase.observer; import java.io.IOException; |
package hbase.observer; import java.io.IOException; import org.apache.hadoop.conf.Configuration; public class LoadWithTableDescriptorExample { |