Apache HBase API

Apache HBase API

HBase가 제공하는 프로그래밍 인터페이스의 빠른 참조를 담은 페이지예요. 완전한 목록은 아니며 User API Reference에 더해 빠른 참조용으로 제공돼요. 여기 예시들은 포괄적이거나 완전하지 않으므로 단지 설명 목적으로만 활용하세요.

출처: 문서

본문

Apache HBase는 여러 외부 API와도 함께 동작해요. 자세한 내용은 Apache HBase External APIs를 참고하세요.

예시 (Examples)

Java로 테이블 생성·수정·삭제하기 (Create, modify and delete a Table Using Java)

package com.example.hbase.admin;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HConstants; 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.io.compress.Compression.Algorithm;

public class Example {

private static final String TABLE_NAME = "MY_TABLE_NAME_TOO"; private static final String CF_DEFAULT = "DEFAULT_COLUMN_FAMILY";

public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException { if (admin.tableExists(table.getTableName())) { admin.disableTable(table.getTableName()); admin.deleteTable(table.getTableName()); } admin.createTable(table); }

public static void createSchemaTables(Configuration config) throws IOException { try (Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin()) {

  HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
  table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.NONE));

  System.out.print("Creating table. ");
  createOrOverwrite(admin, table);
  System.out.println(" Done.");
}

}

public static void modifySchema (Configuration config) throws IOException { try (Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin()) {

  TableName tableName = TableName.valueOf(TABLE_NAME);
  if (!admin.tableExists(tableName)) {
    System.out.println("Table does not exist.");
    System.exit(-1);
  }

  HTableDescriptor table = admin.getTableDescriptor(tableName);

  // Update existing table
  HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
  newColumn.setCompactionCompressionType(Algorithm.GZ);
  newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
  admin.addColumn(tableName, newColumn);

  // Update existing column family
  HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT);
  existingColumn.setCompactionCompressionType(Algorithm.GZ);
  existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
  table.modifyFamily(existingColumn);
  admin.modifyTable(tableName, table);

  // Disable an existing table
  admin.disableTable(tableName);

  // Delete an existing column family
  admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8"));

  // Delete a table (Need to be disabled first)
  admin.deleteTable(tableName);
}

}

public static void main(String... args) throws IOException { Configuration config = HBaseConfiguration.create();

//Add any necessary configuration files (hbase-site.xml, core-site.xml)
config.addResource(new Path(System.getenv("HBASE_CONF_DIR"), "hbase-site.xml"));
config.addResource(new Path(System.getenv("HADOOP_CONF_DIR"), "core-site.xml"));
createSchemaTables(config);
modifySchema(config);

} }

더 알아보기 (Learn more)