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

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

Introduction

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

Prototype


public java.util.List<KeySchemaElement> getKeySchema() 

Source Link

Document

The primary key structure for the table.

Usage

From source file:DynamoDBUtils.java

License:Open Source License

/**
 * Verifies if the table has the expected schema.
 *
 * @param client// w  ww . ja  v  a  2  s .co  m
 *        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;

}

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.//w w w.ja  va  2s .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.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;
    }//from  www.  j  av  a  2s .c  o 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.services.awsrum.utils.DynamoDBUtils.java

License:Open Source License

private static boolean tableHasCorrectSchema(AmazonDynamoDBClient client, String tableName, String key,
        String rangeKey) {/*  w  w w.  j a v  a 2 s.co  m*/
    DescribeTableRequest describeTableRequest = new DescribeTableRequest();
    describeTableRequest.setTableName(tableName);
    DescribeTableResult describeTableResult = client.describeTable(describeTableRequest);
    TableDescription tableDescription = describeTableResult.getTable();
    if (tableDescription.getAttributeDefinitions().size() != 2) {
        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())) {
        if (!attributeDefinition.getAttributeName().equals(rangeKey)
                || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.N.toString())) {
            LOG.error("Attribute name or type does not match existing table.");
            return false;
        }
    }
    if (!attributeDefinition.getAttributeName().equals(rangeKey)
            || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.N.toString())) {
        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() != 2) {
        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;
    }
    KeySchemaElement seconndKse = KSEs.get(1);
    if (!seconndKse.getAttributeName().equals(rangeKey)
            || !seconndKse.getKeyType().equals(KeyType.RANGE.toString())) {
        LOG.error("The hash range key does not match the existing table.");
        return false;
    }
    return true;

}

From source file:com.facebook.presto.dynamodb.DynamodbClient.java

License:Apache License

public DynamodbTable getTable(String tableName) {
    TableDescription table = dynamoDBClient.describeTable(tableName).getTable();
    List<DynamodbColumn> columns = table.getAttributeDefinitions().stream()
            .map(e -> new DynamodbColumn(e.getAttributeName(), getType(e.getAttributeType())))
            .collect(Collectors.toList());

    Optional<String> hashKey = table.getKeySchema().stream().filter(e -> e.getKeyType().equals("HASH"))
            .map(e -> e.getAttributeName()).findAny();
    Optional<String> rangeKey = table.getKeySchema().stream().filter(e -> e.getKeyType().equals("RANGE"))
            .map(e -> e.getAttributeName()).findAny();

    return new DynamodbTable(tableName, hashKey, rangeKey, columns);
}

From source file:org.apache.beam.sdk.io.aws.dynamodb.DynamoDBIOTestHelper.java

License:Apache License

static void createTestTable(String tableName) {
    CreateTableResult res = createDynamoTable(tableName);

    TableDescription tableDesc = res.getTableDescription();

    Assert.assertEquals(tableName, tableDesc.getTableName());
    Assert.assertTrue(tableDesc.getKeySchema().toString().contains(ATTR_NAME_1));
    Assert.assertTrue(tableDesc.getKeySchema().toString().contains(ATTR_NAME_2));

    Assert.assertEquals(tableDesc.getProvisionedThroughput().getReadCapacityUnits(), Long.valueOf(1000));
    Assert.assertEquals(tableDesc.getProvisionedThroughput().getWriteCapacityUnits(), Long.valueOf(1000));
    Assert.assertEquals("ACTIVE", tableDesc.getTableStatus());
    Assert.assertEquals("arn:aws:dynamodb:us-east-1:000000000000:table/" + tableName, tableDesc.getTableArn());

    ListTablesResult tables = dynamoDBClient.listTables();
    Assert.assertEquals(1, tables.getTableNames().size());
}

From source file:org.apache.metamodel.dynamodb.DynamoDbDataContext.java

License:Apache License

@Override
protected Schema getMainSchema() throws MetaModelException {
    final Map<String, SimpleTableDef> tableDefs = new HashMap<>();
    for (final SimpleTableDef tableDef : _tableDefs) {
        tableDefs.put(tableDef.getName(), tableDef);
    }//from w  w w  .jav  a  2 s  .c  o m

    final MutableSchema schema = new MutableSchema(getMainSchemaName());
    final ListTablesResult tables = _dynamoDb.listTables();
    final List<String> tableNames = tables.getTableNames();
    for (final String tableName : tableNames) {
        final MutableTable table = new MutableTable(tableName, schema);
        schema.addTable(table);

        final DescribeTableResult descripeTableResult = _dynamoDb.describeTable(tableName);
        final TableDescription tableDescription = descripeTableResult.getTable();

        // add primary keys
        addColumnFromKeySchema("Primary index", tableDescription.getKeySchema(), table, true);

        // add attributes from global and local indices
        final List<GlobalSecondaryIndexDescription> globalSecondaryIndexes = tableDescription
                .getGlobalSecondaryIndexes();
        if (globalSecondaryIndexes != null) {
            for (final GlobalSecondaryIndexDescription globalSecondaryIndex : globalSecondaryIndexes) {
                addColumnFromKeySchema(globalSecondaryIndex.getIndexName(), globalSecondaryIndex.getKeySchema(),
                        table, false);
            }
        }
        final List<LocalSecondaryIndexDescription> localSecondaryIndexes = tableDescription
                .getLocalSecondaryIndexes();
        if (localSecondaryIndexes != null) {
            for (final LocalSecondaryIndexDescription localSecondaryIndex : localSecondaryIndexes) {
                addColumnFromKeySchema(localSecondaryIndex.getIndexName(), localSecondaryIndex.getKeySchema(),
                        table, false);
            }
        }

        // add top-level attribute definitions
        final List<AttributeDefinition> attributeDefinitions = tableDescription.getAttributeDefinitions();
        for (final AttributeDefinition attributeDefinition : attributeDefinitions) {
            final String attributeName = attributeDefinition.getAttributeName();
            MutableColumn column = (MutableColumn) table.getColumnByName(attributeName);
            if (column == null) {
                column = new MutableColumn(attributeName, table);
                table.addColumn(column);
            }
            final String attributeType = attributeDefinition.getAttributeType();
            column.setType(DynamoDbUtils.toColumnType(attributeName, attributeType));
            column.setIndexed(true);
            column.setNativeType(attributeType);
        }

        // add additional metadata from SimpleTableDefs if available
        final SimpleTableDef tableDef = tableDefs.get(tableName);
        if (tableDef != null) {
            final String[] columnNames = tableDef.getColumnNames();
            final ColumnType[] columnTypes = tableDef.getColumnTypes();
            for (int i = 0; i < columnNames.length; i++) {
                final String columnName = columnNames[i];
                final ColumnType columnType = columnTypes[i];
                MutableColumn column = (MutableColumn) table.getColumnByName(columnName);
                if (column == null) {
                    column = new MutableColumn(columnName, table);
                    table.addColumn(column);
                }
                if (column.getType() == null && columnType != null) {
                    column.setType(columnType);
                }
            }
        }

        // add additional attributes based on global and local indices
        if (globalSecondaryIndexes != null) {
            for (final GlobalSecondaryIndexDescription globalSecondaryIndex : globalSecondaryIndexes) {
                final List<String> nonKeyAttributes = globalSecondaryIndex.getProjection()
                        .getNonKeyAttributes();
                for (final String attributeName : nonKeyAttributes) {
                    addColumnFromNonKeyAttribute(globalSecondaryIndex.getIndexName(), table, attributeName);
                }
            }
        }
        if (localSecondaryIndexes != null) {
            for (final LocalSecondaryIndexDescription localSecondaryIndex : localSecondaryIndexes) {
                final List<String> nonKeyAttributes = localSecondaryIndex.getProjection().getNonKeyAttributes();
                for (final String attributeName : nonKeyAttributes) {
                    addColumnFromNonKeyAttribute(localSecondaryIndex.getIndexName(), table, attributeName);
                }
            }
        }
    }
    return schema;
}

From source file:org.iternine.jeppetto.dao.dynamodb.DynamoDBQueryModelDAO.java

License:Apache License

@SuppressWarnings({ "unchecked", "UnusedParameters" })
protected DynamoDBQueryModelDAO(Class<T> entityClass, Map<String, Object> daoProperties,
        AccessControlContextProvider accessControlContextProvider) {
    this.entityClass = entityClass;
    this.dynamoDB = (AmazonDynamoDB) daoProperties.get("db");
    this.tableName = daoProperties.containsKey("tableName") ? (String) daoProperties.get("tableName")
            : entityClass.getSimpleName();
    this.idGenerator = (IdGenerator<ID>) daoProperties.get("idGenerator");
    this.consistentRead = Boolean.parseBoolean((String) daoProperties.get("consistentRead")); // null okay - defaults to false
    this.optimisticLockField = (String) daoProperties.get("optimisticLockField");
    this.enableScans = Boolean.parseBoolean((String) daoProperties.get("enableScans")); // null okay - defaults to false

    TableDescription tableDescription = dynamoDB.describeTable(tableName).getTable();

    Pair<String, String> primaryKeyAttributeNames = getKeyAttributeNames(tableDescription.getKeySchema());
    this.hashKeyField = primaryKeyAttributeNames.getFirst();
    this.rangeKeyField = primaryKeyAttributeNames.getSecond();

    ProjectionExpressionBuilder projectionExpressionBuilder;
    if (Boolean.parseBoolean((String) daoProperties.get("projectionObject"))) { // null okay - defaults to false
        projectionExpressionBuilder = new ProjectionExpressionBuilder(entityClass, hashKeyField, rangeKeyField,
                optimisticLockField);/*from ww  w. ja  v  a 2 s  .c  o m*/
        this.projectionExpression = projectionExpressionBuilder.getExpression();
        this.projectionExpressionNames = projectionExpressionBuilder.getExpressionAttributeNames();
    } else {
        projectionExpressionBuilder = null;
        this.projectionExpression = null;
        this.projectionExpressionNames = Collections.emptyMap();
    }

    List<String> keyFields = rangeKeyField == null ? Collections.singletonList(hashKeyField)
            : Arrays.asList(hashKeyField, rangeKeyField);
    IndexData baseIndexData = new IndexData(null, keyFields, true);
    this.baseIndexOnly = Collections.singletonMap(hashKeyField,
            Collections.singletonMap(rangeKeyField, baseIndexData));
    this.indexes = processIndexes(tableDescription, projectionExpressionBuilder, baseIndexData);
    this.persistableEnhancer = EnhancerHelper.getPersistableEnhancer(entityClass);

    String updateObjectClassName = (String) daoProperties.get("updateObject");
    if (updateObjectClassName == null) {
        this.updateObjectEnhancer = EnhancerHelper.getUpdateObjectEnhancer(entityClass);
    } else {
        try {
            Class updateObjectClass = Class.forName(updateObjectClassName);

            if (!entityClass.isAssignableFrom(updateObjectClass)) {
                throw new JeppettoException(
                        String.format("Invalid UpdateObject type. %s does not subclass entity type %s",
                                updateObjectClassName, entityClass.getName()));
            }

            this.updateObjectEnhancer = (Enhancer<? extends T>) EnhancerHelper
                    .getUpdateObjectEnhancer(updateObjectClass);
        } catch (ClassNotFoundException e) {
            throw new JeppettoException(e);
        }
    }

    if (Boolean.parseBoolean((String) daoProperties.get("verifyUniqueIds"))) { // null okay - defaults to false
        ConditionExpressionBuilder conditionExpressionBuilder = new ConditionExpressionBuilder()
                .with(hashKeyField, new DynamoDBConstraint(DynamoDBOperator.IsNull));

        if (rangeKeyField != null) {
            conditionExpressionBuilder.with(rangeKeyField, new DynamoDBConstraint(DynamoDBOperator.IsNull));
        }

        this.uniqueIdConditionExpression = conditionExpressionBuilder.getExpression(); // No attribute values needed
    } else {
        this.uniqueIdConditionExpression = null;
    }
}

From source file:org.socialsignin.spring.data.dynamodb.repository.util.Entity2DynamoDBTableSynchronizer.java

License:Apache License

/**
 * @param entityInformation/*from w w w . j a v a 2  s.  c  o m*/
 *            The entity to check for it's table
 * @throws IllegalStateException
 *             is thrown if the existing table doesn't match the entity's
 *             annotation
 */
private DescribeTableResult performValidate(DynamoDBEntityInformation<T, ID> entityInformation)
        throws IllegalStateException {
    Class<T> domainType = entityInformation.getJavaType();

    CreateTableRequest expected = mapper.generateCreateTableRequest(domainType);
    DescribeTableResult result = amazonDynamoDB.describeTable(expected.getTableName());
    TableDescription actual = result.getTable();

    if (!expected.getKeySchema().equals(actual.getKeySchema())) {
        throw new IllegalStateException("KeySchema is not as expected. Expected: <" + expected.getKeySchema()
                + "> but found <" + actual.getKeySchema() + ">");
    }
    LOGGER.debug("KeySchema is valid");

    if (expected.getGlobalSecondaryIndexes() != null) {
        if (!Arrays.deepEquals(expected.getGlobalSecondaryIndexes().toArray(),
                actual.getGlobalSecondaryIndexes().toArray())) {
            throw new IllegalStateException("Global Secondary Indexes are not as expected. Expected: <"
                    + expected.getGlobalSecondaryIndexes() + "> but found <"
                    + actual.getGlobalSecondaryIndexes() + ">");
        }
    }
    LOGGER.debug("Global Secondary Indexes are valid");

    LOGGER.info("Validated table {} for entity{}", expected.getTableName(), domainType);
    return result;
}

From source file:org.xmlsh.aws.util.AWSDDBCommand.java

License:BSD License

protected void writeTableDescription(TableDescription tableDescription) throws XMLStreamException {
    startElement("table");
    attribute("name", tableDescription.getTableName());
    attribute("status", tableDescription.getTableStatus());
    attribute("create-date", Util.formatXSDateTime(tableDescription.getCreationDateTime()));
    attribute("item-count", tableDescription.getItemCount());
    attribute("size", tableDescription.getTableSizeBytes());
    attribute("item-count", tableDescription.getItemCount());

    writeAttributeDefinitions(tableDescription.getAttributeDefinitions());
    writeKeySchemaList(tableDescription.getKeySchema());
    writeLocalSecondaryIndexes(tableDescription.getLocalSecondaryIndexes());
    writeGlobalSecondaryIndexes(tableDescription.getGlobalSecondaryIndexes());
    writeProvisionedThroughput(tableDescription.getProvisionedThroughput());

}