创建表
package com.zykj.hbase.back.utils;
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.Connection;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Table;
import org.junit.Test;
import java.io.IOException;
/**
* description
*
* @author ${user}
* @Time 2019-04-10
*/
public class HbaseUtil {
static Configuration config = null;
private Connection connection = null;
private Table table = null;
private String zk_ip = null;
private String zi_port = null;
{
PropertiesUtils propertiesUtils = new PropertiesUtils();
zk_ip = propertiesUtils.getProperty("zookeeper_ip");
zi_port = propertiesUtils.getProperty("zookeeper_port");
config = HBaseConfiguration.create();// 配置
config.set("hbase.zookeeper.quorum", zk_ip); // zookeeper地址
config.set("hbase.zookeeper.property.clientPort", zi_port);// zookeeper端口
}
public boolean createTable() {
HBaseAdmin admin = null; // hbase表管理
try {
admin = new HBaseAdmin(config);
} catch (IOException e) {
e.printStackTrace();
}
// 创建表描述类
TableName tableName = TableName.valueOf("dept"); // 表名称
HTableDescriptor desc = new HTableDescriptor(tableName);
// 创建列族的描述类
HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
// 将列族添加到表中
desc.addFamily(family);
HColumnDescriptor family2 = new HColumnDescriptor("subdept"); // 列族
// 将列族添加到表中
desc.addFamily(family2);
// System.out.println("judgement");
// 创建表
try {
admin.createTable(desc); // 创建表
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("创建表成功!");
return true;
}
@Test
public void TestCreateT() {
HbaseUtil hbaseUtil = new HbaseUtil();
hbaseUtil.createTable();
}
}