Dynamodb get query returns one of the entry fields null

-1

I have this data class

    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
    
    @DynamoDBTable(tableName = "AtcDevDocumentImages")
    public class ImageDetail {
    
      @DynamoDBHashKey(attributeName = "DocumentId")
      private String documentId;
    
      @DynamoDBRangeKey(attributeName = "TrackPosition")
      private int trackPosition;
    
      @DynamoDBAttribute(attributeName = "ImageId")
      private String imageId;
    
      @DynamoDBAttribute(attributeName = "ImageUrl")
      private String imageUrl;
    
      @DynamoDBAttribute(attributeName = "ImageName")
      private String imageName;
    
      @DynamoDBAttribute(attributeName = "Width")
      private double width;
    
      @DynamoDBAttribute(attributeName = "Height")
      private double height;
    ...
    ...
    }

And this is the entry in my dynamodb table Enter image description here

This is my service class performing the query for documentId (get all images for document id x):

import com.amazonaws.client.builder.AwsClientBuilder;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
    import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList;
    import java.text.SimpleDateFormat;
    import java.util.List;
    import java.util.TimeZone;
    import org.slf4j.Logger;
    
    public class ImageService {
    
      private AmazonDynamoDB amazonDynamoDB;
      private DynamoDBMapper mapper;
    
      private SimpleDateFormat sdf;
    
      private String tableName;
    
      public ImageService local() {
        amazonDynamoDB =
            AmazonDynamoDBClientBuilder.standard()
                .withEndpointConfiguration(
                    new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "local"))
                .build();
        mapper = new DynamoDBMapper(amazonDynamoDB);
        return this;
      }
    
      public ImageService cloud() {
        amazonDynamoDB = AmazonDynamoDBClientBuilder.standard().build();
        mapper = new DynamoDBMapper(amazonDynamoDB);
        this.tableName = System.getenv("IMAGES_TABLE");
        return this;
      }
    
      public ImageService init() {
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        return this;
      }
    
      public List<ImageDetail> getImagesByDocumentId(String documentId, Logger logger) {
        DynamoDBMapperConfig mapperConfig =
            DynamoDBMapperConfig.builder()
                .withTableNameOverride(
                    DynamoDBMapperConfig.TableNameOverride.withTableNameReplacement(tableName))
                .build();
    
        ImageDetail imageDetailKey = new ImageDetail();
        imageDetailKey.setDocumentId(documentId); // Ensure this sets the documentId field
    
        DynamoDBQueryExpression<ImageDetail> queryExpression =
            new DynamoDBQueryExpression<ImageDetail>()
                .withHashKeyValues(imageDetailKey)
                .withConsistentRead(false);
    
        PaginatedQueryList<ImageDetail> query =
            mapper.query(ImageDetail.class, queryExpression, mapperConfig);
        logger.info("results: ");
        logger.info(query.toString());
        return query;
      }
    }

The result I get back is this: 1703153761850,"2023-12-21 10:16:01 da71511d-f14e-48d2-8f33-ae623818745d INFO XyzService - ImageDetail{documentId='a0d266bc-85ce-4595-a85d-fe1142c81fdf', id='null', trackPosition=6, imageUrl='null', name='image1.png', width=1920.0, height=938.0}

I am a bit at loss as to why imageId and imageUrl are null. Any suggestion or recommendation very much welcome. Thank you

  • The item which you show in the image and the item in your output with null values are not the same item. Can you double check that the items attributes exist?

질문됨 6달 전353회 조회
1개 답변
-2

As Described by Leeroy Hannigan, I kindly request you to double check the partition key of the query API.

AWS
지원 엔지니어
답변함 5달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠