@DynamoDBIndexHashKey必须指定HASH GSI名称之一

时间:2018-08-01 22:02:50

标签: java amazon-dynamodb

我的课程如下

@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获取结果,但是不知何故,这导致了异常 有人可以看出我做错了什么吗

2 个答案:

答案 0 :(得分:1)

您的异常消息说

@DynamoDBIndexHashKey must specify one of HASH GSI name/names

如果查看班级中所有的@DynamoDBIndexHashKey注释,除了globalSecondaryIndexName上的注释,它们都会为eventType设置值。如果eventType应该是GSI哈希键,则需要设置此值。如果不应该将eventType作为GSI的哈希键,则需要从该字段中删除@DynamoDBIndexHashKey注释。

有关@DynamoDBImdexHashKey的用法,请参见javadoc

答案 1 :(得分:0)

它必须看起来像这样:

@get:DynamoDBIndexHashKey(attributeName = "product", globalSecondaryIndexName = "product-index")
var product: String? = null

也将其添加到数据库定义中:

CreateTableRequest()

 ...

 .withGlobalSecondaryIndexes(GlobalSecondaryIndex()
            .withIndexName(PRODUCT_INDEX)
            .withKeySchema(
               KeySchemaElement(PRODUCT, KeyType.HASH),
               KeySchemaElement(PRODUCT_RANGE, KeyType.RANGE))
            .withProjection(Projection().withProjectionType(ProjectionType.ALL))
            .withProvisionedThroughput(ProvisionedThroughput(1L, 2L)))