Example usage for com.amazonaws.services.dynamodbv2.model KeySchemaElement getKeyType

List of usage examples for com.amazonaws.services.dynamodbv2.model KeySchemaElement getKeyType

Introduction

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

Prototype


public String getKeyType() 

Source Link

Document

The role that this key attribute will assume:

  • HASH - partition key

  • RANGE - sort key

The partition key of an item is also known as its hash attribute.

Usage

From source file:DynamoDBUtils.java

License:Open Source License

/**
 * Verifies if the table has the expected schema.
 *
 * @param client/*from   ww  w . j av  a  2s .c  om*/
 *        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./*from w  w w . ja v  a 2 s. c o m*/
 * 
 * @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;
    }// w ww .  ja  va  2 s. 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 a2s  .c  o 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.mortardata.pig.storage.DynamoDBStorage.java

License:Apache License

/**
 * HELPERS/*from   w w  w  .  ja  v  a 2  s .co m*/
 *
 * @throws IOException
 **/

private void checkPigSchemaForDynamo(ResourceSchema schema) throws IOException {
    // extract field names
    Set<String> fieldNames = Sets.newHashSetWithExpectedSize(schema.getFields().length);
    for (ResourceFieldSchema field : schema.getFields()) {
        String fieldName = field.getName();
        if (fieldNames.contains(fieldName)) {
            throw new IOException(
                    "Schema cannot contain duplicated field name. Found duplicated: " + fieldName);
        }
        if (field.getType() == DataType.MAP || field.getType() == DataType.TUPLE
                || field.getType() == DataType.BAG) {
            throw new IOException(
                    "DynamoDBStorage can not store map, tuple, or bag types.  Found one in field name: "
                            + fieldName);
        }
        fieldNames.add(fieldName);
    }

    // ensure that Dynamo table primary keys are found in field names
    DescribeTableResult describe = describeDynamoTable();
    List<KeySchemaElement> keySchemaElements = describe.getTable().getKeySchema();

    for (KeySchemaElement keySchemaElement : keySchemaElements) {
        String expectedFieldName = keySchemaElement.getAttributeName();

        if (KeyType.valueOf(keySchemaElement.getKeyType()) == KeyType.HASH) {
            if (!fieldNames.contains(expectedFieldName)) {
                throw new IOException("Dynamo table " + this.tableName + " hash primary key ["
                        + expectedFieldName + "] not found in " + " pig schema fields: " + fieldNames);
            }
        } else if (KeyType.valueOf(keySchemaElement.getKeyType()) == KeyType.RANGE) {
            if (!fieldNames.contains(expectedFieldName)) {
                throw new IOException("Dynamo table " + this.tableName + " range secondary key ["
                        + expectedFieldName + "] not found in " + " pig schema fields: " + fieldNames);
            }
        }
    }

}

From source file:io.fineo.drill.exec.store.dynamo.DrillDynamoTable.java

License:Apache License

public DrillDynamoTable(DynamoStoragePlugin plugin, String tableName, DynamoKeyMapperSpec keyMapper) {
    super(plugin, tableName, new DynamoGroupScanSpec());
    try {//from ww  w  . j a v  a 2 s. c o  m
        this.desc = plugin.getModel().getTable(tableName).waitForActive();
    } catch (InterruptedException e) {
        throw new DrillRuntimeException(e);
    }
    this.key = keyMapper;

    DynamoGroupScanSpec spec = ((DynamoGroupScanSpec) this.getSelection());
    // figure out the pk map
    List<KeySchemaElement> keys = desc.getKeySchema();
    List<AttributeDefinition> attributes = desc.getAttributeDefinitions();
    Map<String, DynamoTableDefinition.PrimaryKey> map = new HashMap<>();
    for (KeySchemaElement key : keys) {
        DynamoTableDefinition.PrimaryKey pk = new DynamoTableDefinition.PrimaryKey(key.getAttributeName(), null,
                KeyType.valueOf(key.getKeyType()) == KeyType.HASH);
        map.put(key.getAttributeName(), pk);
    }
    for (AttributeDefinition elem : attributes) {
        map.get(elem.getAttributeName()).setType(elem.getAttributeType());
    }
    List<DynamoTableDefinition.PrimaryKey> pks = newArrayList(map.values());
    DynamoTableDefinition def = new DynamoTableDefinition(tableName, pks, keyMapper);
    spec.setTable(def);
}

From source file:io.venable.amazonaws.dynamo.table.builder.TableBuilder.java

License:Apache License

private KeySchemaElement getHashKeySchemaElement(Collection<KeySchemaElement> keySchemaElementCollection) {
    for (KeySchemaElement keySchemaElement : keySchemaElementCollection) {
        if (KeyType.HASH.toString().equals(keySchemaElement.getKeyType()))
            return keySchemaElement;
    }/*from   www  .  jav  a  2 s  .  c o  m*/

    throw new IllegalStateException("The hash key was not found, but should have already been created.");
}

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

License:Apache License

private void addColumnFromKeySchema(String indexName, List<KeySchemaElement> keySchema, MutableTable table,
        boolean primaryKey) {
    for (final KeySchemaElement keySchemaElement : keySchema) {
        final String attributeName = keySchemaElement.getAttributeName();
        if (table.getColumnByName(attributeName) == null) {
            final String keyType = keySchemaElement.getKeyType();
            final MutableColumn column = new MutableColumn(attributeName, table).setPrimaryKey(primaryKey);
            appendRemarks(column, indexName + " member ('" + keyType + "' type)");
            table.addColumn(column);/*from  ww w .j  a  v  a 2  s  . c  o m*/
        }
    }
}

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

License:Apache License

private Pair<String, String> getKeyAttributeNames(List<KeySchemaElement> keySchema) {
    Pair<String, String> keyAttributes = new Pair<String, String>();

    for (KeySchemaElement keySchemaElement : keySchema) {
        if (keySchemaElement.getKeyType().equals(KeyType.HASH.name())) {
            keyAttributes.setFirst(keySchemaElement.getAttributeName());
        } else {/*from  www .j av  a2s. c  o  m*/
            keyAttributes.setSecond(keySchemaElement.getAttributeName());
        }
    }

    return keyAttributes;
}

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

License:BSD License

private void writeKeySchemaList(List<KeySchemaElement> keySchema) throws XMLStreamException {

    startElement("key-schema");
    for (KeySchemaElement key : keySchema) {
        startElement("key-schema-element");
        attribute("name", key.getAttributeName());
        attribute("type", key.getKeyType());
        endElement();// ww w.  j  a v a2s.  c om
    }

}