hadoop-hbase-endpoint例子
来源:互联网 发布:淘宝评价多久更新一次 编辑:程序博客网 时间:2024/06/06 20:22
count下users表里使用google邮箱的用户数
服务端:
先定义个接口,extends CoprocessorProtocol,接口里定义个方法public long gmailCounter(String userId) throws IOException;
再定义个类,extends BaseEndpointCoprocessor,再implements刚才自己定义的接口GmailCounterProtocol,实现gmailCounter方法
在gmailCounter方法里先构造一个scan,然后InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment()).getRegion().getScanner(scan);
这个InternalScanner需要do-while循环取值,比较怪异
客户端:
先构造:Batch.Call<GmailCounterProtocol, Long> callable
然后EXEC:Map<byte[], Long> rs = table.coprocessorExec(GmailCounterProtocol.class, startKey, endKey, callable);,必须指定startKey和endKey范围
最后循环取结果:for (Map.Entry<byte[], Long> e : rs.entrySet()) { e.getValue().longValue(); }
接口
package test.hbase.inaction.example5_3;import java.io.IOException;import org.apache.hadoop.hbase.ipc.CoprocessorProtocol;public interface GmailCounterProtocol extends CoprocessorProtocol {public long gmailCounter(String userId) throws IOException;}
package test.hbase.inaction.example5_3;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.hadoop.hbase.KeyValue;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.coprocessor.BaseEndpointCoprocessor;import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.regionserver.InternalScanner;import org.apache.hadoop.hbase.util.Bytes;import test.hbase.inaction.example2_4.UsersDAO;public class UserEndpointServer extends BaseEndpointCoprocessor implementsGmailCounterProtocol {private final Log log = LogFactory.getLog(this.getClass());@Overridepublic long gmailCounter(String prefix) throws IOException {log.info("================================== begin");Scan scan = new Scan(prefix.getBytes());scan.setFilter(new PrefixFilter(prefix.getBytes()));scan.addColumn(UsersDAO.INFO_FAM, UsersDAO.EMAIL_COL);scan.setMaxVersions(1);RegionCoprocessorEnvironment env = (RegionCoprocessorEnvironment) getEnvironment();InternalScanner scanner = env.getRegion().getScanner(scan);long sum = 0;List<KeyValue> kvList = new ArrayList<KeyValue>();boolean hasMore = false;do {hasMore = scanner.next(kvList);for (KeyValue kv : kvList) {log.info(Bytes.toString(kv.getRow()) + ":"+ Bytes.toString(kv.getFamily()) + ":"+ Bytes.toString(kv.getQualifier()) + "="+ Bytes.toString(kv.getValue()));String email = Bytes.toString(kv.getValue());if (email != null && email.toLowerCase().endsWith("@gmail.com")) {sum++;}log.info("sum=" + sum);}kvList.clear();} while (hasMore);scanner.close();log.info("================================== end");return sum;}}
package test.hbase.inaction.example5_3;import java.io.IOException;import java.util.Arrays;import java.util.Map;import org.apache.hadoop.hbase.client.HTableInterface;import org.apache.hadoop.hbase.client.HTablePool;import org.apache.hadoop.hbase.client.coprocessor.Batch;import org.apache.hadoop.hbase.util.Bytes;import test.hbase.inaction.example2_4.UsersDAO;public class UserEndpointClient {private static HTablePool pool = new HTablePool();private static HTableInterface table = pool.getTable(UsersDAO.TABLE_NAME);/** * @param args * @throws Throwable * @throws IOException */public static void main(String[] args) throws Throwable {final String userId = "id";Batch.Call<GmailCounterProtocol, Long> callable = new Batch.Call<GmailCounterProtocol, Long>() {@Overridepublic Long call(GmailCounterProtocol instance) throws IOException {return instance.gmailCounter(userId);}};byte[] startKey = Bytes.toBytes(userId);byte[] endKey = Arrays.copyOf(startKey, startKey.length);endKey[endKey.length - 1]++;Map<byte[], Long> rs = table.coprocessorExec(GmailCounterProtocol.class, startKey, endKey, callable);long sum = 0;for (Map.Entry<byte[], Long> e : rs.entrySet()) {sum += e.getValue().longValue();}System.out.println(sum);}}
hbase-env.sh
export HBASE_CLASSPATH=/home/u/myjar/FollowsObserver.jar
hbase-site.xml
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://172.10.10.144:9000/hbase</value>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>hbase.coprocessor.region.classes</name>
<value>test.hbase.inaction.example5_3.UserEndpointServer</value>
</property>
</configuration>
HBase数据库log日志
2014-10-31 16:47:45,028 INFO test.hbase.inaction.example5_3.UserEndpointServer: id01:info:email=wyjxx@gmail.com
2014-10-31 16:47:45,028 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=1
2014-10-31 16:47:45,028 INFO test.hbase.inaction.example5_3.UserEndpointServer: id02:info:email=asdasdf@qq.com
2014-10-31 16:47:45,028 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=1
2014-10-31 16:47:45,029 INFO test.hbase.inaction.example5_3.UserEndpointServer: id03:info:email=xxx@gmail.com
2014-10-31 16:47:45,029 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=2
2014-10-31 16:47:45,029 INFO test.hbase.inaction.example5_3.UserEndpointServer: id04:info:email=yyy@163.com
2014-10-31 16:47:45,029 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=2
2014-10-31 16:47:45,030 INFO test.hbase.inaction.example5_3.UserEndpointServer: id05:info:email=zzz@sohu.com
2014-10-31 16:47:45,030 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=2
2014-10-31 16:47:45,030 INFO test.hbase.inaction.example5_3.UserEndpointServer: id06:info:email=test@gmail.com
2014-10-31 16:47:45,030 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=3
2014-10-31 16:47:45,030 INFO test.hbase.inaction.example5_3.UserEndpointServer: id07:info:email=test7@gmail.com
2014-10-31 16:47:45,030 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=4
2014-10-31 16:47:45,031 INFO test.hbase.inaction.example5_3.UserEndpointServer: id08:info:email=test8@gmail.com
2014-10-31 16:47:45,031 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=5
2014-10-31 16:47:45,031 INFO test.hbase.inaction.example5_3.UserEndpointServer: id09:info:email=test9@sina.com.cn
2014-10-31 16:47:45,031 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=5
2014-10-31 16:47:45,032 INFO test.hbase.inaction.example5_3.UserEndpointServer: id99:info:email=test99@gmail.com
2014-10-31 16:47:45,032 INFO test.hbase.inaction.example5_3.UserEndpointServer: sum=6
- hadoop-hbase-endpoint例子
- hbase 自定义 endpoint coprocessor
- HBase Coprocessor 之endpoint
- hbase 自定义 endpoint coprocessor
- HBase Endpoint编程示例
- Hbase Endpoint Coprocessor
- 在Hbase Endpoint Coprocessor中使用coprocessorProxy操作例子与问题解析
- hadoop-HBase-observer的一个例子
- Hadoop集群之上Hbase Shell例子
- 使用HBase EndPoint(coprocessor)进行计算
- 使用HBase EndPoint(coprocessor)进行计算
- 使用HBase EndPoint(coprocessor)进行计算
- hbase协处理器--创建endpoint协处理器
- 使用HBase EndPoint(coprocessor)进行计算
- HBASE例子
- Hadoop HBase
- Hadoop HBase
- hadoop hbase
- mysql 创建表 指定字符集
- 物化视图相关工具包
- 【Android】PopupWindow的简单运用
- hadoop+Mysql的关系(实现Hadoop运算结果插入到Mysql数据库)
- Android 图像算法处理
- hadoop-hbase-endpoint例子
- poj1509 Glass Beads sam
- 安装XEN
- 请允许别人比你优秀
- UVa 401 - Palindromes
- thinkphp中使用查询表达式
- java 集合交并补
- 常用网络命令(windows下)
- 递归方法遍历树节点,使用Dev的TreeList控件