aws / aws-sdk-java-v2

The official AWS SDK for Java - Version 2
Apache License 2.0
2.2k stars 847 forks source link

V2 example for the Global Security Index #3841

Open shahcoding opened 1 year ago

shahcoding commented 1 year ago

Describe the issue

Please add the example for Global security Index related APIs for the v2 aws SDK in java

If it is already available, please share.

Links

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSIJavaDocumentAPI.Example.html

shahcoding commented 1 year ago

If someone is looking, a sample to query by GSI ` import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.QueryRequest; import software.amazon.awssdk.services.dynamodb.model.QueryResponse;

import java.util.HashMap; import java.util.Map;

public class QueryGSIExample {

public static void main(String[] args) {
    DynamoDbClient client = DynamoDbClient.create();

    String gsiName = "your-gsi-name";
    String gsiPartitionKeyName = "your-gsi-partition-key-name";
    String gsiPartitionKeyValue = "your-gsi-partition-key-value";

    Map<String, AttributeValue> valueMap = new HashMap<>();
    valueMap.put(":partitionValue", AttributeValue.builder().s(gsiPartitionKeyValue).build());

    QueryRequest queryRequest = QueryRequest.builder()
            .tableName("your-table-name")
            .indexName(gsiName)
            .keyConditionExpression(gsiPartitionKeyName + " = :partitionValue")
            .expressionAttributeValues(valueMap)
            .build();

    QueryResponse queryResponse = client.query(queryRequest);
    System.out.println("Query result: " + queryResponse.items());
}

}

`

debora-ito commented 1 year ago

HI @shahcoding good call out.

We do have a query example in the aws-doc-sdk-examples DynamoDB folder, but it does not show how to query an index.

And we are aware of the Java v1 examples that are present in the service specific developer guides (like DynamoDB, S3), we need to work with the Documentation team to add Java v2 examples.