import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
public class DDLTest01 {
public static void main(String[] args) throws IOException {
//构建实例
DDLTest01 test01 = new DDLTest01();
//构建配置对象,用于管理当前程序的所有配置:用于加载hbase-default和hbase-site文件
Configuration conf = HBaseConfiguration.create();
//在配置中指定服务端地址:Zookeeper地址
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,node3:2181");
//构建连接对象
Connection conn = ConnectionFactory.createConnection(conf);
//构建管理员
Admin admin = conn.getAdmin();
//NS操作:列举、创建、删除
//test01.listNS(admin);
//test01.createNS(admin);
//test01.delNS(admin);
//Table操作:列举、创建、删除
//test01.listTable(admin);
test01.createTable(admin);
//释放资源
admin.close();
conn.close();
}
/**
* 创建表
* @param admin
*/
private void createTable(Admin admin) throws IOException {
//定义一个表对象
TableName tableName = TableName.valueOf("111:01");
if (admin.tableExists(tableName)){
//禁用
admin.disableTable(tableName);
//删除
admin.deleteTable(tableName);
}
//构建列族的描述器对象
ColumnFamilyDescriptor aa = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("basic")).setMaxVersions(2).build();
ColumnFamilyDescriptor bb = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("other")).setMaxVersions(1).build();
//构建一个表的描述器对象
TableDescriptor build = TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(aa).setColumnFamily(bb).build();
admin.createTable(build);
}
/**
* 列举所有表
* @param admin
*/
private void listTable(Admin admin) throws IOException {
List<TableDescriptor> list = admin.listTableDescriptors();
for (TableDescriptor descriptor : list) {
System.out.println(descriptor.getTableName().getNameAsString());
}
}
/**
* 删除NS
* @param admin
*/
private void delNS(Admin admin) throws IOException {
admin.deleteNamespace("111");
}
/**
* 创建NS
* @param admin
*/
private void createNS(Admin admin) throws IOException {
NamespaceDescriptor build = NamespaceDescriptor.create("111").build();
admin.createNamespace(build);
}
/**
* 列举当前所有的NS
* @param admin
*/
private void listNS(Admin admin) throws IOException {
NamespaceDescriptor[] descriptors = admin.listNamespaceDescriptors();
for (NamespaceDescriptor descriptor : descriptors) {
System.out.println(descriptor.getName());
}
}
}
HBase JavaAPI:DDL
猜你喜欢
转载自blog.csdn.net/tian_1_2_3/article/details/115505458
今日推荐
周排行