Example usage for com.amazonaws.services.dynamodbv2.model AttributeDefinition getAttributeType

List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeDefinition getAttributeType

Introduction

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

Prototype


public String getAttributeType() 

Source Link

Document

The data type for the attribute, where:

  • S - the attribute is of type String

  • N - the attribute is of type Number

  • B - the attribute is of type Binary

Usage

From source file:DynamoDBUtils.java

License:Open Source License

/**
 * Verifies if the table has the expected schema.
 *
 * @param client/*  w  w  w .  ja va  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: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 ww. j  a  v a  2 s .  co 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. j  a v  a2s .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;
    }//from w  w  w  .j a va 2s. com
    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 2s . c om*/
    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: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 {//  w  w w  .ja  va 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:org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler.java

License:Open Source License

void checkTableSchemaType(TableDescription tableDescription, Table table) throws MetaException {
    List<FieldSchema> tableSchema = table.getSd().getCols();

    for (FieldSchema fieldSchema : tableSchema) {
        for (AttributeDefinition definition : tableDescription.getAttributeDefinitions()) {
            validateKeySchema(definition.getAttributeName(), definition.getAttributeType(), fieldSchema);
        }/* w ww.j  a va2 s  . c  om*/

        // Check for each field type
        if (HiveDynamoDBTypeFactory.getTypeObjectFromHiveType(fieldSchema.getType()) == null) {
            throw new MetaException(
                    "The hive type " + fieldSchema.getType() + " is not supported in " + "DynamoDB");
        }
    }
}

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);
    }//  w w w  .  j a v  a  2s.  com

    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.xmlsh.aws.util.AWSDDBCommand.java

License:BSD License

private void writeAttributeDefinition(AttributeDefinition def) throws XMLStreamException {
    startElement("attribute-definition");
    attribute("name", def.getAttributeName());
    attribute("type", def.getAttributeType());
    endElement();/*w  ww .java  2  s .c  o m*/
}