Search in sources :

Example 1 with GlobalSecondaryIndexDescription

use of com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription in project aws-sdk-android by aws-amplify.

the class Search method isKeyAttribute.

private static boolean isKeyAttribute(Table table, String indexName, String attributeName) {
    GlobalSecondaryIndexDescription gsi = null;
    final LocalSecondaryIndexDescription lsi;
    if (StringUtils.isBlank(indexName)) {
        return table.getKeys().containsKey(attributeName);
    } else if (table.getGlobalSecondaryIndexes().get(indexName) != null) {
        gsi = table.getGlobalSecondaryIndexes().get(indexName);
        for (final KeySchemaElement element : gsi.getKeySchema()) {
            return isKeyAttribute(element, attributeName);
        }
    } else if (table.getLocalSecondaryIndexes().get(indexName) != null) {
        lsi = table.getLocalSecondaryIndexes().get(indexName);
        for (final KeySchemaElement element : lsi.getKeySchema()) {
            return isKeyAttribute(element, attributeName);
        }
    } else {
        throw new IllegalStateException(String.format("Unable to locate index %s on table %s", indexName, table.getTableName()));
    }
    return false;
}
Also used : GlobalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription) LocalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 2 with GlobalSecondaryIndexDescription

use of com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription in project aws-sdk-android by aws-amplify.

the class Table method loadTableDescription.

/**
 * Load table description.
 */
public void loadTableDescription() {
    clear();
    final DescribeTableRequest request = Table.appendDynamoDBDocumentUserAgentString(new DescribeTableRequest(this.tableName));
    final DescribeTableResult result = client.describeTable(request);
    this.tableDescription = result.getTable();
    for (final KeySchemaElement element : this.tableDescription.getKeySchema()) {
        final String keyName = element.getAttributeName();
        for (final AttributeDefinition ad : this.tableDescription.getAttributeDefinitions()) {
            if (ad.getAttributeName().equals(keyName)) {
                final KeyDescription kd = new KeyDescription();
                kd.setHash("HASH".equalsIgnoreCase(element.getKeyType()));
                kd.setType(getType(ad.getAttributeType()));
                if (kd.isHash()) {
                    this.hashKeys.add(keyName);
                } else {
                    this.rangeKeys.add(keyName);
                }
                this.keys.put(keyName, kd);
                break;
            }
        }
    }
    if (this.tableDescription.getLocalSecondaryIndexes() != null) {
        for (final LocalSecondaryIndexDescription index : this.tableDescription.getLocalSecondaryIndexes()) {
            this.localSecondaryIndexes.put(index.getIndexName(), index);
            this.localSecondaryIndexNames.add(index.getIndexName());
        }
    }
    if (this.tableDescription.getGlobalSecondaryIndexes() != null) {
        for (final GlobalSecondaryIndexDescription index : this.tableDescription.getGlobalSecondaryIndexes()) {
            this.globalSecondaryIndexes.put(index.getIndexName(), index);
            this.globalSecondaryIndexNames.add(index.getIndexName());
        }
    }
    for (final AttributeDefinition ad : this.tableDescription.getAttributeDefinitions()) {
        this.attributes.add(ad);
    }
}
Also used : AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) GlobalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult) KeyDescription(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) LocalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription)

Example 3 with GlobalSecondaryIndexDescription

use of com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription in project aws-athena-query-federation by awslabs.

the class DDBTableUtils method getTable.

/**
 * Fetches metadata for a DynamoDB table
 *
 * @param tableName the (case sensitive) table name
 * @param invoker the ThrottlingInvoker to call DDB with
 * @param ddbClient the DDB client to use
 * @return the table metadata
 */
public static DynamoDBTable getTable(String tableName, ThrottlingInvoker invoker, AmazonDynamoDB ddbClient) throws TimeoutException {
    DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
    TableDescription table = invoker.invoke(() -> ddbClient.describeTable(request).getTable());
    KeyNames keys = getKeys(table.getKeySchema());
    // get data statistics
    long approxTableSizeInBytes = table.getTableSizeBytes();
    long approxItemCount = table.getItemCount();
    final long provisionedReadCapacity = table.getProvisionedThroughput() != null ? table.getProvisionedThroughput().getReadCapacityUnits() : PSUEDO_CAPACITY_FOR_ON_DEMAND;
    // get secondary indexes
    List<LocalSecondaryIndexDescription> localSecondaryIndexes = table.getLocalSecondaryIndexes() != null ? table.getLocalSecondaryIndexes() : ImmutableList.of();
    List<GlobalSecondaryIndexDescription> globalSecondaryIndexes = table.getGlobalSecondaryIndexes() != null ? table.getGlobalSecondaryIndexes() : ImmutableList.of();
    ImmutableList.Builder<DynamoDBIndex> indices = ImmutableList.builder();
    localSecondaryIndexes.forEach(i -> {
        KeyNames indexKeys = getKeys(i.getKeySchema());
        // DynamoDB automatically fetches all attributes from the table for local secondary index, so ignore projected attributes
        indices.add(new DynamoDBIndex(i.getIndexName(), indexKeys.getHashKey(), indexKeys.getRangeKey(), ProjectionType.ALL, ImmutableList.of()));
    });
    globalSecondaryIndexes.stream().filter(i -> IndexStatus.fromValue(i.getIndexStatus()).equals(IndexStatus.ACTIVE)).forEach(i -> {
        KeyNames indexKeys = getKeys(i.getKeySchema());
        indices.add(new DynamoDBIndex(i.getIndexName(), indexKeys.getHashKey(), indexKeys.getRangeKey(), ProjectionType.fromValue(i.getProjection().getProjectionType()), i.getProjection().getNonKeyAttributes() == null ? ImmutableList.of() : i.getProjection().getNonKeyAttributes()));
    });
    return new DynamoDBTable(tableName, keys.getHashKey(), keys.getRangeKey(), table.getAttributeDefinitions(), indices.build(), approxTableSizeInBytes, approxItemCount, provisionedReadCapacity);
}
Also used : Schema(org.apache.arrow.vector.types.pojo.Schema) ThrottlingInvoker(com.amazonaws.athena.connector.lambda.ThrottlingInvoker) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) KeyType(com.amazonaws.services.dynamodbv2.model.KeyType) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) IndexStatus(com.amazonaws.services.dynamodbv2.model.IndexStatus) HashSet(java.util.HashSet) LocalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) ImmutableList(com.google.common.collect.ImmutableList) SchemaBuilder(com.amazonaws.athena.connector.lambda.data.SchemaBuilder) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Map(java.util.Map) ItemUtils(com.amazonaws.services.dynamodbv2.document.ItemUtils) DynamoDBTable(com.amazonaws.athena.connectors.dynamodb.model.DynamoDBTable) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) GlobalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription) ProjectionType(com.amazonaws.services.dynamodbv2.model.ProjectionType) Logger(org.slf4j.Logger) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) Set(java.util.Set) Field(org.apache.arrow.vector.types.pojo.Field) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDBIndex(com.amazonaws.athena.connectors.dynamodb.model.DynamoDBIndex) List(java.util.List) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) DynamoDBIndex(com.amazonaws.athena.connectors.dynamodb.model.DynamoDBIndex) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) GlobalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription) LocalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription) DynamoDBTable(com.amazonaws.athena.connectors.dynamodb.model.DynamoDBTable)

Aggregations

GlobalSecondaryIndexDescription (com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription)3 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)3 LocalSecondaryIndexDescription (com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription)3 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)2 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)2 ThrottlingInvoker (com.amazonaws.athena.connector.lambda.ThrottlingInvoker)1 SchemaBuilder (com.amazonaws.athena.connector.lambda.data.SchemaBuilder)1 DynamoDBIndex (com.amazonaws.athena.connectors.dynamodb.model.DynamoDBIndex)1 DynamoDBTable (com.amazonaws.athena.connectors.dynamodb.model.DynamoDBTable)1 KeyDescription (com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription)1 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)1 ItemUtils (com.amazonaws.services.dynamodbv2.document.ItemUtils)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)1 IndexStatus (com.amazonaws.services.dynamodbv2.model.IndexStatus)1 KeyType (com.amazonaws.services.dynamodbv2.model.KeyType)1 ProjectionType (com.amazonaws.services.dynamodbv2.model.ProjectionType)1 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)1 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)1 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)1