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

The following examples show how to use org.apache.hadoop.hbase.client.Admin#truncateTable() . 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: TestBackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void cleanBackupTable() throws IOException {
  Admin admin = UTIL.getAdmin();
  admin.disableTable(BackupSystemTable.getTableName(conf));
  admin.truncateTable(BackupSystemTable.getTableName(conf), true);
  if (admin.isTableDisabled(BackupSystemTable.getTableName(conf))) {
    admin.enableTable(BackupSystemTable.getTableName(conf));
  }
}
 
Example 2
Source File: HBaseTestingUtility.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Truncate a table using the admin command.
 * Effectively disables, deletes, and recreates the table.
 * @param tableName table which must exist.
 * @param preserveRegions keep the existing split points
 * @return HTable for the new table
 */
public Table truncateTable(final TableName tableName, final boolean preserveRegions) throws
    IOException {
  Admin admin = getAdmin();
  if (!admin.isTableDisabled(tableName)) {
    admin.disableTable(tableName);
  }
  admin.truncateTable(tableName, preserveRegions);
  return getConnection().getTable(tableName);
}
 
Example 3
Source File: TruncateTableAction.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void perform() throws Exception {
  HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
  Admin admin = util.getAdmin();

  // Don't try the truncate if we're stopping
  if (context.isStopping()) {
    return;
  }

  boolean preserveSplits = random.nextBoolean();
  getLogger().info("Performing action: Truncate table {} preserve splits {}",
    tableName.getNameAsString(), preserveSplits);
  admin.truncateTable(tableName, preserveSplits);
}
 
Example 4
Source File: IndexScrutinyToolForTenantIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void testWithOutput(OutputFormat outputFormat) throws Exception {
    connTenant.createStatement()
            .execute(String.format(upsertQueryStr, tenantViewName, tenantId, 1, "x"));
    connTenant.createStatement()
            .execute(String.format(upsertQueryStr, tenantViewName, tenantId, 2, "x2"));
    connTenant.createStatement()
            .execute(String.format(upsertQueryStr, tenantViewName, tenantId, 3, "x3"));
    connTenant.createStatement().execute(String.format("UPSERT INTO %s (\":ID\", \"0:NAME\") values (%d, '%s')",
            indexNameTenant, 5555, "wrongName"));
    connTenant.commit();

    ConnectionQueryServices queryServices = connGlobal.unwrap(PhoenixConnection.class).getQueryServices();
    Admin admin = queryServices.getAdmin();
    TableName tableName = TableName.valueOf(viewIndexTableName);
    admin.disableTable(tableName);
    admin.truncateTable(tableName, false);

    String[]
            argValues =
            getArgValues("", tenantViewName, indexNameTenant, 10L, SourceTable.DATA_TABLE_SOURCE, true, outputFormat, null,
                    tenantId, EnvironmentEdgeManager.currentTimeMillis());
    List<Job> completedJobs = runScrutiny(argValues);

    assertEquals(1, completedJobs.size());
    for (Job job : completedJobs) {
        assertTrue(job.isSuccessful());
        Counters counters = job.getCounters();
        assertEquals(0, getCounterValue(counters, VALID_ROW_COUNT));
        assertEquals(3, getCounterValue(counters, INVALID_ROW_COUNT));
    }
}
 
Example 5
Source File: TestReplicator.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void truncateTable(HBaseTestingUtility util, TableName tablename) throws IOException {
  Admin admin = util.getAdmin();
  admin.disableTable(tableName);
  admin.truncateTable(tablename, false);
}
 
Example 6
Source File: IndexRebuildTaskIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndexRebuildTask() throws Throwable {
    String baseTable = generateUniqueName();
    String viewName = generateUniqueName();
    Connection conn = null;
    Connection tenantConn = null;
    try {
        conn = DriverManager.getConnection(getUrl());
        conn.setAutoCommit(false);
        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
        props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, TENANT1);

        tenantConn =DriverManager.getConnection(getUrl(), props);
        String ddlFormat =
                "CREATE TABLE IF NOT EXISTS " + baseTable + "  ("
                        + " %s PK2 VARCHAR NOT NULL, V1 VARCHAR, V2 VARCHAR "
                        + " CONSTRAINT NAME_PK PRIMARY KEY (%s PK2)" + " ) %s";
        conn.createStatement().execute(generateDDL(ddlFormat));
        conn.commit();
        // Create a view
        String viewDDL = "CREATE VIEW " + viewName + " AS SELECT * FROM " + baseTable;
        tenantConn.createStatement().execute(viewDDL);

        // Create index
        String indexName = generateUniqueName();
        String idxSDDL = String.format("CREATE INDEX %s ON %s (V1)", indexName, viewName);

        tenantConn.createStatement().execute(idxSDDL);

        // Insert rows
        int numOfValues = 1000;
        for (int i=0; i < numOfValues; i++){
            tenantConn.createStatement().execute(
                    String.format("UPSERT INTO %s VALUES('%s', '%s', '%s')", viewName, String.valueOf(i), "y",
                            "z"));
        }
        tenantConn.commit();

        waitForIndexRebuild(conn, indexName, PIndexState.ACTIVE);
        String viewIndexTableName = MetaDataUtil.getViewIndexPhysicalName(baseTable);
        ConnectionQueryServices queryServices = conn.unwrap(PhoenixConnection.class).getQueryServices();

        Table indexHTable = queryServices.getTable(Bytes.toBytes(viewIndexTableName));
        int count = getUtility().countRows(indexHTable);
        assertEquals(numOfValues, count);

        // Alter to Unusable makes the index status inactive.
        // If I Alter to DISABLE, it fails to in Index tool while setting state to active due to Invalid transition.
        tenantConn.createStatement().execute(
                String.format("ALTER INDEX %s ON %s UNUSABLE", indexName, viewName));
        tenantConn.commit();

        // Remove index contents and try again
        Admin admin = queryServices.getAdmin();
        TableName tableName = TableName.valueOf(viewIndexTableName);
        admin.disableTable(tableName);
        admin.truncateTable(tableName, false);

        count = getUtility().countRows(indexHTable);
        assertEquals(0, count);

        String data = "{\"IndexName\":\"" + indexName + "\"}";

        // Run IndexRebuildTask
        TaskRegionObserver.SelfHealingTask task =
                new TaskRegionObserver.SelfHealingTask(
                        TaskRegionEnvironment, QueryServicesOptions.DEFAULT_TASK_HANDLING_MAX_INTERVAL_MS);

        Timestamp startTs = new Timestamp(EnvironmentEdgeManager.currentTimeMillis());
        Task.addTask(conn.unwrap(PhoenixConnection.class), PTable.TaskType.INDEX_REBUILD,
                TENANT1, null, viewName,
                PTable.TaskStatus.CREATED.toString(), data, null, startTs, null, true);
        task.run();

        // Check task status and other column values.
        waitForTaskState(conn, PTable.TaskType.INDEX_REBUILD, viewName, PTable.TaskStatus.COMPLETED);

        // See that index is rebuilt and confirm index has rows
        count = getUtility().countRows(indexHTable);
        assertEquals(numOfValues, count);
    } finally {
        if (conn != null) {
            conn.createStatement().execute("DELETE " + " FROM " + PhoenixDatabaseMetaData.SYSTEM_TASK_NAME
                    + " WHERE TABLE_NAME ='" + viewName  + "'");
            conn.commit();
            conn.close();
        }
        if (tenantConn != null) {
            tenantConn.close();
        }
    }
}
 
Example 7
Source File: IndexToolIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndexToolWithTenantId() throws Exception {
    if (!useTenantId) { return;}
    String tenantId = generateUniqueName();
    String schemaName = generateUniqueName();
    String dataTableName = generateUniqueName();
    String viewTenantName = generateUniqueName();
    String indexNameGlobal = generateUniqueName();
    String indexNameTenant = generateUniqueName();
    String viewIndexTableName = "_IDX_" + dataTableName;

    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection connGlobal = DriverManager.getConnection(getUrl(), props);
    props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
    Connection connTenant = DriverManager.getConnection(getUrl(), props);
    String createTblStr = "CREATE TABLE %s (TENANT_ID VARCHAR(15) NOT NULL,ID INTEGER NOT NULL"
            + ", NAME VARCHAR, CONSTRAINT PK_1 PRIMARY KEY (TENANT_ID, ID)) MULTI_TENANT=true";
    String createViewStr = "CREATE VIEW %s AS SELECT * FROM %s";

    String upsertQueryStr = "UPSERT INTO %s (TENANT_ID, ID, NAME) VALUES('%s' , %d, '%s')";
    String createIndexStr = "CREATE INDEX %s ON %s (NAME) ";

    try {
        String tableStmtGlobal = String.format(createTblStr, dataTableName);
        connGlobal.createStatement().execute(tableStmtGlobal);

        String viewStmtTenant = String.format(createViewStr, viewTenantName, dataTableName);
        connTenant.createStatement().execute(viewStmtTenant);

        String idxStmtTenant = String.format(createIndexStr, indexNameTenant, viewTenantName);
        connTenant.createStatement().execute(idxStmtTenant);

        connTenant.createStatement()
                .execute(String.format(upsertQueryStr, viewTenantName, tenantId, 1, "x"));
        connTenant.commit();

        runIndexTool(true, false, "", viewTenantName, indexNameTenant,
                tenantId, 0, new String[0]);

        String selectSql = String.format("SELECT ID FROM %s WHERE NAME='x'", viewTenantName);
        ResultSet rs = connTenant.createStatement().executeQuery("EXPLAIN " + selectSql);
        String actualExplainPlan = QueryUtil.getExplainPlan(rs);
        assertExplainPlan(false, actualExplainPlan, "", viewIndexTableName);
        rs = connTenant.createStatement().executeQuery(selectSql);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertFalse(rs.next());

        // Remove from tenant view index and build.
        ConnectionQueryServices queryServices = connGlobal.unwrap(PhoenixConnection.class).getQueryServices();
        Admin admin = queryServices.getAdmin();
        TableName tableName = TableName.valueOf(viewIndexTableName);
        admin.disableTable(tableName);
        admin.truncateTable(tableName, false);

        runIndexTool(true, false, "", viewTenantName, indexNameTenant,
                tenantId, 0, new String[0]);

        Table htable= queryServices.getTable(Bytes.toBytes(viewIndexTableName));
        int count = getUtility().countRows(htable);
        // Confirm index has rows
        assertTrue(count == 1);

        selectSql = String.format("SELECT /*+ INDEX(%s) */ COUNT(*) FROM %s",
                indexNameTenant, viewTenantName);
        rs = connTenant.createStatement().executeQuery(selectSql);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertFalse(rs.next());

        String idxStmtGlobal =
                String.format(createIndexStr, indexNameGlobal, dataTableName);
        connGlobal.createStatement().execute(idxStmtGlobal);

        // run the index MR job this time with tenant id.
        // We expect it to return -1 because indexTable is not correct for this tenant.
        runIndexTool(true, false, schemaName, dataTableName, indexNameGlobal,
                tenantId, -1, new String[0]);

    } finally {
        connGlobal.close();
        connTenant.close();
    }
}