package com.hbase;
import org.apache.hadoop.conf.Configuration;
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.client.HBaseAdmin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author qt
* @description c
* @date 2018/8/10 11:06
*/
public class HBaseTableTest {
private static final String ZKADDRESS = "";
private static Connection connection = null;
private static Admin hBaseAdmin = null;
static {
Configuration config = HBaseConfiguration.create();
config.set(HConstants.ZOOKEEPER_QUORUM, ZKADDRESS);
try {
connection = ConnectionFactory.createConnection(config);
hBaseAdmin = connection.getAdmin();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建表, HColumnDescriptor包含有关列族的信息,例如版本数,压缩设置等。它在创建表或添加列时用作输入。
* @param tableName
* @param familyName
* @throws Exception
*/
public void create(String tableName, String familyName) throws Exception {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor(familyName));
System.out.print("Creating table. ");
hBaseAdmin.createTable(tableDescriptor);
}
/**
* 列出表
* @return
* @throws IOException
*/
public static List<String> list() throws IOException {
HTableDescriptor[] tableDescriptor = hBaseAdmin.listTables();
List<String> list = new ArrayList<String>();
for(HTableDescriptor x : tableDescriptor){
System.out.println(x.getNameAsString());
list.add(x.getNameAsString());
}
return list;
}
/**
* 禁用表
* @param tableName
* @throws IOException
*/
public static void disableTable(String tableName) throws IOException {
Boolean bool = hBaseAdmin.isTableDisabled(TableName.valueOf(tableName));
if(!bool){
hBaseAdmin.disableTable(TableName.valueOf(tableName));
System.out.println(tableName+"Table disabled");
}
}
/**
* 启用表
* @param tableName
* @throws Exception
*/
public static void enableTable(String tableName) throws Exception {
Boolean bool = hBaseAdmin.isTableEnabled(TableName.valueOf(tableName));
if(!bool){
hBaseAdmin.enableTable(TableName.valueOf(tableName));
System.out.println(tableName+"Table Enabled");
}
}
/**
* 添加列族
* @param tableName
* @param familyNames
* @throws IOException
*/
public static void addFamily(String tableName, String familyNames) throws IOException {
HColumnDescriptor columnDescriptor = new HColumnDescriptor(familyNames);
hBaseAdmin.addColumn(TableName.valueOf(tableName), columnDescriptor);
}
/**
* 删除列族
* @param tableName
* @param familyNames
* @throws IOException
*/
public static void deleteFamily(String tableName, String familyNames) throws IOException {
hBaseAdmin.deleteColumn(TableName.valueOf(tableName), familyNames.getBytes());
}
/**
* 表是否存在
* @param tableName
* @return
* @throws IOException
*/
public static boolean existsTab(String tableName) throws IOException {
return hBaseAdmin.tableExists(TableName.valueOf(tableName));
}
/**
* 删除表
* @param tableName
* @throws IOException
*/
public static boolean deleteTable(String tableName) throws IOException {
if(!existsTab(tableName)){
return false;
}
disableTable(tableName);
hBaseAdmin.deleteTable(TableName.valueOf(tableName));
return true;
}
/**
* 断开连接
* @throws IOException
*/
public static void close() throws IOException {
if(hBaseAdmin != null) {
hBaseAdmin.close();
}
if(connection != null) {
connection.close();
}
}
public static void main(String[] args) throws Exception {
HBaseTableTest.enableTable("test");
HBaseTableTest.close();
}
}