0
votes

I am new to DynamoDB and wanted to know how can we query on a table in DynamoDB by using ONLY partition key in JAVA

I have table called "ervive-pdi-data-invalid-qa" and it's Schema is :

  1. partition key is "SubmissionId"
  2. Sort key is "Id".
  3. City (Attribute)
  4. Errors (Attribute)

The table looks like this: Table

I want to retrieve the sort key value and remaining attributes data by using Partition key using (software.amazon.awssdk) new version of AWS SDK DynamoDB classes.

is it possible to get it? If so, can any one post the answers?

Have tried this:

       DynamoDbClient ddb = 
           DynamoDbClient.builder().region(Region.US_EAST_1).build();
             DynamoDbEnhancedClient enhancedClient = 
                DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        //Define table
        DynamoDbTable<ErvivePdiDataInvalidQa> table = 
           enhancedClient.table("ervive-pdi-data-invalid-qa",
                  TableSchema.fromBean(ErvivePdiDataInvalidQa.class));

        Key key = Key.builder().partitionValue(2023).build();
        ErvivePdiDataInvalidQa result = table.getItem(r->r.key(key));
        System.out.println("The record id is "+result.getId());

ErvivePdiDataInvalidQa table class is in below comment*

and it is returning "The provided key element does not match the schema (Service: DynamoDb, Status Code: 400, Request ID: PE1MKPMQ9MLT51OLJQVDCURQGBVV4KQNSO5AEMVJF66Q9ASUAAJG, Extended Request ID: null)"

2
It is possible, just try to create a query includes both Partition key and Range key, then remove Range key condition part.hoangdv

2 Answers

0
votes

Query you need is documented in one of the examples of AWS Dynamodb Query API for Java.

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2).build();
DynamoDB dynamoDB = new DynamoDB(client);

Table table = dynamoDB.getTable("ervive-pdi-data-invalid-qa");

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("SubmissionId = :v_id")
    .withValueMap(new ValueMap()
        .withInt(":v_id", 2146));

ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
    item = iterator.next();
    System.out.println(item.toJSONPretty());
}

A single Query operation can retrieve a maximum of 1 MB of data, see documentation

0
votes

I have been working with Padma on this issue. We first tried A. Khan's code but could not get passed authentication with v1. Instead we got "WARNING: Your profile name includes a 'profile ' prefix. This is considered part of the profile name in the Java SDK, so you will need to include this prefix in your profile name when you reference this profile from your Java code." ultimately it could not get the credentials. Our credentials assume IAM roles in .aws/config-i2 file. It works fine in v2 but not v1.

So then we tried v2 of the SDK and have no problems with connecting but we get NULL returned on trying to fetch all records from the table.

In all of the below attempts using v2 of SDK, table data returns NULL

We created this table class

package data;

import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;

@DynamoDbBean
public class ErvivePdiDataInvalidQa {
    private int submissionId;
    private String id;
    private String address1;
    private String city;
    private String dateOfBirth;
    private String errors;
    private String firstName;
    private String firstNameNormalized;
    private String gender;
    private String lastName;
    private String lastNameNormalized;
    private String middleNameInitial;
    private String postalCode;
    private String rowNumber;
    private String state;
    private String submissionType;

    @DynamoDbPartitionKey
    public int getSubmissionId() {
        return submissionId;
    }

    public void setSubmissionId(int submissionId) {
        this.submissionId = submissionId;
    }

    @DynamoDbSortKey
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String Address1) {
        this.address1 = Address1;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getErrors() {
        return errors;
    }

    public void setErrors(String errors) {
        this.errors = errors;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstNameNormalized() {
        return firstNameNormalized;
    }

    public void setFirstNameNormalized(String firstNameNormalized) {
        this.firstNameNormalized = firstNameNormalized;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastNameNormalized() {
        return lastNameNormalized;
    }

    public void setLastNameNormalized(String lastNameNormalized) {
        this.lastNameNormalized = lastNameNormalized;
    }

    public String getMiddleNameInitial() {
        return middleNameInitial;
    }

    public void setMiddleNameInitial(String middleNameInitial) {
        this.middleNameInitial = middleNameInitial;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getRowNumber() {
        return rowNumber;
    }

    public void setRowNumber(String rowNumber) {
        this.rowNumber = rowNumber;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getSubmissionType() {
        return submissionType;
    }

    public void setSubmissionType(String submissionType) {
        this.submissionType = submissionType;
    }
}

DynamoDB code to get all records

//Connection
        DynamoDbClient ddb = DynamoDbClient.builder().build();
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        //Define table
        DynamoDbTable<ErvivePdiDataInvalidQa> table = enhancedClient.table("ervive-pdi-data-invalid-qa", TableSchema.fromBean(ErvivePdiDataInvalidQa.class));

        //Get All Items from table - RETURNING NULL
        Iterator<ErvivePdiDataInvalidQa> results = table.scan().items().iterator();
        while (results.hasNext()) {

            ErvivePdiDataInvalidQa rec = results.next();
            System.out.println("The record id is "+rec.getId());
        }

Also tried:

DynamoDB code to filter by SubmissionID

AttributeValue attr = AttributeValue.builder()
                .n("1175")
                .build();

        // Get only Open items in the Work table
        Map<String, AttributeValue> myMap = new HashMap<>();
        myMap.put(":val1", attr);

        Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#sid", "SubmissionId");

        // Set the Expression so only Closed items are queried from the Work table
        Expression expression = Expression.builder()
                .expressionValues(myMap)
                .expressionNames(myExMap)
                .expression("#sid = :val1")
                .build();

        ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder()
                .filterExpression(expression)
                .limit(15)
                .build();

        // Get items in the Record table and write out the ID value
        Iterator<ErvivePdiDataInvalidQa> results = table.scan(enhancedRequest).items().iterator();

        while (results.hasNext()) {

            ErvivePdiDataInvalidQa record = results.next();
            System.out.println("The record id is " + record.getId());
        }