异步hbase的用法

异步hbase包asynchbase-1.4.1.jar,maven的pom如下:

<dependency>
  <groupId>org.hbase</groupId>
  <artifactId>asynchbase</artifactId>
  <version>1.4.1</version>
</dependency>

HBaseClient client = new HBaseClient("test036083.sqa.cm4.tbsite.net");

//生成一个PutRequest对象

 byte[][] qualifiers = new byte[kvs.size()][];
 byte[][] values = new byte[kvs.size()][];

PutRequest put = new PutRequest("table-name".getBytes(), "kv".getBytes(), "columnFamily".getBytes(), qualifiers, values, System.currentTimeMillis())

//异步写入

Deferred<Object> d= client.put(put);

d.addErrback(new Callback<Object, Exception> {
        @Override
        public Object call(final Exception arg) {
            if (arg instanceof PleaseThrottleException) {
                final PleaseThrottleException e = (PleaseThrottleException) arg;
                LOG.warn("Need to throttle, HBase isn't keeping up.", e);
                throttle = true;
                final HBaseRpc rpc = e.getFailedRpc();
                if (rpc instanceof PutRequest) {
                    client.put((PutRequest) rpc); // Don't lose edits.
                }
                return null;
            }
            return arg;
        }

        @Override
        public String toString() {
            return "importFile errback";
        });

Deferred类似于JDK中的异步线程返回对象Future,这里加了个Callback对象是当Deferred为available时且是一个异常,就会执行加入的Callback对象。

异步hbase大大提供了大数据量下hbase的写入性能,是一个不错的优化点~

相关推荐