下面列出了org.apache.hadoop.hbase.client.HBaseAdmin#tableExists ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void checkHBaseTables(List<CubeInstance> cubes) throws IOException {
reporter.log("## Checking HBase Table of segments");
HBaseAdmin hbaseAdmin = new HBaseAdmin(HBaseConfiguration.create());
try {
for (CubeInstance cube : cubes) {
for (CubeSegment segment : cube.getSegments()) {
if (segment.getStatus() != SegmentStatusEnum.NEW) {
String tableName = segment.getStorageLocationIdentifier();
if ((!hbaseAdmin.tableExists(tableName)) || (!hbaseAdmin.isTableEnabled(tableName))) {
reporter.log("HBase table: {} not exist for segment: {}, project: {}", tableName, segment,
cube.getProject());
reporter.log(
"The rebuild url: -d '{\"startTime\":{}, \"endTime\":{}, \"buildType\":\"REFRESH\"}' /kylin/api/cubes/{}/build",
segment.getTSRange().start, segment.getTSRange().end, cube.getName());
}
}
}
}
} finally {
if (null != hbaseAdmin) {
hbaseAdmin.close();
}
}
}
public static void create() throws Exception {
HBaseAdmin admin = new HBaseAdmin(cfg);
if (admin.tableExists(tableName)) {
System.out.println("[info]table has created!");
} else {
try {
TableName table = TableName.valueOf(tableName);
HTableDescriptor tableDescriptor = new HTableDescriptor(table);
tableDescriptor.addFamily(new HColumnDescriptor(familyName));
admin.createTable(tableDescriptor);
} catch (TableExistsException e) {
System.out.println("[warning] table exists!");
}
}
System.out.println("[info]create table success!");
}
/**
* @param tableName
* @return
*/
public boolean deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conn);
if (admin.tableExists(tableName)) {
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
LOGGER.info(">>>> Table {} delete success!", tableName);
} catch (Exception ex) {
LOGGER.error("delete table error:", ex);
return false;
}
} else {
LOGGER.warn(">>>> Table {} delete but not exist.", tableName);
}
admin.close();
return true;
}
public static boolean delete(String tablename) throws IOException{
HBaseAdmin admin=new HBaseAdmin(cfg);
if(admin.tableExists(tablename)){
try
{
admin.disableTable(tablename);
admin.deleteTable(tablename);
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
return true;
}
public Boolean createTable(String tableName, String familyName) throws Exception {
HBaseAdmin admin = new HBaseAdmin(conn);
if (admin.tableExists(tableName)) {
LOGGER.warn(">>>> Table {} exists!", tableName);
admin.close();
return false;
}
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
tableDesc.addFamily(new HColumnDescriptor(familyName));
admin.createTable(tableDesc);
LOGGER.info(">>>> Table {} create success!", tableName);
admin.close();
return true;
}
private void checkHBaseTables(List<CubeInstance> cubes) throws IOException {
reporter.log("## Checking HBase Table of segments");
HBaseAdmin hbaseAdmin = new HBaseAdmin(HBaseConfiguration.create());
try {
for (CubeInstance cube : cubes) {
for (CubeSegment segment : cube.getSegments()) {
if (segment.getStatus() != SegmentStatusEnum.NEW) {
String tableName = segment.getStorageLocationIdentifier();
if ((!hbaseAdmin.tableExists(tableName)) || (!hbaseAdmin.isTableEnabled(tableName))) {
reporter.log("HBase table: {} not exist for segment: {}, project: {}", tableName, segment,
cube.getProject());
reporter.log(
"The rebuild url: -d '{\"startTime\":{}, \"endTime\":{}, \"buildType\":\"REFRESH\"}' /kylin/api/cubes/{}/build",
segment.getTSRange().start, segment.getTSRange().end, cube.getName());
}
}
}
}
} finally {
if (null != hbaseAdmin) {
hbaseAdmin.close();
}
}
}
private HTable createStateTable() {
String stateTableName = ConfigUtil.getTailerStateTable(_conf);
HBaseAdmin admin = getHBaseAdmin();
try {
if (!admin.tableExists(stateTableName)) {
HTableDescriptor tableDesc = new HTableDescriptor(stateTableName);
HColumnDescriptor familyDesc = new HColumnDescriptor(STATE_TABLE_COL_FAMILY);
familyDesc.setMaxVersions(1);
tableDesc.addFamily(familyDesc);
admin.createTable(tableDesc);
}
return new HTable(_conf, stateTableName);
} catch (Exception e) {
throw new RuntimeException("Failed to create state table", e);
}
}
/**
* Method to disable and delete HBase Tables i.e. "int-test-01"
*/
private static void deleteTestTable(String tableName) throws IOException {
// Reset configuration
config.clear();
config.set("hbase.zookeeper.quorum", QUORUM);
config.set("hbase.zookeeper.property.clientPort", QUORUM_PORT);
HBaseAdmin hbase = new HBaseAdmin(config);
if (hbase.tableExists(tableName)) {
LOG.info("Table: " + tableName + " exists.");
hbase.disableTable(tableName);
hbase.deleteTable(tableName);
LOG.info("Table: " + tableName + " disabled and deleted.");
} else {
LOG.info("Table: " + tableName + " does not exist.");
}
hbase.close();
}
private static void check(HBaseAdmin admin, HTable htable) throws DataXException, IOException {
if (!admin.isMasterRunning()) {
throw new IllegalStateException("HBase master 没有运行, 请检查您的配置 或者 联系 Hbase 管理员.");
}
if (!admin.tableExists(htable.getTableName())) {
throw new IllegalStateException("HBase源头表" + Bytes.toString(htable.getTableName()) + "不存在, 请检查您的配置 或者 联系 Hbase 管理员.");
}
if (!admin.isTableAvailable(htable.getTableName()) || !admin.isTableEnabled(htable.getTableName())) {
throw new IllegalStateException("HBase源头表" + Bytes.toString(htable.getTableName()) + " 不可用, 请检查您的配置 或者 联系 Hbase 管理员.");
}
}
private static void check(HBaseAdmin admin, HTable htable) throws DataXException, IOException {
if (!admin.isMasterRunning()) {
throw new IllegalStateException("HBase master 没有运行, 请检查您的配置 或者 联系 Hbase 管理员.");
}
if (!admin.tableExists(htable.getTableName())) {
throw new IllegalStateException("HBase源头表" + Bytes.toString(htable.getTableName())
+ "不存在, 请检查您的配置 或者 联系 Hbase 管理员.");
}
if (!admin.isTableAvailable(htable.getTableName()) || !admin.isTableEnabled(htable.getTableName())) {
throw new IllegalStateException("HBase源头表" + Bytes.toString(htable.getTableName())
+ " 不可用, 请检查您的配置 或者 联系 Hbase 管理员.");
}
}
private static boolean createTable(byte[] tableName, byte[] columnFamilyName,
short regionCount, long regionMaxSize, HBaseAdmin admin)
throws IOException {
if (admin.tableExists(tableName)) {
return false;
}
HTableDescriptor tableDescriptor = new HTableDescriptor();
tableDescriptor.setName(tableName);
HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamilyName);
columnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY);
columnDescriptor.setBlocksize(64 * 1024);
columnDescriptor.setBloomFilterType(BloomType.ROW);
columnDescriptor.setMaxVersions(10);
tableDescriptor.addFamily(columnDescriptor);
tableDescriptor.setMaxFileSize(regionMaxSize);
tableDescriptor.setValue(tableDescriptor.SPLIT_POLICY,
ConstantSizeRegionSplitPolicy.class.getName());
tableDescriptor.setDeferredLogFlush(true);
regionCount = (short) Math.abs(regionCount);
int regionRange = Short.MAX_VALUE / regionCount;
int counter = 0;
byte[][] splitKeys = new byte[regionCount][];
for (byte[] splitKey : splitKeys) {
counter = counter + regionRange;
String key = StringUtils.leftPad(Integer.toString(counter), 5, '0');
splitKey = Bytes.toBytes(key);
System.out.println(" - Split: " + splitKey);
}
return true;
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
@Override
public boolean deleteResource(JobConf jobConf) throws IOException {
HBaseAdmin hBaseAdmin = getHBaseAdmin(jobConf);
if (hBaseAdmin.tableExists(tableName)) {
return true;
} else {
throw new IOException("DELETE records: " + tableName + " does NOT EXIST!!!");
}
}
protected static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = admin.tableExists(tableName);
if (tableExists) {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
@Test
public void testCreateTable () throws Exception {
String ddl = "CREATE TABLE flume_test " +
" (flume_time timestamp not null, col1 varchar , col2 varchar" +
" CONSTRAINT pk PRIMARY KEY (flume_time))\n";
final String fullTableName = "FLUME_TEST";
sinkContext = new Context ();
sinkContext.put(FlumeConstants.CONFIG_TABLE, fullTableName);
sinkContext.put(FlumeConstants.CONFIG_JDBC_URL, getUrl());
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER,EventSerializers.REGEX.name());
sinkContext.put(FlumeConstants.CONFIG_TABLE_DDL, ddl);
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_REGULAR_EXPRESSION,"^([^\t]+)\t([^\t]+)$");
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_COLUMN_NAMES,"col1,col2");
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_ROWKEY_TYPE_GENERATOR,DefaultKeyGenerator.TIMESTAMP.name());
sink = new PhoenixSink();
Configurables.configure(sink, sinkContext);
Assert.assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
final Channel channel = this.initChannel();
sink.setChannel(channel);
sink.start();
HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
try {
boolean exists = admin.tableExists(fullTableName);
Assert.assertTrue(exists);
}finally {
admin.close();
}
}
@Override
public int run(String[] args) throws Exception {
Options options = new Options();
try {
options.addOption(OPTION_II_NAME);
options.addOption(OPTION_HTABLE_NAME);
parseOptions(options, args);
String tableName = getOptionValue(OPTION_HTABLE_NAME);
String iiName = getOptionValue(OPTION_II_NAME);
KylinConfig config = KylinConfig.getInstanceFromEnv();
IIManager iiManager = IIManager.getInstance(config);
IIInstance ii = iiManager.getII(iiName);
int sharding = ii.getDescriptor().getSharding();
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
HColumnDescriptor cf = new HColumnDescriptor(IIDesc.HBASE_FAMILY);
cf.setMaxVersions(1);
//cf.setCompressionType(Algorithm.LZO);
cf.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
tableDesc.addFamily(cf);
tableDesc.setValue(IRealizationConstants.HTableTag, config.getMetadataUrlPrefix());
Configuration conf = HBaseConfiguration.create(getConf());
if (User.isHBaseSecurityEnabled(conf)) {
// add coprocessor for bulk load
tableDesc.addCoprocessor("org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint");
}
DeployCoprocessorCLI.deployCoprocessor(tableDesc);
// drop the table first
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
// create table
byte[][] splitKeys = getSplits(sharding);
if (splitKeys.length == 0)
splitKeys = null;
admin.createTable(tableDesc, splitKeys);
if (splitKeys != null) {
for (int i = 0; i < splitKeys.length; i++) {
System.out.println("split key " + i + ": " + BytesUtil.toHex(splitKeys[i]));
}
}
System.out.println("create hbase table " + tableName + " done.");
admin.close();
return 0;
} catch (Exception e) {
printUsage(options);
throw e;
}
}
@Override
public boolean createResource(JobConf jobConf) throws IOException {
HBaseAdmin hBaseAdmin = getHBaseAdmin(jobConf);
if (hBaseAdmin.tableExists(tableName)) {
return true;
}
LOG.info("creating hbase table: {}", tableName);
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
String[] familyNames = ((HBaseRawScheme) getScheme()).getFamilyNames();
for (String familyName : familyNames) {
tableDescriptor.addFamily(new HColumnDescriptor(familyName));
}
hBaseAdmin.createTable(tableDescriptor);
return true;
}
@Override
public boolean createResource(JobConf jobConf) throws IOException {
HBaseAdmin hBaseAdmin = getHBaseAdmin(jobConf);
if (hBaseAdmin.tableExists(tableName)) {
return true;
}
LOG.info("Creating HBase Table: {}", tableName);
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
String[] familyNames = ((HBaseScheme) getScheme()).getFamilyNames();
for (String familyName : familyNames) {
tableDescriptor.addFamily(new HColumnDescriptor(familyName));
}
hBaseAdmin.createTable(tableDescriptor);
return true;
}