public void deleteTable(String tableName) { try (HBaseAdmin admin = (HBaseAdmin) getConnection().getAdmin()) { if (admin.isTableEnabled(tableName)) { disableTable(tableName); } admin.deleteTable(tableName); } catch (IOException e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } }
public Object call() throws Exception { logger.info("Deleting HBase table " + htableName); if (hbaseAdmin.tableExists(htableName)) { if (hbaseAdmin.isTableEnabled(htableName)) { hbaseAdmin.disableTable(htableName); } hbaseAdmin.deleteTable(htableName); logger.info("Deleted HBase table " + htableName); } else { logger.info("HBase table" + htableName + " does not exist"); } return null; } }
@Override public HTableDescriptor[] disableTables(Pattern pattern) throws IOException { List<HTableDescriptor> failed = new LinkedList<>(); for (HTableDescriptor table : listTables(pattern)) { if (isTableEnabled(table.getTableName())) { try { disableTable(table.getTableName()); } catch (IOException ex) { LOG.info("Failed to disable table " + table.getTableName(), ex); failed.add(table); } } } return failed.toArray(new HTableDescriptor[failed.size()]); }
@Test public void test() throws IOException { HBaseAdmin hBaseAdmin = mock(HBaseAdmin.class); HTableDescriptor[] hds = new HTableDescriptor[2]; HTableDescriptor d1 = mock(HTableDescriptor.class); HTableDescriptor d2 = mock(HTableDescriptor.class); hds[0] = d1; hds[1] = d2; when(d1.getValue("KYLIN_HOST")).thenReturn("../examples/test_metadata/"); when(d2.getValue("KYLIN_HOST")).thenReturn("../examples/test_metadata/"); when(d1.getTableName()).thenReturn(TableName.valueOf("KYLIN_J9TE08D9IA")); String toBeDel = "to-be-del"; when(d2.getTableName()).thenReturn(TableName.valueOf(toBeDel)); when(hBaseAdmin.listTables("KYLIN_.*")).thenReturn(hds); when(hBaseAdmin.tableExists(toBeDel)).thenReturn(true); when(hBaseAdmin.isTableEnabled(toBeDel)).thenReturn(false); StorageCleanJobHbaseUtil.cleanUnusedHBaseTables(hBaseAdmin, true, 100000); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(hBaseAdmin).deleteTable(captor.capture()); assertEquals(Lists.newArrayList(toBeDel), captor.getAllValues()); } }
throws IOException { checkTableExists(tableName); if (!isTableEnabled(tableName)) {
@Override public HTableDescriptor[] disableTables(Pattern pattern) throws IOException { List<HTableDescriptor> failed = new LinkedList<>(); for (HTableDescriptor table : listTables(pattern)) { if (isTableEnabled(table.getTableName())) { try { disableTable(table.getTableName()); } catch (IOException ex) { LOG.info("Failed to disable table " + table.getTableName(), ex); failed.add(table); } } } return failed.toArray(new HTableDescriptor[failed.size()]); }
if (!admin.isTableEnabled(tableName)) if (admin.isTableEnabled(tableName))
if (admin.isTableAvailable(databaseName)) if (admin.isTableEnabled(databaseName))
private void addColumnFamilies(List<TableInfo> tableInfos, HTableDescriptor hTableDescriptor, boolean isUpdate) throws IOException { if (admin.isTableEnabled(databaseName)) { admin.disableTable(databaseName); } for (TableInfo tableInfo : tableInfos) { HColumnDescriptor columnDescriptor = hTableDescriptor.getFamily(tableInfo.getTableName().getBytes()); if (columnDescriptor != null && !isUpdate) { admin.deleteColumn(databaseName, tableInfo.getTableName()); addColumn(tableInfo); } else if (columnDescriptor == null) { addColumn(tableInfo); } } if (admin.isTableDisabled(databaseName)) { admin.enableTable(databaseName); } }
/** * Adds the column family. * * @param tableName * the table name * @param columnFamily * the column family */ public void addColumnFamily(String tableName, String columnFamily) { try { utility.getHBaseAdmin().disableTable(tableName); utility.getHBaseAdmin().addColumn(tableName, new HColumnDescriptor(columnFamily)); utility.getHBaseAdmin().enableTable(tableName); while (utility.getHBaseAdmin().isTableEnabled(tableName)) { return; } } catch (InvalidFamilyOperationException ife) { logger.info("Column family:" + columnFamily + " already exist!", ife); } catch (IOException e) { logger.error("", e); } }
/** * @param tableName name of table to check * @return true if table is on-line * @throws IOException if a remote or network exception occurs */ public boolean isTableEnabled(String tableName) throws IOException { return isTableEnabled(Bytes.toBytes(tableName)); } /**
public boolean isTableEnabled(ByteBuffer table) throws AIOError { try { return admin.isTableEnabled(Bytes.toBytes(table)); } catch (IOException e) { AIOError ioe = new AIOError(); ioe.message = new Utf8(e.getMessage()); throw ioe; } }
public static void deleteTable(HBaseAdmin admin, byte[] tableName) throws IOException { if (admin.tableExists(tableName)) { System.out.printf("Deleting %s\n", Bytes.toString(tableName)); if (admin.isTableEnabled(tableName)) { admin.disableTable(tableName); } admin.deleteTable(tableName); } }
public static void deleteTable(HBaseAdmin admin, byte[] tableName) throws IOException { if (admin.tableExists(tableName)) { System.out.printf("Deleting %s\n", Bytes.toString(tableName)); if (admin.isTableEnabled(tableName)) { admin.disableTable(tableName); } admin.deleteTable(tableName); } }
public void waitTableEnabled(byte[] table, long timeoutMillis) throws InterruptedException, IOException { long startWait = System.currentTimeMillis(); while (!getHBaseAdmin().isTableAvailable(table) && !getHBaseAdmin().isTableEnabled(table)) { assertTrue("Timed out waiting for table to become available and enabled " + Bytes.toStringBinary(table), System.currentTimeMillis() - startWait < timeoutMillis); Thread.sleep(200); } }
public static boolean dropTable(HBaseAdmin admin, String tableName) throws IOException { if (isTableCreated(admin, tableName)) { if (admin.isTableEnabled(tableName)) { admin.disableTable(tableName); } admin.deleteTable(tableName); } return true; }
protected void waitForEnabled(String tableName) throws Exception { for (int i = 0; i < MAX_WAIT_ITERATION; i++) { if (admin.isTableEnabled(tableName)) { return; } Thread.sleep(WAIT_INTERVAL); } Assert.fail(Util.getMethodName() + " failed"); }
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."); } }
protected void deleteTable(HBaseAdmin admin, byte[] tableName) throws IOException { if (admin.tableExists(tableName)) { if (admin.isTableEnabled(tableName)) { admin.disableTable(tableName); } admin.deleteTable(tableName); } }
@Test public void testCheckAndInit() throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); assertTrue(admin.tableExists(metaTable)); assertTrue(admin.isTableEnabled(metaTable)); admin.close(); }