下面列出了org.apache.hadoop.hbase.client.Admin#isTableEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void deleteTable(Connection conn, String tableName) throws IOException {
Admin hbase = conn.getAdmin();
try {
if (!tableExists(conn, tableName)) {
logger.debug("HTable '" + tableName + "' does not exists");
return;
}
logger.debug("delete HTable '" + tableName + "'");
if (hbase.isTableEnabled(TableName.valueOf(tableName))) {
hbase.disableTable(TableName.valueOf(tableName));
}
hbase.deleteTable(TableName.valueOf(tableName));
logger.debug("HTable '" + tableName + "' deleted");
} finally {
hbase.close();
}
}
public static void deleteTable(Connection conn, String tableName) throws IOException {
Admin hbase = conn.getAdmin();
try {
if (!tableExists(conn, tableName)) {
logger.debug("HTable '" + tableName + "' does not exists");
return;
}
logger.debug("delete HTable '" + tableName + "'");
if (hbase.isTableEnabled(TableName.valueOf(tableName))) {
hbase.disableTable(TableName.valueOf(tableName));
}
hbase.deleteTable(TableName.valueOf(tableName));
logger.debug("HTable '" + tableName + "' deleted");
} finally {
hbase.close();
}
}
@Test
public void testNoEnableAfterDisablePolicy() throws Exception {
Put p = new Put(Bytes.toBytes("to_reject"));
p.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
Bytes.toBytes("reject"));
final TableName tn = helper.writeUntilViolation(SpaceViolationPolicy.DISABLE);
final Admin admin = TEST_UTIL.getAdmin();
// Disabling a table relies on some external action (over the other policies), so wait a bit
// more than the other tests.
for (int i = 0; i < NUM_RETRIES * 2; i++) {
if (admin.isTableEnabled(tn)) {
LOG.info(tn + " is still enabled, expecting it to be disabled. Will wait and re-check.");
Thread.sleep(2000);
}
}
assertFalse(tn + " is still enabled but it should be disabled", admin.isTableEnabled(tn));
try {
admin.enableTable(tn);
} catch (AccessDeniedException e) {
String exceptionContents = StringUtils.stringifyException(e);
final String expectedText = "violated space quota";
assertTrue(
"Expected the exception to contain " + expectedText + ", but was: " + exceptionContents,
exceptionContents.contains(expectedText));
}
}
private void disableTable(Admin admin, String dataTable, HashSet<String>indexes)
throws IOException {
if (admin.isTableEnabled(TableName.valueOf(dataTable))) {
if (!dryRun) {
admin.disableTable(TableName.valueOf(dataTable));
}
LOGGER.info("Disabled data table " + dataTable);
} else {
LOGGER.info( "Data table " + dataTable + " is already disabled");
}
for (String indexName : indexes) {
if (admin.isTableEnabled(TableName.valueOf(indexName))) {
if (!dryRun) {
admin.disableTable(TableName.valueOf(indexName));
}
LOGGER.info("Disabled index table " + indexName);
} else {
LOGGER.info( "Index table " + indexName + " is already disabled");
}
}
}
private void enableTable(Admin admin, String dataTable, Set<String>indexes)
throws IOException {
if (!admin.isTableEnabled(TableName.valueOf(dataTable))) {
if (!dryRun) {
admin.enableTable(TableName.valueOf(dataTable));
}
LOGGER.info("Enabled data table " + dataTable);
} else {
LOGGER.info( "Data table " + dataTable + " is already enabled");
}
for (String indexName : indexes) {
if(!admin.isTableEnabled(TableName.valueOf(indexName))) {
if (!dryRun) {
admin.enableTable(TableName.valueOf(indexName));
}
LOGGER.info("Enabled index table " + indexName);
} else {
LOGGER.info( "Index table " + indexName + " is already enabled");
}
}
}
private void dropTestHTables() throws IOException {
Configuration conf = HBaseConnection.getCurrentHBaseConfiguration();
Admin hbaseAdmin = new HBaseAdmin(conf);
if (hbaseAdmin.tableExists(aclTable)) {
if (hbaseAdmin.isTableEnabled(aclTable))
hbaseAdmin.disableTable(aclTable);
hbaseAdmin.deleteTable(aclTable);
}
if (hbaseAdmin.tableExists(userTable)) {
if (hbaseAdmin.isTableEnabled(userTable))
hbaseAdmin.disableTable(userTable);
hbaseAdmin.deleteTable(userTable);
}
hbaseAdmin.close();
}
private void dropTestHTables() throws IOException {
Configuration conf = HBaseConnection.getCurrentHBaseConfiguration();
Admin hbaseAdmin = new HBaseAdmin(conf);
if (hbaseAdmin.tableExists(aclTable)) {
if (hbaseAdmin.isTableEnabled(aclTable))
hbaseAdmin.disableTable(aclTable);
hbaseAdmin.deleteTable(aclTable);
}
if (hbaseAdmin.tableExists(userTable)) {
if (hbaseAdmin.isTableEnabled(userTable))
hbaseAdmin.disableTable(userTable);
hbaseAdmin.deleteTable(userTable);
}
hbaseAdmin.close();
}
/**
* Canary entry point for specified table.
* @throws Exception exception
*/
private static List<Future<Void>> sniff(final Admin admin, final Sink sink, String tableName,
ExecutorService executor, TaskType taskType, boolean rawScanEnabled, LongAdder readLatency,
boolean readAllCF) throws Exception {
LOG.debug("Checking table is enabled and getting table descriptor for table {}", tableName);
if (admin.isTableEnabled(TableName.valueOf(tableName))) {
return CanaryTool.sniff(admin, sink, admin.getDescriptor(TableName.valueOf(tableName)),
executor, taskType, rawScanEnabled, readLatency, readAllCF);
} else {
LOG.warn("Table {} is not enabled", tableName);
}
return new LinkedList<>();
}
@Before
public void setUp() throws Exception {
Admin admin = util.getAdmin();
if (admin.tableExists(tableName)) {
if (admin.isTableEnabled(tableName)) {
admin.disableTable(tableName);
}
admin.deleteTable(tableName);
}
util.createTable(tableName, new byte[][] {dummy, test});
TestCoprocessor.PREPUT_BYPASSES.set(0);
TestCoprocessor.PREPUT_INVOCATIONS.set(0);
}
private void enableTable(final Admin admin, final TableName tableName) throws IOException {
admin.enableTableAsync(tableName);
while (!admin.isTableEnabled(tableName)) {
try {
Thread.sleep(10);
} catch (final InterruptedException e) {
// Do nothing
}
}
}
public static boolean resetCoprocessor(String tableName, Admin hbaseAdmin, Path hdfsCoprocessorJar)
throws IOException {
KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv();
HTableDescriptor desc = hbaseAdmin.getTableDescriptor(TableName.valueOf(tableName));
//when the table has migrated from dev env to test(prod) env, the dev server
//should not reset the coprocessor of the table.
String host = desc.getValue(IRealizationConstants.HTableTag);
if (!host.equalsIgnoreCase(kylinConfig.getMetadataUrlPrefix())) {
logger.warn("This server doesn't own this table: " + tableName);
return false;
}
logger.info("reset coprocessor on " + tableName);
logger.info("Disable " + tableName);
if (hbaseAdmin.isTableEnabled(TableName.valueOf(tableName))) {
hbaseAdmin.disableTable(TableName.valueOf(tableName));
}
while (desc.hasCoprocessor(CubeObserverClassOld2)) {
desc.removeCoprocessor(CubeObserverClassOld2);
}
while (desc.hasCoprocessor(CubeEndpointClass)) {
desc.removeCoprocessor(CubeEndpointClass);
}
while (desc.hasCoprocessor(IIEndpointClass)) {
desc.removeCoprocessor(IIEndpointClass);
}
// remove legacy coprocessor from v1.x
while (desc.hasCoprocessor(CubeObserverClassOld)) {
desc.removeCoprocessor(CubeObserverClassOld);
}
while (desc.hasCoprocessor(IIEndpointClassOld)) {
desc.removeCoprocessor(IIEndpointClassOld);
}
addCoprocessorOnHTable(desc, hdfsCoprocessorJar);
// update commit tags
String commitInfo = KylinVersion.getGitCommitInfo();
if (!StringUtils.isEmpty(commitInfo)) {
desc.setValue(IRealizationConstants.HTableGitTag, commitInfo);
}
hbaseAdmin.modifyTable(TableName.valueOf(tableName), desc);
logger.info("Enable " + tableName);
hbaseAdmin.enableTable(TableName.valueOf(tableName));
return true;
}
public static boolean resetCoprocessor(String tableName, Admin hbaseAdmin, Path hdfsCoprocessorJar)
throws IOException {
KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv();
HTableDescriptor desc = hbaseAdmin.getTableDescriptor(TableName.valueOf(tableName));
//when the table has migrated from dev env to test(prod) env, the dev server
//should not reset the coprocessor of the table.
String host = desc.getValue(IRealizationConstants.HTableTag);
if (!host.equalsIgnoreCase(kylinConfig.getMetadataUrlPrefix())) {
logger.warn("This server doesn't own this table: " + tableName);
return false;
}
logger.info("reset coprocessor on " + tableName);
logger.info("Disable " + tableName);
if (hbaseAdmin.isTableEnabled(TableName.valueOf(tableName))) {
hbaseAdmin.disableTable(TableName.valueOf(tableName));
}
while (desc.hasCoprocessor(CubeObserverClassOld2)) {
desc.removeCoprocessor(CubeObserverClassOld2);
}
while (desc.hasCoprocessor(CubeEndpointClass)) {
desc.removeCoprocessor(CubeEndpointClass);
}
while (desc.hasCoprocessor(IIEndpointClass)) {
desc.removeCoprocessor(IIEndpointClass);
}
// remove legacy coprocessor from v1.x
while (desc.hasCoprocessor(CubeObserverClassOld)) {
desc.removeCoprocessor(CubeObserverClassOld);
}
while (desc.hasCoprocessor(IIEndpointClassOld)) {
desc.removeCoprocessor(IIEndpointClassOld);
}
addCoprocessorOnHTable(desc, hdfsCoprocessorJar);
// update commit tags
String commitInfo = KylinVersion.getGitCommitInfo();
if (!StringUtils.isEmpty(commitInfo)) {
desc.setValue(IRealizationConstants.HTableGitTag, commitInfo);
}
hbaseAdmin.modifyTable(TableName.valueOf(tableName), desc);
logger.info("Enable " + tableName);
hbaseAdmin.enableTable(TableName.valueOf(tableName));
return true;
}
void loadingClassFromLibDirInJar(String libPrefix) throws Exception {
FileSystem fs = cluster.getFileSystem();
File innerJarFile1 = buildCoprocessorJar(cpName1);
File innerJarFile2 = buildCoprocessorJar(cpName2);
File outerJarFile = new File(TEST_UTIL.getDataTestDir().toString(), "outer.jar");
ClassLoaderTestHelper.addJarFilesToJar(
outerJarFile, libPrefix, innerJarFile1, innerJarFile2);
// copy the jars into dfs
fs.copyFromLocalFile(new Path(outerJarFile.getPath()),
new Path(fs.getUri().toString() + Path.SEPARATOR));
String jarFileOnHDFS = fs.getUri().toString() + Path.SEPARATOR +
outerJarFile.getName();
assertTrue("Copy jar file to HDFS failed.",
fs.exists(new Path(jarFileOnHDFS)));
LOG.info("Copied jar file to HDFS: " + jarFileOnHDFS);
// create a table that references the coprocessors
TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(tableName);
tdb.setColumnFamily(ColumnFamilyDescriptorBuilder
.newBuilder(Bytes.toBytes("test")).build());
// without configuration values
tdb.setValue("COPROCESSOR$1", jarFileOnHDFS + "|" + cpName1
+ "|" + Coprocessor.PRIORITY_USER);
// with configuration values
tdb.setValue("COPROCESSOR$2", jarFileOnHDFS + "|" + cpName2
+ "|" + Coprocessor.PRIORITY_USER + "|k1=v1,k2=v2,k3=v3");
Admin admin = TEST_UTIL.getAdmin();
if (admin.tableExists(tableName)) {
if (admin.isTableEnabled(tableName)) {
admin.disableTable(tableName);
}
admin.deleteTable(tableName);
}
TableDescriptor tableDescriptor = tdb.build();
admin.createTable(tableDescriptor);
waitForTable(tableDescriptor.getTableName());
// verify that the coprocessors were loaded
boolean found1 = false, found2 = false, found2_k1 = false,
found2_k2 = false, found2_k3 = false;
MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
for (HRegion region: hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
if (region.getRegionInfo().getRegionNameAsString().startsWith(tableName.getNameAsString())) {
CoprocessorEnvironment env;
env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName1);
if (env != null) {
found1 = true;
}
env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName2);
if (env != null) {
found2 = true;
Configuration conf = env.getConfiguration();
found2_k1 = conf.get("k1") != null;
found2_k2 = conf.get("k2") != null;
found2_k3 = conf.get("k3") != null;
}
}
}
assertTrue("Class " + cpName1 + " was missing on a region", found1);
assertTrue("Class " + cpName2 + " was missing on a region", found2);
assertTrue("Configuration key 'k1' was missing on a region", found2_k1);
assertTrue("Configuration key 'k2' was missing on a region", found2_k2);
assertTrue("Configuration key 'k3' was missing on a region", found2_k3);
}
static boolean checkTable(Admin admin, TestOptions opts) throws IOException {
TableName tableName = TableName.valueOf(opts.tableName);
boolean needsDelete = false, exists = admin.tableExists(tableName);
boolean isReadCmd = opts.cmdName.toLowerCase(Locale.ROOT).contains("read")
|| opts.cmdName.toLowerCase(Locale.ROOT).contains("scan");
if (!exists && isReadCmd) {
throw new IllegalStateException(
"Must specify an existing table for read commands. Run a write command first.");
}
TableDescriptor desc =
exists ? admin.getDescriptor(TableName.valueOf(opts.tableName)) : null;
byte[][] splits = getSplits(opts);
// recreate the table when user has requested presplit or when existing
// {RegionSplitPolicy,replica count} does not match requested, or when the
// number of column families does not match requested.
if ((exists && opts.presplitRegions != DEFAULT_OPTS.presplitRegions)
|| (!isReadCmd && desc != null &&
!StringUtils.equals(desc.getRegionSplitPolicyClassName(), opts.splitPolicy))
|| (!isReadCmd && desc != null && desc.getRegionReplication() != opts.replicas)
|| (desc != null && desc.getColumnFamilyCount() != opts.families)) {
needsDelete = true;
// wait, why did it delete my table?!?
LOG.debug(MoreObjects.toStringHelper("needsDelete")
.add("needsDelete", needsDelete)
.add("isReadCmd", isReadCmd)
.add("exists", exists)
.add("desc", desc)
.add("presplit", opts.presplitRegions)
.add("splitPolicy", opts.splitPolicy)
.add("replicas", opts.replicas)
.add("families", opts.families)
.toString());
}
// remove an existing table
if (needsDelete) {
if (admin.isTableEnabled(tableName)) {
admin.disableTable(tableName);
}
admin.deleteTable(tableName);
}
// table creation is necessary
if (!exists || needsDelete) {
desc = getTableDescriptor(opts);
if (splits != null) {
if (LOG.isDebugEnabled()) {
for (int i = 0; i < splits.length; i++) {
LOG.debug(" split " + i + ": " + Bytes.toStringBinary(splits[i]));
}
}
}
if (splits != null) {
admin.createTable(desc, splits);
} else {
admin.createTable(desc);
}
LOG.info("Table " + desc + " created");
}
return admin.tableExists(tableName);
}
/**
* Returns if given table exists and is enabled.
*
* @param hbaseAdmin HBase admin, must be initialized
* @param tableName table name
* @return true if table exists
* @throws IOException if a remote or network exception occurs when connecting to HBase
*/
public static boolean isTableAvailable(Admin hbaseAdmin, String tableName) throws IOException {
TableName name = TableName.valueOf(tableName);
return hbaseAdmin.isTableAvailable(name) &&
hbaseAdmin.isTableEnabled(name);
}