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
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?