【HBase】五、HBase的Java接口

  HBase是Hadoop中的一个重要组件,自然也是基于Java语言开发的,因此HBase有很好的Java接口供程序员调用,通过一个例子来演示java如何使用HBase数据库。

  要想在HBase中创建一个表,首先要创建一个Admin的实例,然后用它来创建名为test并且只有一个列族data的表,然后确认创建成功后,需要对这个表进行操作,这时需要新建一个Table的实例,其参数为表名。接下来为了插入数据需要循环创建put对象,通过put.add方法指明列族、列修饰符、对应的值,然后使用table的put方法将数据插入数据库。

  同样的,要从数据库中读取数据需要创建一个Get类的对象,我们说过,HBase的读取必须是依赖于行键的,所以Get的参数就是要指明行键,然后调用table.get方法得到对应的数据。

  如果想要进行全表扫描,需要使用Scan对象。另外,在删除表之前必须首先设置为禁用。

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class ExampleClient {

  public static void main(String[] args) throws IOException {
    Configuration config = HBaseConfiguration.create();
    Connection connection = ConnectionFactory.createConnection(config);
    try {
      // Create table
      Admin admin = connection.getAdmin();
      try {
        TableName tableName = TableName.valueOf("test");
        HTableDescriptor htd = new HTableDescriptor(tableName);
        HColumnDescriptor hcd = new HColumnDescriptor("data");
        htd.addFamily(hcd);
        admin.createTable(htd);
        HTableDescriptor[] tables = admin.listTables();
        if (tables.length != 1 &&
            Bytes.equals(tableName.getName(), tables[0].getTableName().getName())) {
          throw new IOException("Failed create of table");
        }
        // Run some operations -- three puts, a get, and a scan -- against the table.
        Table table = connection.getTable(tableName);
        try {
          for (int i = 1; i <= 3; i++) {
            byte[] row = Bytes.toBytes("row" + i);
            Put put = new Put(row);
            byte[] columnFamily = Bytes.toBytes("data");
            byte[] qualifier = Bytes.toBytes(String.valueOf(i));
            byte[] value = Bytes.toBytes("value" + i);
            put.add(columnFamily, qualifier, value);
            table.put(put);
          }
          Get get = new Get(Bytes.toBytes("row1"));
          Result result = table.get(get);
          System.out.println("Get: " + result);
          Scan scan = new Scan();
          ResultScanner scanner = table.getScanner(scan);
          try {
            for (Result scannerResult: scanner) {
              System.out.println("Scan: " + scannerResult);
            }
          } finally {
            scanner.close();
          }
          // Disable then drop the table
          admin.disableTable(tableName);
          admin.deleteTable(tableName);
        } finally {
          table.close();
        }
      } finally {
        admin.close();
      }
    } finally {
      connection.close();
    }
  }
}

  除了Java接口之外,HBase作为Hadoop的“三驾马车”之一,与MapReduce也有很好的衔接,HBase表可以作为MapReduce 作业的源/输出,在输入输出格式上,MapReduce提供了 TableInputFormat ,使得作为输入时,数据会在区域的边界进行分割,map可以拿到一个完整的区域进行处理,而 TableOutputFormat 使得reduce的输出可以写入HBase数据库。

posted @ 2019-06-30 15:37  gzshan  阅读(1166)  评论(0编辑  收藏  举报