📅  最后修改于: 2023-12-03 15:00:33.522000             🧑  作者: Mango
DynamoDB 是亚马逊提供的一种高性能 NoSQL 数据库,具有高可靠性、高可扩展性、低延迟和自动伸缩等特点。DynamoDB 全局二级索引是 DynamoDB 的一个特性,可以让您在 DynamoDB 表中创建和查询全局二级索引。
DynamoDB 表的主键分为分区键和排序键,分区键用于分区和负载均衡,排序键用于存储和查询数据。在 DynamoDB 表中,创建全局二级索引可以帮助我们更灵活、方便地查询数据。
一个全局二级索引包含一个分区键和一个排序键,它可以独立于主键进行查询,也可以使用主键查询。全局二级索引可以在 DynamoDB 表创建之前或之后创建。
在 DynamoDB 表中,创建全局二级索引需要填写以下信息:
下面是使用 AWS SDK for Java 创建全局二级索引的示例代码:
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
String tableName = "myTableName";
String indexName = "myIndexName";
String partitionKey = "myPartitionKey";
String sortKey = "mySortKey";
List<AttributeDefinition> attributes = client
.describeTable(tableName)
.getTable()
.getAttributeDefinitions();
boolean partitionKeyExists = attributes.stream()
.anyMatch(attr -> attr.getAttributeName().equals(partitionKey));
if (!partitionKeyExists) {
throw new IllegalArgumentException("Partition key not found in table attributes");
}
List<KeySchemaElement> keySchemas = new ArrayList<>();
keySchemas.add(new KeySchemaElement()
.withAttributeName(partitionKey)
.withKeyType(KeyType.HASH));
boolean sortKeyExists = attributes.stream()
.anyMatch(attr -> attr.getAttributeName().equals(sortKey));
if (sortKeyExists) {
keySchemas.add(new KeySchemaElement()
.withAttributeName(sortKey)
.withKeyType(KeyType.RANGE));
}
GlobalSecondaryIndex index = new GlobalSecondaryIndex()
.withIndexName(indexName)
.withKeySchema(keySchemas)
.withProjection(new Projection().withProjectionType(ProjectionType.ALL))
.withProvisionedThroughput(new ProvisionedThroughput(10L, 10L));
CreateGlobalSecondaryIndexAction createIndexAction =
new CreateGlobalSecondaryIndexAction().withIndex(index);
UpdateTableRequest updateTableRequest = new UpdateTableRequest()
.withTableName(tableName)
.withAttributeDefinitions(attributes)
.withGlobalSecondaryIndexUpdates(Arrays.asList(createIndexAction));
client.updateTable(updateTableRequest);
全局二级索引查询支持 Query 和 Scan 操作。Query 操作需要提供分区键和排序键的条件表达式,Scan 操作仅需要提供分区键的条件表达式。
下面是使用 AWS SDK for Java 进行全局二级索引查询的示例代码:
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
String tableName = "myTableName";
String indexName = "myIndexName";
String partitionKey = "myPartitionKey";
String sortKey = "mySortKey";
QueryRequest queryRequest = new QueryRequest()
.withTableName(tableName)
.withIndexName(indexName)
.withKeyConditionExpression(partitionKey + "= :pkVal and " + sortKey + " > :skVal")
.withExpressionAttributeValues(Map.of(
":pkVal", new AttributeValue("myPartitionKeyValue"),
":skVal", new AttributeValue("mySortKeyValue")
));
QueryResult queryResult = client.query(queryRequest);
List<Map<String, AttributeValue>> items = queryResult.getItems();
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
String tableName = "myTableName";
String indexName = "myIndexName";
String partitionKey = "myPartitionKey";
ScanRequest scanRequest = new ScanRequest()
.withTableName(tableName)
.withIndexName(indexName)
.withFilterExpression(partitionKey + " = :pkVal")
.withExpressionAttributeValues(Map.of(
":pkVal", new AttributeValue("myPartitionKeyValue")
));
ScanResult scanResult = client.scan(scanRequest);
List<Map<String, AttributeValue>> items = scanResult.getItems();
DynamoDB 全局二级索引可以帮助我们更灵活、方便地查询数据。本文介绍了如何创建和查询全局二级索引,希望对您有所帮助。