Example usage for com.amazonaws.services.dynamodbv2.model TableDescription getTableStatus

List of usage examples for com.amazonaws.services.dynamodbv2.model TableDescription getTableStatus

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model TableDescription getTableStatus.

Prototype


public String getTableStatus() 

Source Link

Document

The current state of the table:

  • CREATING - The table is being created.

    Usage

    From source file:TableCreator.java

    License:Open Source License

    private static void waitForTableToBecomeAvailable(String tableName) {
        System.out.println("Waiting for " + tableName + " to become ACTIVE...");
    
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (10 * 60 * 1000);
        while (System.currentTimeMillis() < endTime) {
            try {//from  www  .j  a va  2  s . c o m
                Thread.sleep(1000 * 20);
            } catch (Exception e) {
            }
            try {
                DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
                TableDescription tableDescription = dynamoDB.describeTable(request).getTable();
                String tableStatus = tableDescription.getTableStatus();
                System.out.println("  - current state: " + tableStatus);
                if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                    return;
            } catch (AmazonServiceException ase) {
                if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
                    throw ase;
            }
        }
    
        throw new RuntimeException("Table " + tableName + " never went active");
    }
    

    From source file:TableCreator.java

    License:Open Source License

    private static void waitForTableToDelete(String tableName) {
        System.out.println("Waiting for " + tableName + " to become ACTIVE...");
    
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (10 * 60 * 1000);
        while (System.currentTimeMillis() < endTime) {
            try {/*from www. j a v  a  2s  .  co  m*/
                Thread.sleep(1000 * 20);
            } catch (Exception e) {
            }
            try {
                DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
                TableDescription tableDescription = dynamoDB.describeTable(request).getTable();
                String tableStatus = tableDescription.getTableStatus();
                System.out.println("  - current state: " + tableStatus);
                if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                    return;
            } catch (AmazonServiceException ase) {
                if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
                    throw ase;
            }
        }
    
        throw new RuntimeException("Table " + tableName + " never went active");
    }
    

    From source file:aws.example.dynamodb.DescribeTable.java

    License:Open Source License

    public static void main(String[] args) {
        final String USAGE = "\n" + "Usage:\n" + "    DescribeTable <table>\n\n" + "Where:\n"
                + "    table - the table to get information about.\n\n" + "Example:\n"
                + "    DescribeTable HelloTable\n";
    
        if (args.length < 1) {
            System.out.println(USAGE);
            System.exit(1);//w  w w . ja  v a  2  s .c o m
        }
    
        String table_name = args[0];
        System.out.format("Getting description for %s\n\n", table_name);
    
        final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();
    
        try {
            TableDescription table_info = ddb.describeTable(table_name).getTable();
    
            if (table_info != null) {
                System.out.format("Table name  : %s\n", table_info.getTableName());
                System.out.format("Table ARN   : %s\n", table_info.getTableArn());
                System.out.format("Status      : %s\n", table_info.getTableStatus());
                System.out.format("Item count  : %d\n", table_info.getItemCount().longValue());
                System.out.format("Size (bytes): %d\n", table_info.getTableSizeBytes().longValue());
    
                ProvisionedThroughputDescription throughput_info = table_info.getProvisionedThroughput();
                System.out.println("Throughput");
                System.out.format("  Read Capacity : %d\n", throughput_info.getReadCapacityUnits().longValue());
                System.out.format("  Write Capacity: %d\n", throughput_info.getWriteCapacityUnits().longValue());
    
                List<AttributeDefinition> attributes = table_info.getAttributeDefinitions();
                System.out.println("Attributes");
                for (AttributeDefinition a : attributes) {
                    System.out.format("  %s (%s)\n", a.getAttributeName(), a.getAttributeType());
                }
            }
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }
        System.out.println("\nDone!");
    }
    

    From source file:awslabs.lab22.Lab22.java

    License:Open Source License

    /**
     * Validate the table matches the expected schema for the lab. Since it's built by hand, there's a decent chance
     * that it may not be what's expected.//from  w  w w .  ja v  a 2  s.com
     * 
     * @param ddbClient The DynamoDB client object.
     * @param tableName The name of the table to validate.
     * 
     * @return True if the schema of the table is correct. False if a problem is found.
     */
    private static Boolean confirmTableSchema(AmazonDynamoDBClient ddbClient, String tableName) {
        System.out.println("Confirming table schema.");
        TableDescription tableDescription = optionalLabCode.getTableDescription(ddbClient, tableName);
        if (tableDescription == null) {
            System.out.println("Table does not exist.");
            // Can't match the schema if the table isn't there.
            return false;
        }
        if (!tableDescription.getTableStatus().equals("ACTIVE")) {
            System.out.println("Table is not active.");
            return false;
        }
    
        if (tableDescription.getAttributeDefinitions() == null || tableDescription.getKeySchema() == null) {
            System.out.println("Schema doesn't match.");
            return false;
        }
        for (AttributeDefinition attributeDefinition : tableDescription.getAttributeDefinitions()) {
            String attributeName = attributeDefinition.getAttributeName();
            if (attributeName.equals("Company") || attributeName.equals("Email") || attributeName.equals("First")
                    || attributeName.equals("Last")) {
                if (!attributeDefinition.getAttributeType().equals("S")) {
                    // We have a matching attribute, but the type is wrong.
                    System.out.println(attributeDefinition.getAttributeName()
                            + " attribute is wrong type in attribute definition.");
                    return false;
                }
            } else if (attributeName.equals("Age")) {
                if (!attributeDefinition.getAttributeType().equals("N")) {
                    System.out.println("Age attribute is wrong type in attribute definition.");
                    // We have a matching attribute, but the type is wrong.
                    return false;
                }
            }
        }
        // If we've gotten here, the attributes are good. Now check the key
        // schema.
        if (tableDescription.getKeySchema().size() != 2) {
            System.out.println("Wrong number of elements in the key schema.");
            return false;
        }
        for (KeySchemaElement keySchemaElement : tableDescription.getKeySchema()) {
            String attributeName = keySchemaElement.getAttributeName();
            if (attributeName.equals("Company")) {
                if (!keySchemaElement.getKeyType().equals("HASH")) {
                    // We have a matching attribute, but the type is wrong.
                    System.out.println("Company attribute is wrong type in key schema.");
                    return false;
                }
            } else if (attributeName.equals("Email")) {
                if (!keySchemaElement.getKeyType().equals("RANGE")) {
                    // We have a matching attribute, but the type is wrong.
                    System.out.println("Email attribute is wrong type in key schema.");
                    return false;
                }
            } else {
                System.out.println(
                        "Unexpected attribute (" + keySchemaElement.getAttributeName() + ") in the key schema.");
            }
        }
        System.out.println("Table schema is as expected.");
        // We've passed our checks.
        return true;
    
    }
    

    From source file:awslabs.lab22.SolutionCode.java

    License:Open Source License

    @Override
    public String getTableStatus(AmazonDynamoDBClient ddbClient, String tableName) {
        TableDescription tableDescription = getTableDescription(ddbClient, tableName);
        if (tableDescription == null) {
            return "NOTFOUND";
        }// w  ww .  j  av a  2  s  .  c  o  m
        return tableDescription.getTableStatus();
    }
    

    From source file:awslabs.lab51.SolutionCode.java

    License:Open Source License

    @Override
    public Boolean validateSchema(TableDescription tableDescription) {
        if (tableDescription == null) {
            labController.logMessageToPage("Null table description passed to validation method.");
            return false;
        }// w  w  w.j av a  2 s. co  m
        if (!tableDescription.getTableStatus().equals("ACTIVE")) {
            labController.logMessageToPage("Table is not active.");
            return false;
        }
    
        if (tableDescription.getAttributeDefinitions() == null || tableDescription.getKeySchema() == null) {
            labController.logMessageToPage("Schema doesn't match.");
            return false;
        }
        for (AttributeDefinition attributeDefinition : tableDescription.getAttributeDefinitions()) {
            String attributeName = attributeDefinition.getAttributeName();
            if (attributeName.equals("Key") || attributeName.equals("Bucket")) {
                if (!attributeDefinition.getAttributeType().equals("S")) {
                    // We have a matching attribute, but the type is wrong.
                    labController.logMessageToPage(attributeDefinition.getAttributeName()
                            + " attribute is wrong type in attribute definition.");
                    return false;
                }
            }
        }
        // If we've gotten here, the attributes are good. Now check the key schema.
        if (tableDescription.getKeySchema().size() != 2) {
            labController.logMessageToPage("Wrong number of elements in the key schema.");
            return false;
        }
        for (KeySchemaElement keySchemaElement : tableDescription.getKeySchema()) {
            String attributeName = keySchemaElement.getAttributeName();
            if (attributeName.equals("Key")) {
                if (!keySchemaElement.getKeyType().equals("HASH")) {
                    // We have a matching attribute, but the type is wrong.
                    labController.logMessageToPage("Key attribute is wrong type in key schema.");
                    return false;
                }
            } else if (attributeName.equals("Bucket")) {
                if (!keySchemaElement.getKeyType().equals("RANGE")) {
                    // We have a matching attribute, but the type is wrong.
                    labController.logMessageToPage("Bucket attribute is wrong type in key schema.");
                    return false;
                }
            } else {
                labController.logMessageToPage(
                        "Unexpected attribute (" + keySchemaElement.getAttributeName() + ") in the key schema.");
            }
        }
        labController.logMessageToPage("Table schema is valid.");
        // We've passed our checks.
        return true;
    }
    

    From source file:com.amazon.janusgraph.diskstorage.dynamodb.DynamoDbDelegate.java

    License:Open Source License

    public void waitForTableCreation(final String tableName, final boolean verifyIndexesList,
            final List<LocalSecondaryIndexDescription> expectedLsiList,
            final List<GlobalSecondaryIndexDescription> expectedGsiList) throws BackendException {
        boolean successFlag = false;
        int retryCount = 0;
        while (!successFlag && retryCount < maxRetries) {
            try {/*from ww  w.  j  av a 2 s  . c o  m*/
                boolean areAllGsisActive = true;
                final TableDescription td = describeTable(tableName);
                if (verifyIndexesList) {
                    final Set<LocalSecondaryIndexDescription> expectedLSIs = new HashSet<LocalSecondaryIndexDescription>();
                    if (expectedLsiList != null) {
                        expectedLSIs.addAll(expectedLsiList);
                    }
                    final Set<LocalSecondaryIndexDescription> actualLSIs = new HashSet<LocalSecondaryIndexDescription>();
                    if (td.getLocalSecondaryIndexes() != null) {
                        actualLSIs.addAll(td.getLocalSecondaryIndexes());
                    }
                    // the lsi list should be there even if the table is in creating state
                    if (!(expectedLsiList == null && td.getLocalSecondaryIndexes() == null
                            || expectedLSIs.equals(actualLSIs))) {
                        throw new PermanentBackendException(
                                "LSI list is not as expected during table creation. expectedLsiList="
                                        + expectedLsiList.toString() + "; table description=" + td.toString());
                    }
    
                    // ignore the status of all GSIs since they will mess up .equals()
                    if (td.getGlobalSecondaryIndexes() != null) {
                        for (final GlobalSecondaryIndexDescription gDesc : td.getGlobalSecondaryIndexes()) {
                            if (!isTableAcceptingWrites(gDesc.getIndexStatus())) {
                                areAllGsisActive = false;
                                break;
                            }
                        }
                    }
    
                    // the gsi list should be there even if the table is in creating state
                    if (!areGsisSameConfiguration(expectedGsiList, td.getGlobalSecondaryIndexes())) {
                        throw new PermanentBackendException(
                                "GSI list is not as expected during table creation. expectedGsiList="
                                        + expectedGsiList.toString() + "; table description=" + td.toString());
                    }
                }
                successFlag = isTableAcceptingWrites(td.getTableStatus()) && areAllGsisActive;
            } catch (BackendNotFoundException ignore) {
                successFlag = false;
            }
    
            if (!successFlag) {
                interruptibleSleep(CONTROL_PLANE_RETRY_DELAY_MS);
            }
            retryCount++;
        }
        if (!successFlag) {
            throw new PermanentBackendException(
                    "Table creation not completed for table " + tableName + " after retrying " + this.maxRetries
                            + " times for a duration of " + CONTROL_PLANE_RETRY_DELAY_MS * this.maxRetries + " ms");
        }
    }
    

    From source file:com.amazon.janusgraph.diskstorage.dynamodb.DynamoDbDelegate.java

    License:Open Source License

    void createTableAndWaitForActive(final CreateTableRequest request) throws BackendException {
        final String tableName = request.getTableName();
        Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName), "Table name was null or empty");
        final TableDescription desc;
        try {//from   ww w . jav a  2  s.c o  m
            desc = this.describeTable(tableName);
            if (null != desc && isTableAcceptingWrites(desc.getTableStatus())) {
                return; //store existed
            }
        } catch (BackendNotFoundException e) {
            log.debug(tableName + " did not exist yet, creating it", e);
        }
    
        createTable(request);
        waitForTableCreation(tableName, false /*verifyIndexesList*/, null /*expectedLsiList*/,
                null /*expectedGsiList*/);
    }
    

    From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.manager.DynamoDbTemplateInfrastructureManager.java

    License:Apache License

    private boolean isTableCreated(final String tableName) {
        try {/*from   w w w .  j a v  a2 s. co  m*/
            final DescribeTableResult result = amazonDynamoDbClient
                    .describeTable(new DescribeTableRequest(tableName));
            final TableDescription tableDescription = result.getTable();
            final String tableStatus = tableDescription.getTableStatus();
            final String returnedTableName = tableDescription.getTableName();
            return tableName.equals(returnedTableName) && TableStatus.ACTIVE.toString().equals(tableStatus);
        } catch (final ResourceNotFoundException e) {
            return false;
        }
    }
    

    From source file:com.erudika.para.persistence.AWSDynamoUtils.java

    License:Apache License

    /**
     * Gives basic information about a DynamoDB table (status, creation date, size).
     * @param appid name of the {@link com.erudika.para.core.App}
     * @return a map/*w  w w  .  j  a  v  a2s.  c  o m*/
     */
    public static Map<String, Object> getTableStatus(final String appid) {
        if (StringUtils.isBlank(appid)) {
            return Collections.emptyMap();
        }
        try {
            final TableDescription td = getClient().describeTable(getTableNameForAppid(appid)).getTable();
            return new HashMap<String, Object>() {
                {
                    put("id", appid);
                    put("status", td.getTableStatus());
                    put("created", td.getCreationDateTime().getTime());
                    put("sizeBytes", td.getTableSizeBytes());
                    put("itemCount", td.getItemCount());
                    put("readCapacityUnits", td.getProvisionedThroughput().getReadCapacityUnits());
                    put("writeCapacityUnits", td.getProvisionedThroughput().getWriteCapacityUnits());
                }
            };
        } catch (Exception e) {
            logger.error(null, e);
        }
        return Collections.emptyMap();
    }