HBase修改表结构

package com.hbase.HBaseAdmin;

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.util.Bytes;

/**
* @author:FengZhen
* @create:2018年9月12日
* 修改表结构
*/
public class ModifyTable {

private static String addr="HDP233,HDP232,HDP231";
private static String port="2181";
private static Connection connection;

/**
* 获取连接
*/
public static void getConnection(){
Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum",addr);
conf.set("hbase.zookeeper.property.clientPort", port);
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}

/*
* 关闭连接
*/
public static void close() {
/**
* close connection
**/
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* Equals: true
New schema: 'test_modify', {TABLE_ATTRIBUTES => {MAX_FILESIZE => '1073741824'}, {NAME => 'colfam1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}, {NAME => 'colfam2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
*/
/**
* modifyTable方法只提供了异步的操作模式,没有提供同步的操作模式。
* 如果用户需要确认是否已经成功,需要在客户端代码中显式循环地调用getTableDescriptor方法获取元数据
* 直到结果与本地实例匹配。
* @param args
*/
public static void main(String[] args) {
getConnection();
try {
String tableName = "test_modify";
Admin admin = connection.getAdmin();
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
HColumnDescriptor columnDescriptor = new HColumnDescriptor(Bytes.toBytes("colfam1"));
tableDescriptor.addFamily(columnDescriptor);
//使用旧结构建表
admin.createTable(tableDescriptor);

HTableDescriptor tableDescriptor2 = admin.getTableDescriptor(TableName.valueOf(tableName));
HColumnDescriptor columnDescriptor2 = new HColumnDescriptor(Bytes.toBytes("colfam2"));
tableDescriptor2.addFamily(columnDescriptor2);
tableDescriptor2.setMaxFileSize(1024 * 1024 * 1024L);

admin.disableTable(TableName.valueOf(tableName));
admin.modifyTable(TableName.valueOf(tableName), tableDescriptor2);
admin.enableTable(TableName.valueOf(tableName));

HTableDescriptor tableDescriptor3 = admin.getTableDescriptor(TableName.valueOf(tableName));
System.out.println("Equals: " + tableDescriptor2.equals(tableDescriptor3));
System.out.println("New schema: " + tableDescriptor3);
} catch (IOException e) {
e.printStackTrace();
}
close();
}

}