1.配置文件和连接信息
// 两个变量的声明:
private static Connection connection = null;
private static Admin admin = null;
static{
try {
// 1.获取配置文件信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");
// 2.创建连接对象
connection = ConnectionFactory.createConnection(configuration);
// 3.创建admin对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
2.判断表是否存在
public static boolean isTableExist(String tableName) throws IOException {
// 5.判断表是否存在
boolean exists = admin.tableExists(TableName.valueOf(tableName));
// 6.返回结果
return exists;
}
3.创建表
public static void creaTable(String tableName,String... cfs) throws IOException {
// 10.1 是否存在列族信息
if(cfs.length <= 0){
System.out.println("请设置列族信息!");
return;
}
// 10.2 判断表是否存在
if(isTableExist(tableName)){
System.out.println(tableName + "表已存在!");
return;
}
// 10.3 创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 10.4 循环添加列族信息
for (String cf : cfs) {
// 10.5 创建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
// 10.6 添加具体的列族信息
hTableDescriptor.addFamily(hColumnDescriptor);
}
// 10.7 创建表
admin.createTable(hTableDescriptor);
}
4.单独创建关闭资源
public static void close(){
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.测试和关闭
public static void main(String[] args) throws IOException {
// 8.测试表是否存在
System.out.println(isTableExist("stu1"));
// 11.创建表测试
creaTable("stu1","info1","info2");
// 12.创建完测试表是否存在
System.out.println(isTableExist("stu1"));
// 9.关闭资源的调用
close();
}
整体详情:
package test;
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.client.HTable;
import java.io.IOException;
public class CreateTable {
// 两个变量的声明:
private static Connection connection = null;
private static Admin admin = null;
static{
try {
// 1.获取配置文件信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");
// 2.创建连接对象
connection = ConnectionFactory.createConnection(configuration);
// 3.创建admin对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 4.判断表是否存在
public static boolean isTableExist(String tableName) throws IOException {
// 5.判断表是否存在
boolean exists = admin.tableExists(TableName.valueOf(tableName));
// 6.返回结果
return exists;
}
// 10.创建表
public static void creaTable(String tableName,String... cfs) throws IOException {
// 10.1 是否存在列族信息
if(cfs.length <= 0){
System.out.println("请设置列族信息!");
return;
}
// 10.2 判断表是否存在
if(isTableExist(tableName)){
System.out.println(tableName + "表已存在!");
return;
}
// 10.3 创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 10.4 循环添加列族信息
for (String cf : cfs) {
// 10.5 创建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
// 10.6 添加具体的列族信息
hTableDescriptor.addFamily(hColumnDescriptor);
}
// 10.7 创建表
admin.createTable(hTableDescriptor);
}
// 7.关闭资源
public static void close(){
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
// 8.测试表是否存在
System.out.println(isTableExist("stu1"));
// 11.创建表测试
creaTable("stu1","info1","info2");
// 12.创建完测试表是否存在
System.out.println(isTableExist("stu1"));
// 9.关闭资源的调用
close();
}
}