搜索
首页数据库mysql教程Hbase的协处理器

1.起因(Why HBase Coprocessor) HBase作为列族数据库最经常被人诟病的特性包括:无法轻易建立“二级索引”,难以执行求和、计数、排序等操作。比如,在旧版本的(0.92)Hbase中,统计数据表的总行数,需要使用Counter方法,执行一次MapReduce Job才能得到。虽

1.起因(Why HBase  Coprocessor)

HBase作为列族数据库最经常被人诟病的特性包括:无法轻易建立“二级索引”,难以执行求和、计数、排序等操作。比如,在旧版本的(

2.灵感来源( Source of Inspration)

HBase协处理器的灵感来自于Jeff Dean 09年的演讲( P66-67)。它根据该演讲实现了类似于bigtable的协处理器,包括以下特性:

  • 每个表服务器的任意子表都可以运行代码
  • 客户端的高层调用接口(客户端能够直接访问数据表的行地址,多行读写会自动分片成多个并行的RPC调用)
  • 提供一个非常灵活的、可用于建立分布式服务的数据模型
  • 能够自动化扩展、负载均衡、应用请求路由
HBase的协处理器灵感来自bigtable,但是实现细节不尽相同。HBase建立了一个框架,它为用户提供类库和运行时环境,使得他们的代码能够在HBase region server和master上处理。

3.细节剖析(Implementation)

协处理器分两种类型,系统协处理器可以全局导入region server上的所有数据表,表协处理器即是用户可以指定一张表使用协处理器。协处理器框架为了更好支持其行为的灵活性,提供了两个不同方面的插件。一个是观察者(observer),类似于关系数据库的触发器。另一个是终端(endpoint),动态的终端有点像存储过程。

 3.1观察者(Observer)

观察者的设计意图是允许用户通过插入代码来重载协处理器框架的upcall方法,而具体的事件触发的callback方法由HBase的核心代码来执行。协处理器框架处理所有的callback调用细节,协处理器自身只需要插入添加或者改变的功能。

以HBase0.92版本为例,它提供了三种观察者接口:

  • RegionObserver:提供客户端的数据操纵事件钩子:Get、Put、Delete、Scan等。
  • WALObserver:提供WAL相关操作钩子。
  • MasterObserver:提供DDL-类型的操作钩子。如创建、删除、修改数据表等。

这些接口可以同时使用在同一个地方,按照不同优先级顺序执行.用户可以任意基于协处理器实现复杂的HBase功能层。HBase有很多种事件可以触发观察者方法,这些事件与方法从HBase0.92版本起,都会集成在HBase API中。不过这些API可能会由于各种原因有所改动,不同版本的接口改动比较大,具体参考Java Doc。

RegionObserver工作原理,如图1所示。更多关于Observer细节请参见HBaseBook的第9.6.3章节。

regionobserver.png

图1 RegionObserver工作原理

 

3.2终端(Endpoint)

终端是动态RPC插件的接口,它的实现代码被安装在服务器端,从而能够通过HBase RPC唤醒。客户端类库提供了非常方便的方法来调用这些动态接口,它们可以在任意时候调用一个终端,它们的实现代码会被目标region远程执行,结果会返回到终端。用户可以结合使用这些强大的插件接口,为HBase添加全新的特性。终端的使用,如下面流程所示:

  1. 定义一个新的protocol接口,必须继承CoprocessorProtocol.
  2. 实现终端接口,该实现会被导入region环境执行。
  3. 继承抽象类BaseEndpointCoprocessor.
  4. 在客户端,终端可以被两个新的HBase Client API调用 。单个region:HTableInterface.coprocessorProxy(Class protocol, byte[] row) 。rigons区域:HTableInterface.coprocessorExec(Class protocol, byte[] startKey, byte[] endKey, Batch.Call callable)

整体的终端调用过程范例,如图2所示:

rpc.png

图2 终端调用过程范例

4.编程实践(Code Example)

在该实例中,我们通过计算HBase表中行数的一个实例,来真实感受协处理器 的方便和强大。在旧版的HBase我们需要编写MapReduce代码来汇总数据表中的行数,在0.92以上的版本HBase中,只需要编写客户端的代码即可实现,非常适合用在WebService的封装上。

4.1启用协处理器 Aggregation(Enable Coprocessor Aggregation)

我们有两个方法:1.启动全局aggregation,能过操纵所有的表上的数据。通过修改hbase-site.xml这个文件来实现,只需要添加如下代码:

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)"><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,0,0)">property</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">></span>
   <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)"><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,0,0)">name</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">></span>hbase.coprocessor.user.region.classes<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)"></span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,0,0)">name</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">></span>
   <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)"><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,0,0)">value</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">></span>org.apache.hadoop.hbase.coprocessor.AggregateImplementation<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)"></span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,0,0)">value</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">></span>
 <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)"></span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,0,0)">property</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">></span></span></span></span>

2.启用表aggregation,只对特定的表生效。通过HBase Shell 来实现。

(1)disable指定表。hbase> disable 'mytable'

(2)添加aggregation hbase> alter 'mytable', METHOD => 'table_att','coprocessor'=>'|org.apache.hadoop.hbase.coprocessor.AggregateImplementation||'

(3)重启指定表 hbase> enable 'mytable'

4.2统计行数代码(Code Snippet) 

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
import org.apache.hadoop.hbase.util.Bytes;

public class MyAggregationClient {

	private static final byte[] TABLE_NAME = Bytes.toBytes("bigtable1w");
	private static final byte[] CF = Bytes.toBytes("bd");
	public static void main(String[] args) throws Throwable {
	Configuration customConf = new Configuration();
	customConf.set("hbase.zookeeper.quorum",
	"192.168.58.101");
	//提高RPC通信时长
	customConf.setLong("hbase.rpc.timeout", 600000);
	//设置Scan缓存
	customConf.setLong("hbase.client.scanner.caching", 1000);
	Configuration configuration = HBaseConfiguration.create(customConf);
	AggregationClient aggregationClient = new AggregationClient(
	configuration);
	Scan scan = new Scan();
	//指定扫描列族,唯一值
	scan.addFamily(CF);
	//long rowCount = aggregationClient.rowCount(TABLE_NAME, null, scan);
	long rowCount = aggregationClient.rowCount(TableName.valueOf("bigtable1w"), new LongColumnInterpreter(), scan);
	System.out.println("row count is " + rowCount);

	}
	


}


4.3 典型例子

  协处理器其中的一个作用是使用Observer创建二级索引。先举个实际例子: 
  我们要查询指定店铺指定客户购买的订单,首先有一张订单详情表,它以被处理后的订单id作为rowkey;其次有一张以客户nick为rowkey的索引表,结构如下: 

rowkey family 
dp_id+buy_nick1 tid1:null tid2:null ... 
dp_id+buy_nick2 tid3:null 
... 
该表可以通过Coprocessor来构建,实例代码: 

[html] view plain copy  在CODE上查看代码片派生到我的代码片
  1. public class TestCoprocessor extends BaseRegionObserver {   
  2.     @Override   
  3.      public void prePut(final ObserverContextRegionCoprocessorEnvironment> e,   
  4.      final Put put, final WALEdit edit, final boolean writeToWAL)   
  5.      throws IOException {   
  6.          Configuration conf = new Configuration();   
  7.          HTable table = new HTable(conf, "index_table");   
  8.          ListKeyValue> kv = put.get("data".getBytes(), "name".getBytes());   
  9.          IteratorKeyValue> kvItor = kv.iterator();   
  10.          while (kvItor.hasNext()) {   
  11.              KeyValue tmp = kvItor.next();   
  12.              Put indexPut = new Put(tmp.getValue());   
  13.              indexPut.add("index".getBytes(), tmp.getRow(), Bytes.toBytes(System.currentTimeMillis()));   
  14.              table.put(indexPut);   
  15.          }   
  16.          table.close();   
  17.      }   
  18. }   

即继承BaseRegionObserver类,实现prePut方法,在插入订单详情表之前,向索引表插入索引数据。 

4.4索引表的使用 

先在索引表get索引表,获取tids,然后根据tids查询订单详情表。 
当有多个查询条件(多张索引表),根据逻辑运算符(and 、or)确定tids。 


4.5使用时注意 


1.索引表是一张普通的hbase表,为安全考虑需要开启Hlog记录日志。 
2.索引表的rowkey最好是不可变量,避免索引表中产生大量的脏数据。 
3.如上例子,column是横向扩展的(宽表),rowkey设计除了要考虑region均衡,也要考虑column数量,即表不要太宽。建议不超过3位数。 
4.如上代码,一个put操作其实是先后向两张表put数据,为保证一致性,需要考虑异常处理,建议异常时重试。 

4.6效率情况 


put操作效率不高,如上代码,每插入一条数据需要创建一个新的索引表连接(可以使用htablepool优化),向索引表插入数据。即耗时是双倍的,对hbase的集群的压力也是双倍的。当索引表有多个时,压力会更大。 
查询效率比filter高,毫秒级别,因为都是rowkey的查询。 
如上是估计的效率情况,需要根据实际业务场景和集群情况而定,最好做预先测试。 

4.7Coprocessor二级索引方案优劣 


优点:在put压力不大、索引region均衡的情况下,查询很快。 
缺点:业务性比较强,若有多个字段的查询,需要建立多张索引表,需要保证多张表的数据一致性,且在hbase的存储和内存上都会有更高的要求
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能