Hbase 建表
2018-10-18 本文已影响27人
大猪大猪
private static Admin admin = null;
private static final String user = "admin/admin@DEMO.COM";
private static final String keyPath = "/etc/security/keytabs/admin.keytab";
static {
System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");
System.setProperty("sun.security.krb5.debug", "false");
Configuration conf = new Configuration();
conf.addResource("hbase-site.xml");
conf.set("hbase.zookeeper.quorum", "storm4.demo.com,storm2.demo.com,storm3.demo.com");
conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2181");
conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/hbase-secure");
conf.set("hbase.security.authentication", "kerberos");
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab(user, keyPath);
} catch (IOException e) {
e.printStackTrace();
}
Connection connection= null;
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
try {
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized static Table getTable(String tableName) {
try {
return TABLE_MAP.get(tableName);
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
public synchronized static void createTable(String tableName) throws HBaseException {
TableName table = TableName.valueOf(tableName);
TableDescriptorBuilder tableDesc = TableDescriptorBuilder.newBuilder(table);
tableDesc.setValue(TableDescriptorBuilder.SPLIT_POLICY, KeyPrefixRegionSplitPolicy.class.getName());
tableDesc.setValue(KeyPrefixRegionSplitPolicy.PREFIX_LENGTH_KEY, "2");
ColumnFamilyDescriptor extCF = ColumnFamilyDescriptorBuilder.newBuilder("ext".getBytes()).setTimeToLive("90 DAYS").build();
ColumnFamilyDescriptor deviceCF = ColumnFamilyDescriptorBuilder.newBuilder("device".getBytes()).setTimeToLive("90 DAYS").build();
ColumnFamilyDescriptor locationCF = ColumnFamilyDescriptorBuilder.newBuilder("location".getBytes()).setTimeToLive("90 DAYS").build();
tableDesc.setColumnFamilies(Arrays.asList(extCF, locationCF, deviceCF));
try {
byte[][] splitKeys = new byte[4][];
splitKeys[0] = Bytes.toBytes("00");
splitKeys[1] = Bytes.toBytes("40");
splitKeys[2] = Bytes.toBytes("80");
splitKeys[3] = Bytes.toBytes("c0");
admin.createTable(tableDesc.build(), splitKeys);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
createTable("DSP_LOG_TABLE");
} catch (HBaseException e) {
e.printStackTrace();
}
}