Hbase-DDL原生api操作
- DDL操作
- 1.判断表是否存在
- 2.创建表
- 3.创建命名空间
- 4.删除表
创建项目引入依赖,文章使用 hbase 1.3.1版本
<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.3.1</version>
</dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>1.3.1</version>
</dependency>
创建 log4j.properties 日志文件
# Configure logging for testing: optionally with log file#log4j.rootLogger&#61;debug,appender
log4j.rootLogger&#61;info,appender
#log4j.rootLogger&#61;error,appender#\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.appender&#61;org.apache.log4j.ConsoleAppender
#\u6837\u5F0F\u4E3ATTCCLayout
log4j.appender.appender.layout&#61;org.apache.log4j.TTCCLayout
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class DdlApi {private static Connection connection &#61; null;private static Admin admin &#61; null;static {try {Configuration configuration &#61; HBaseConfiguration.create();configuration.set("hbase.zookeeper.quorum", "192.168.100.136,192.168.100.137,192.168.100.138");configuration.set("hbase.zookeeper.property.clientPort", "2181");connection &#61; ConnectionFactory.createConnection(configuration);admin &#61; connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {createTable("0408:stu","info1","info2");close();} catch (Exception e) {e.printStackTrace();}}public static Boolean isTableExist(String tableName) throws IOException {
boolean b &#61; admin.tableExists(TableName.valueOf(tableName));close();return b;}public static void createTable(String tableName,String... cfs) throws IOException {if(cfs.length <&#61; 0){System.out.println("设置列族信息&#xff01;");return;}if(isTableExist(tableName)){System.out.println(tableName&#43;"表已存在");return;}HTableDescriptor hTableDescriptor &#61; new HTableDescriptor(TableName.valueOf(tableName));for(String cf : cfs){HColumnDescriptor hColumnDescriptor &#61; new HColumnDescriptor(cf);hTableDescriptor.addFamily(hColumnDescriptor);}admin.createTable(hTableDescriptor);}public static void createNameSpace(String ns){NamespaceDescriptor namespaceDescriptor &#61; NamespaceDescriptor.create(ns).build();try {admin.createNamespace(namespaceDescriptor);}catch (NamespaceExistException e){System.out.println("命名空间已存在");}catch (IOException e) {e.printStackTrace();}}public static void dropTable(String tabale){try {if(!isTableExist(tabale)){System.out.println(tabale&#43;"表不已存在");return;}admin.disableTable(TableName.valueOf(tabale));admin.deleteTable(TableName.valueOf(tabale));close();} catch (IOException e) {e.printStackTrace();}}public static void close(){if (admin !&#61; null){try {admin.close();} catch (IOException e) {e.printStackTrace();}}if(connection !&#61; null){try {connection.close();} catch (IOException e) {e.printStackTrace();}}}
}