我有这个数据类
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;
...
...
}
这是我的服务类,执行 documentId 查询(获取文档 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;
}
}
我得到的结果是这样的: 1703153761850,“2023-12-21 10:16:01 da71511d-f14e-48d2-8f33-ae623818745d 信息 XyzService-ImageDetail{documentId='a0d266bc-85ce-4595-a85d-fe1142c81fdf', id='null' , 轨道位置=6 , imageUrl='null', name='image1.png', width=1920.0, height=938.0}
我有点不明白为什么 imageId 和 imageUrl 为空。 欢迎任何建议或推荐。
可能是注解不够,除了注解数据类中的字段外,对应字段的getter方法还应该与dynamodb表项属性同名,即对于imageUrl,将url与ImageUrl注解还不够,属性本身需要被称为 imageUrl,或者至少它的 getter 应该被称为 getImageUrl()