我的班级如下
@DynamoDBTable(tableName = LogConstant.TableName)
public class Journal {
@DynamoDBIndexHashKey(attributeName = "event_type")
private String eventType;
@DynamoDBIndexHashKey(attributeName = "user_id",globalSecondaryIndexName = LogConstant.GlobalUserIdIndex)
private String userId;
@DynamoDBIndexHashKey(attributeName = "user_identifier", globalSecondaryIndexName = LogConstant.GlobalUserIdentifierIndex)
private String userIdentifier;
@DynamoDBIndexHashKey(attributeName = "order_id", globalSecondaryIndexName = LogConstant.GlobalOrderIdIndex)
private String orderId;
@DynamoDBTypeConvertedEnum
@DynamoDBAttribute(attributeName = "generated_by")
private GenertionType generatedBy;
@DynamoDBTypeConvertedEnum
@DynamoDBHashKey(attributeName = "interacting_service")
private InteractingService interactingSerice;
@DynamoDBAttribute(attributeName = "agent")
private String agent;
@DynamoDBAttribute(attributeName = "content")
private String content;
@DynamoDBRangeKey
@DynamoDBIndexRangeKey(attributeName = "created_at",
globalSecondaryIndexNames = {LogConstant.GlobalUserIdIndex,LogConstant.GlobalUserIdentifierIndex,LogConstant.GlobalOrderIdIndex})
private String createdAt;
得到搜索结果的服务是
DynamoDBQueryExpression<Journal> expression = new
DynamoDBQueryExpression<Journal>()
.withIndexName(LogConstant.GlobalUserIdIndex)
.withConsistentRead(false)
.withHashKeyValues(journal);
TableDescription table = DynamoDbStarter.getDynamoDB().getTable(LogConstant.TableName).describe();
return DynamoDbStarter.getDynamoDBMapper().query(Journal.class, expression);
即使插入正常工作,但在获取结果期间却出现异常
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: @DynamoDBIndexHashKey must specify one of HASH GSI name/names
at com.amazonaws.services.dynamodbv2.datamodeling.StandardAnnotationMaps$FieldMap.globalSecondaryIndexNames(StandardAnnotationMaps.java:345) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel$Properties$Immutable.<init>(DynamoDBMapperFieldModel.java:459) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$Bean.<init>(StandardBeanProperties.java:92) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$Bean.<init>(StandardBeanProperties.java:86) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$BeanMap.putOrFlatten(StandardBeanProperties.java:217) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$BeanMap.putAll(StandardBeanProperties.java:207) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$BeanMap.<init>(StandardBeanProperties.java:198) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$CachedBeans.getBeans(StandardBeanProperties.java:55) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$CachedBeans.access$100(StandardBeanProperties.java:48) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties.of(StandardBeanProperties.java:42) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$TableBuilder.<init>(StandardModelFactories.java:132) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$TableBuilder.<init>(StandardModelFactories.java:116) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$StandardTableFactory.getTable(StandardModelFactories.java:107) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.getTableModel(DynamoDBMapper.java:393) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.createQueryRequestFromExpression(DynamoDBMapper.java:1626) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.query(DynamoDBMapper.java:1503) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.query(AbstractDynamoDBMapper.java:265) ~[aws-java-sdk-dynamodb-1.11.172.jar:na]
at com.furlenco.platform.loggingservice.service.impl.LogServiceimpl.getLogsBasedOnSecondaryIndexUserId(LogServiceimpl.java:86) ~[classes/:na]
at com.furlenco.platform.loggingservice.service.impl.LogServiceimpl.getEventLogsForUserId(LogServiceimpl.java:67) ~[classes/:na]
我有哈希键为interacting_service和范围键为CreatedDate的表。我正在尝试通过GSI获取结果,但是不知何故,这导致了异常
有人可以看出我做错了什么吗
最佳答案
您的异常消息说
@DynamoDBIndexHashKey must specify one of HASH GSI name/names
如果查看类中的所有
@DynamoDBIndexHashKey
批注,除了globalSecondaryIndexName
上的批注外,它们都为eventType
设置了一个值。如果eventType
应该是GSI哈希键,则需要设置此值。如果不应将eventType
用作GSI的哈希键,则需要从该字段中删除@DynamoDBIndexHashKey
批注。有关
@DynamoDBImdexHashKey
用法的更多详细信息,请参见javadoc。