HBase新的客户端接口

最近学习接触HBase的东西,看了《Habase in Action》,但里面关于HBase接口都是过时的接口,以下为HBase新的客户端接口:

package com.n10k;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class NewClientAPIExample extends Configured implements Tool {

  /** 定义HBase的表. */
  private static final TableName TABLE_NAME = TableName.valueOf("MyTable");
  /** 列族名 */
  private static final byte[] CF = Bytes.toBytes("cf1");

  public int run(String[] argv) throws IOException {
    setConf(HBaseConfiguration.create(getConf()));

    /** 连接集群. 一个单连接被所有应用线程共享. */
    Connection connection = null;
    /* 一个针对特定表的轻量级句柄。被一个单线程使用*/
    Table table = null;
    try {
      // 建立到集群的连接
      connection = ConnectionFactory.createConnection(getConf());
      // 拿到目标表的句柄
      table = connection.getTable(TABLE_NAME);
      //描述我们想要写的数据
      Put p = new Put(Bytes.toBytes("someRow"));
      p.addColumn(CF, Bytes.toBytes("qual"), Bytes.toBytes(42.0d));
      // 发送数据
      table.put(p);
    } finally {
      // 关闭释放资源
      if (table != null) table.close();
      if (connection != null) connection.close();
    }
    return 0;
  }

  public static void main(String[] argv) throws Exception {
    int ret = ToolRunner.run(new NewClientAPIExample(), argv);
    System.exit(ret);
  }
}
原文地址:https://www.cnblogs.com/bigdata1024/p/8387433.html