Java Code Examples for org.apache.hadoop.hbase.client.Admin#tableExists()

The following examples show how to use org.apache.hadoop.hbase.client.Admin#tableExists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestHelloHBase.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateNamespaceAndTable() throws Exception {
  Admin admin = TEST_UTIL.getAdmin();
  HelloHBase.createNamespaceAndTable(admin);

  boolean namespaceExists
          = HelloHBase.namespaceExists(admin, HelloHBase.MY_NAMESPACE_NAME);
  assertEquals("#createNamespaceAndTable failed to create namespace.",
          true, namespaceExists);

  boolean tableExists = admin.tableExists(HelloHBase.MY_TABLE_NAME);
  assertEquals("#createNamespaceAndTable failed to create table.",
          true, tableExists);

  admin.disableTable(HelloHBase.MY_TABLE_NAME);
  admin.deleteTable(HelloHBase.MY_TABLE_NAME);
  admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
}
 
Example 2
Source File: TestGzipFilter.java    From hbase with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  TEST_UTIL.startMiniCluster();
  REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
  client = new Client(new Cluster().add("localhost",
    REST_TEST_UTIL.getServletPort()));
  Admin admin = TEST_UTIL.getAdmin();
  if (admin.tableExists(TABLE)) {
    return;
  }
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(TABLE);
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFA)).build();
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
  admin.createTable(tableDescriptorBuilder.build());
}
 
Example 3
Source File: HBaseNotificationStoreIntegrationTest.java    From streamline with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpClass() throws IOException {
    connection = ConnectionFactory.createConnection();
    Admin admin = connection.getAdmin();

    for (Map.Entry<String, List<String>> tableInfo : tableToCfs.entrySet()) {
        TableName tableName = TableName.valueOf(tableInfo.getKey());
        if (!admin.tableExists(tableName)) {
            // table not found, creating
            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
            for (String columnFamily : tableInfo.getValue()) {
                tableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
            }
            admin.createTable(tableDescriptor);
        }
    }
}
 
Example 4
Source File: SpaceQuotaHelperForTests.java    From hbase with Apache License 2.0 6 votes vote down vote up
TableName createTableInNamespace(NamespaceDescriptor nd) throws Exception {
  final Admin admin = testUtil.getAdmin();
  final TableName tn = TableName.valueOf(nd.getName(),
      testName.getMethodName() + counter.getAndIncrement());

  // Delete the old table
  if (admin.tableExists(tn)) {
    admin.disableTable(tn);
    admin.deleteTable(tn);
  }

  // Create the table
  TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tn)
      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(F1)).build();

  admin.createTable(tableDesc);
  return tn;
}
 
Example 5
Source File: TestTableScan.java    From hbase with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  conf = TEST_UTIL.getConfiguration();
  conf.set(Constants.CUSTOM_FILTERS, "CustomFilter:" + CustomFilter.class.getName());
  TEST_UTIL.startMiniCluster();
  REST_TEST_UTIL.startServletContainer(conf);
  client = new Client(new Cluster().add("localhost",
    REST_TEST_UTIL.getServletPort()));
  Admin admin = TEST_UTIL.getAdmin();
  if (!admin.tableExists(TABLE)) {
    TableDescriptorBuilder tableDescriptorBuilder =
      TableDescriptorBuilder.newBuilder(TABLE);
    ColumnFamilyDescriptor columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFA)).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFB)).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());
    expectedRows1 = TestScannerResource.insertData(conf, TABLE, COLUMN_1, 1.0);
    expectedRows2 = TestScannerResource.insertData(conf, TABLE, COLUMN_2, 0.5);
    expectedRows3 = TestScannerResource.insertData(conf, TABLE, COLUMN_EMPTY, 1.0);
  }
}
 
Example 6
Source File: HelloHBase.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes Admin#createNamespace and Admin#createTable to create a namespace
 * with a table that has one column-family.
 *
 * @param admin Standard Admin object
 * @throws IOException If IO problem encountered
 */
static void createNamespaceAndTable(final Admin admin) throws IOException {

  if (!namespaceExists(admin, MY_NAMESPACE_NAME)) {
    System.out.println("Creating Namespace [" + MY_NAMESPACE_NAME + "].");

    admin.createNamespace(NamespaceDescriptor
            .create(MY_NAMESPACE_NAME).build());
  }
  if (!admin.tableExists(MY_TABLE_NAME)) {
    System.out.println("Creating Table [" + MY_TABLE_NAME.getNameAsString()
            + "], with one Column Family ["
            + Bytes.toString(MY_COLUMN_FAMILY_NAME) + "].");

    admin.createTable(new TableDescriptorBuilder.ModifyableTableDescriptor(MY_TABLE_NAME)
      .setColumnFamily(
        new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
          MY_COLUMN_FAMILY_NAME)));
  }
}
 
Example 7
Source File: SecondaryIndexTable.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
public SecondaryIndexTable(TransactionServiceClient transactionServiceClient, Table hTable,
                           byte[] secondaryIndex) throws IOException {
  secondaryIndexTableName = TableName.valueOf(hTable.getName().getNameAsString() + ".idx");
  Table secondaryIndexHTable = null;
  try {
    conn  = ConnectionFactory.createConnection(hTable.getConfiguration());
    Admin hBaseAdmin = conn.getAdmin();
    if (!hBaseAdmin.tableExists(secondaryIndexTableName)) {
      hBaseAdmin.createTable(new HTableDescriptor(secondaryIndexTableName));
    }
    secondaryIndexHTable = conn.getTable(secondaryIndexTableName);
  } catch (Exception e) {
    Throwables.propagate(e);
  } 

  this.secondaryIndex = secondaryIndex;
  this.transactionAwareHTable = new TransactionAwareHTable(hTable);
  this.secondaryIndexTable = new TransactionAwareHTable(secondaryIndexHTable);
  this.transactionContext = new TransactionContext(transactionServiceClient, transactionAwareHTable,
                                                   secondaryIndexTable);
}
 
Example 8
Source File: TestRegionObserverBypass.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: IntegrationTestDDLMasterFailover.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
void perform() throws IOException {
  Admin admin = connection.getAdmin();
  try {
    TableDescriptor td = createTableDesc();
    TableName tableName = td.getTableName();
    if ( admin.tableExists(tableName)){
      return;
    }
    String numRegionKey = String.format(NUM_REGIONS_KEY, this.getClass().getSimpleName());
    numRegions = getConf().getInt(numRegionKey, DEFAULT_NUM_REGIONS);
    byte[] startKey = Bytes.toBytes("row-0000000000");
    byte[] endKey = Bytes.toBytes("row-" + Integer.MAX_VALUE);
    LOG.info("Creating table:" + td);
    admin.createTable(td, startKey, endKey, numRegions);
    Assert.assertTrue("Table: " + td + " was not created", admin.tableExists(tableName));
    TableDescriptor freshTableDesc = admin.getDescriptor(tableName);
    Assert.assertTrue(
      "After create, Table: " + tableName + " in not enabled", admin.isTableEnabled(tableName));
    enabledTables.put(tableName, freshTableDesc);
    LOG.info("Created table:" + freshTableDesc);
  } catch (Exception e) {
    LOG.warn("Caught exception in action: " + this.getClass());
    throw e;
  } finally {
    admin.close();
  }
}
 
Example 10
Source File: RowResourceBase.java    From hbase with Apache License 2.0 5 votes vote down vote up
@After
public void afterMethod() throws Exception {
  Admin admin = TEST_UTIL.getAdmin();
  if (admin.tableExists(TABLE_NAME)) {
    TEST_UTIL.deleteTable(TABLE_NAME);
  }
}
 
Example 11
Source File: HelloHBase.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes Admin#disableTable, Admin#deleteTable, and Admin#deleteNamespace to
 * disable/delete Table and delete Namespace.
 *
 * @param admin Standard Admin object
 * @throws IOException If IO problem is encountered
 */
static void deleteNamespaceAndTable(final Admin admin) throws IOException {
  if (admin.tableExists(MY_TABLE_NAME)) {
    System.out.println("Disabling/deleting Table ["
            + MY_TABLE_NAME.getNameAsString() + "].");
    admin.disableTable(MY_TABLE_NAME); // Disable a table before deleting it.
    admin.deleteTable(MY_TABLE_NAME);
  }
  if (namespaceExists(admin, MY_NAMESPACE_NAME)) {
    System.out.println("Deleting Namespace [" + MY_NAMESPACE_NAME + "].");
    admin.deleteNamespace(MY_NAMESPACE_NAME);
  }
}
 
Example 12
Source File: CreateTable.java    From cloud-bigtable-examples with Apache License 2.0 5 votes vote down vote up
private static boolean tableExists(TableName tableName, Admin admin)  {
  try {
    return admin.tableExists(tableName);
  } catch (Exception e) {
    LOG.error("Could not figure out if table " + tableName + " exists.", e);
    return false;
  } finally {
  }
}
 
Example 13
Source File: AcidGuaranteesTestTool.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void createTableIfMissing(Admin admin, boolean useMob) throws IOException {
  if (!admin.tableExists(TABLE_NAME)) {
    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TABLE_NAME);
    Stream.of(FAMILIES).map(ColumnFamilyDescriptorBuilder::of)
        .forEachOrdered(builder::setColumnFamily);
    admin.createTable(builder.build());
  }
  ColumnFamilyDescriptor cfd = admin.getDescriptor(TABLE_NAME).getColumnFamilies()[0];
  if (cfd.isMobEnabled() != useMob) {
    admin.modifyColumnFamily(TABLE_NAME, ColumnFamilyDescriptorBuilder.newBuilder(cfd)
        .setMobEnabled(useMob).setMobThreshold(4).build());
  }
}
 
Example 14
Source File: IndexVerificationResultRepository.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public void createResultTable(Connection connection) throws IOException, SQLException {
    ConnectionQueryServices queryServices = connection.unwrap(PhoenixConnection.class).getQueryServices();
    Admin admin = queryServices.getAdmin();
    TableName resultTableName = TableName.valueOf(RESULT_TABLE_NAME);
    if (!admin.tableExists(resultTableName)) {
        ColumnFamilyDescriptor columnDescriptor =
            ColumnFamilyDescriptorBuilder.newBuilder(RESULT_TABLE_COLUMN_FAMILY).
                setTimeToLive(MetaDataProtocol.DEFAULT_LOG_TTL).build();
        TableDescriptor tableDescriptor =
            TableDescriptorBuilder.newBuilder(resultTableName).
                setColumnFamily(columnDescriptor).build();
        admin.createTable(tableDescriptor);
        resultHTable = admin.getConnection().getTable(resultTableName);
    }
}
 
Example 15
Source File: TestSequenceIdMonotonicallyIncreasing.java    From hbase with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() throws IOException {
  Admin admin = UTIL.getAdmin();
  if (admin.tableExists(NAME)) {
    admin.disableTable(NAME);
    admin.deleteTable(NAME);
  }
}
 
Example 16
Source File: CubeHTableUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static void deleteHTable(TableName tableName) throws IOException {
    Admin admin = HBaseConnection.get(KylinConfig.getInstanceFromEnv().getStorageUrl()).getAdmin();
    try {
        if (admin.tableExists(tableName)) {
            logger.info("disabling hbase table " + tableName);
            admin.disableTable(tableName);
            logger.info("deleting hbase table " + tableName);
            admin.deleteTable(tableName);
        }
    } finally {
        IOUtils.closeQuietly(admin);
    }
}
 
Example 17
Source File: TestSchemaResource.java    From hbase with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() throws Exception {
  Admin admin = TEST_UTIL.getAdmin();

  for (String table : new String[] {TABLE1, TABLE2}) {
    TableName t = TableName.valueOf(table);
    if (admin.tableExists(t)) {
      admin.disableTable(t);
      admin.deleteTable(t);
    }
  }

  conf.set("hbase.rest.readonly", "false");
}
 
Example 18
Source File: TestClassLoading.java    From hbase with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 19
Source File: LookupTableToHFileJob.java    From kylin with Apache License 2.0 4 votes vote down vote up
private boolean hTableExists(Admin admin, String hTableName) throws IOException {
    return admin.tableExists(TableName.valueOf(hTableName));
}
 
Example 20
Source File: TestAccessController.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testPermissionList() throws Exception {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  final byte[] family1 = Bytes.toBytes("f1");
  final byte[] family2 = Bytes.toBytes("f2");
  final byte[] qualifier = Bytes.toBytes("q");

  // create table
  Admin admin = TEST_UTIL.getAdmin();
  if (admin.tableExists(tableName)) {
    deleteTable(TEST_UTIL, tableName);
  }
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(family1));
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(family2));
  tableDescriptor.setOwner(USER_OWNER);
  createTable(TEST_UTIL, tableDescriptor);
  try {
    List<UserPermission> perms =
        admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build());
    UserPermission ownerperm = new UserPermission(USER_OWNER.getName(),
        Permission.newBuilder(tableName).withActions(Action.values()).build());
    assertTrue("Owner should have all permissions on table",
      hasFoundUserPermission(ownerperm, perms));

    User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "user", new String[0]);
    String userName = user.getShortName();

    UserPermission up =
        new UserPermission(userName, Permission.newBuilder(tableName).withFamily(family1)
            .withQualifier(qualifier).withActions(Permission.Action.READ).build());
    assertFalse("User should not be granted permission: " + up.toString(),
      hasFoundUserPermission(up, perms));

    // grant read permission
    grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
      Permission.Action.READ);

    perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build());
    UserPermission upToVerify =
        new UserPermission(userName, Permission.newBuilder(tableName).withFamily(family1)
            .withQualifier(qualifier).withActions(Permission.Action.READ).build());
    assertTrue("User should be granted permission: " + upToVerify.toString(),
      hasFoundUserPermission(upToVerify, perms));

    upToVerify = new UserPermission(userName, Permission.newBuilder(tableName).withFamily(family1)
        .withQualifier(qualifier).withActions(Permission.Action.WRITE).build());
    assertFalse("User should not be granted permission: " + upToVerify.toString(),
      hasFoundUserPermission(upToVerify, perms));

    // grant read+write
    grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
      Permission.Action.WRITE, Permission.Action.READ);

    perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build());
    upToVerify = new UserPermission(userName,
        Permission.newBuilder(tableName).withFamily(family1).withQualifier(qualifier)
            .withActions(Permission.Action.WRITE, Permission.Action.READ).build());
    assertTrue("User should be granted permission: " + upToVerify.toString(),
      hasFoundUserPermission(upToVerify, perms));

    // revoke
    revokeFromTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
      Permission.Action.WRITE, Permission.Action.READ);

    perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build());
    assertFalse("User should not be granted permission: " + upToVerify.toString(),
      hasFoundUserPermission(upToVerify, perms));

    // disable table before modification
    admin.disableTable(tableName);

    User newOwner = User.createUserForTesting(conf, "new_owner", new String[] {});
    tableDescriptor.setOwner(newOwner);
    admin.modifyTable(tableDescriptor);

    perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build());
    UserPermission newOwnerperm = new UserPermission(newOwner.getName(),
        Permission.newBuilder(tableName).withActions(Action.values()).build());
    assertTrue("New owner should have all permissions on table",
      hasFoundUserPermission(newOwnerperm, perms));
  } finally {
    // delete table
    deleteTable(TEST_UTIL, tableName);
  }
}