在dynamoDb中查询全局二级索引
Query global secondary index in dynamoDb
我正在尝试根据全局二级索引检查我的 table 上是否有任何具有该值的项目。我是 dynamo 的新手,我正在使用 java 和 AWS SDK v2。我不确定是否要执行查询,我必须只使用索引名称查询它,然后同时使用分区键和排序键。
public Integer checkIfExists(String serialNumber) {
String tableName = System.getenv("TABLE_NAME");
String partitionKeyName="GSI4PK";
String partitionKeyVal="GSI4SK";
String partitionAlias="#a";
// Set up an alias for the partition key name in case it's a reserved word
HashMap<String, String> attrNameAlias = new HashMap<String, String>();
attrNameAlias.put(partitionAlias, partitionKeyName);
// Set up mapping of the partition name with the value
HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
attrValues.put(":val1", AttributeValue.builder().s(serialNumber).build());
QueryRequest queryReq = QueryRequest.builder().tableName(tableName)
.keyConditionExpression(partitionKeyName + " = :val1").
expressionAttributeValues(attrValues).indexName("CertificateDirect").build();
try {
QueryResponse response = dynamoDbClient.query(queryReq);
System.out.println(response.toString());
System.out.println(response.count());
return response.count();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return -1;
}
我还看到有一个名为 partitionAlias 的值,我不确定它的作用
这是使用 java 和 AWS SDK v2
进行查询的方式
private Map<String, AttributeValue> getDynamoQuery(String partitionKeyName, String sortingKeyName,
String partitionKey, String sortingKey, String tableName, String indexName) {
HashMap<String, AttributeValue> attrValues = new HashMap<>();
attrValues.put(":val1", AttributeValue.builder().s(partitionKey).build());
attrValues.put(":val2", AttributeValue.builder().s(sortingKey).build());
QueryRequest queryReq = QueryRequest.builder().tableName(tableName).indexName(indexName)
.keyConditionExpression(partitionKeyName + " = :val1 and " + sortingKeyName + " = :val2")
.expressionAttributeValues(attrValues).build();
QueryResponse response = dynamoDbClient.query(queryReq);
return response.items().get(0);
}
我正在尝试根据全局二级索引检查我的 table 上是否有任何具有该值的项目。我是 dynamo 的新手,我正在使用 java 和 AWS SDK v2。我不确定是否要执行查询,我必须只使用索引名称查询它,然后同时使用分区键和排序键。
public Integer checkIfExists(String serialNumber) {
String tableName = System.getenv("TABLE_NAME");
String partitionKeyName="GSI4PK";
String partitionKeyVal="GSI4SK";
String partitionAlias="#a";
// Set up an alias for the partition key name in case it's a reserved word
HashMap<String, String> attrNameAlias = new HashMap<String, String>();
attrNameAlias.put(partitionAlias, partitionKeyName);
// Set up mapping of the partition name with the value
HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
attrValues.put(":val1", AttributeValue.builder().s(serialNumber).build());
QueryRequest queryReq = QueryRequest.builder().tableName(tableName)
.keyConditionExpression(partitionKeyName + " = :val1").
expressionAttributeValues(attrValues).indexName("CertificateDirect").build();
try {
QueryResponse response = dynamoDbClient.query(queryReq);
System.out.println(response.toString());
System.out.println(response.count());
return response.count();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return -1;
}
我还看到有一个名为 partitionAlias 的值,我不确定它的作用
这是使用 java 和 AWS SDK v2
进行查询的方式private Map<String, AttributeValue> getDynamoQuery(String partitionKeyName, String sortingKeyName,
String partitionKey, String sortingKey, String tableName, String indexName) {
HashMap<String, AttributeValue> attrValues = new HashMap<>();
attrValues.put(":val1", AttributeValue.builder().s(partitionKey).build());
attrValues.put(":val2", AttributeValue.builder().s(sortingKey).build());
QueryRequest queryReq = QueryRequest.builder().tableName(tableName).indexName(indexName)
.keyConditionExpression(partitionKeyName + " = :val1 and " + sortingKeyName + " = :val2")
.expressionAttributeValues(attrValues).build();
QueryResponse response = dynamoDbClient.query(queryReq);
return response.items().get(0);
}