Java Code Examples for com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient#describeTable()

The following examples show how to use com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient#describeTable() . 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: DynamoDBUtils.java    From amazon-kinesis-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies if the table has the expected schema.
 * 
 * @param client
 *        The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges
 * @param tableName
 *        The Amazon DynamoDB table to check
 * @param key
 *        The expected hashkey for the Amazon DynamoDB table
 * @return true if the Amazon DynamoDB table exists and the expected hashkey matches the table schema,
 *         otherwise return false
 */
private static boolean tableHasCorrectSchema(AmazonDynamoDBClient client, String tableName, String key) {
    DescribeTableRequest describeTableRequest = new DescribeTableRequest();
    describeTableRequest.setTableName(tableName);
    DescribeTableResult describeTableResult = client.describeTable(describeTableRequest);
    TableDescription tableDescription = describeTableResult.getTable();
    if (tableDescription.getAttributeDefinitions().size() != 1) {
        LOG.error("The number of attribute definitions does not match the existing table.");
        return false;
    }
    AttributeDefinition attributeDefinition = tableDescription.getAttributeDefinitions().get(0);
    if (!attributeDefinition.getAttributeName().equals(key)
            || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.S.toString())) {
        LOG.error("Attribute name or type does not match existing table.");
        return false;
    }
    List<KeySchemaElement> KSEs = tableDescription.getKeySchema();
    if (KSEs.size() != 1) {
        LOG.error("The number of key schema elements does not match the existing table.");
        return false;
    }
    KeySchemaElement kse = KSEs.get(0);
    if (!kse.getAttributeName().equals(key) || !kse.getKeyType().equals(KeyType.HASH.toString())) {
        LOG.error("The hash key does not match the existing table.");
        return false;
    }
    return true;

}
 
Example 2
Source File: DynamoDBUtils.java    From amazon-kinesis-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to determine if an Amazon DynamoDB table exists.
 * 
 * @param client
 *        The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges
 * @param tableName
 *        The Amazon DynamoDB table to check for
 * @return true if the Amazon DynamoDB table exists, otherwise return false
 */
private static boolean tableExists(AmazonDynamoDBClient client, String tableName) {
    DescribeTableRequest describeTableRequest = new DescribeTableRequest();
    describeTableRequest.setTableName(tableName);
    try {
        client.describeTable(describeTableRequest);
        return true;
    } catch (ResourceNotFoundException e) {
        return false;
    }
}
 
Example 3
Source File: StreamsAdapterDemoHelper.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
public static DescribeTableResult describeTable(AmazonDynamoDBClient client, String tableName) {
    return client.describeTable(new DescribeTableRequest().withTableName(tableName));
}
 
Example 4
Source File: DynamoDBUtils.java    From amazon-kinesis-connectors with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to get the status of an Amazon DynamoDB table
 * 
 * @param client
 *        The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges
 * @param tableName
 *        The Amazon DynamoDB table to get the status of
 * @return
 */
private static TableStatus getTableStatus(AmazonDynamoDBClient client, String tableName) {
    DescribeTableRequest describeTableRequest = new DescribeTableRequest();
    describeTableRequest.setTableName(tableName);
    DescribeTableResult describeTableResult = client.describeTable(describeTableRequest);
    String status = describeTableResult.getTable().getTableStatus();
    return TableStatus.fromValue(status);
}