是否可以在基于JVM的DynamoDB Local实例上创建全局二级索引

时间:2018-07-25 13:25:52

标签: java amazon-dynamodb dynamo-local

我是DynamoDB的新手,我在JVM内运行着一个本地实例,但是当我尝试创建一个定义如下的索引时

GlobalSecondaryIndexUpdate stockIndex = new GlobalSecondaryIndexUpdate()
        .withCreate(new CreateGlobalSecondaryIndexAction()
                .withIndexName("StockIndex")
                .withKeySchema(new KeySchemaElement().withAttributeName("stock").withKeyType(KeyType.RANGE), // Partition key
                        new KeySchemaElement().withAttributeName("date").withKeyType(KeyType.RANGE)) // Compound key?
                .withProjection(new Projection().withProjectionType(projectionType)));

我遇到未知的内部故障,例如:

Caused by: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The request processing has failed because of an unknown error, exception or failure. (Service: AmazonDynamoDBv2; Status Code: 500; Error Code: InternalFailure; Request ID: dccfbf27-2e33-463c-b36e-97a432f4cd6b)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1588)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1258)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:742)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:716)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2089)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2065)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeUpdateTable(AmazonDynamoDBClient.java:1921)

尝试创建GSI时。我定义的索引是否错误,还是DynamoDB Local不支持此索引?

更新2018年7月30日:

我使用以下方法进行了测试:

// StockIndex
GlobalSecondaryIndexUpdate stockIndex = new GlobalSecondaryIndexUpdate()
        .withCreate(new CreateGlobalSecondaryIndexAction()
                .withIndexName("StockIndex")
                .withKeySchema(
                        new KeySchemaElement().withAttributeName("stock").withKeyType(KeyType.HASH)) // Partition key
                        new KeySchemaElement().withAttributeName("date").withKeyType(KeyType.RANGE)) // Compound key?
                .withProjection(new Projection().withProjectionType(ProjectionType.ALL)));

,并且在date上没有RANGE键,但是两次我都得到了同样模糊的InternalFailure。我怀疑DynamoDB Local实例根本不支持GSI(尽管Usage Notes中根本没有提到二级索引)

2 个答案:

答案 0 :(得分:1)

有可能。我猜您收到的错误是因为您有

.withKeyType(KeyType.RANGE)

指定两次。范围/排序键只能是1个,其他键必须是哈希键。

答案 1 :(得分:0)

事实证明,这里存在两个问题:(1)缺少HASH /分区键;(2)我没有指定明确的“设置的吞吐量”。以下代码有效:

{"index": "creation.date.string"}

请注意 CreateGlobalSecondaryIndexAction stockIndex = new CreateGlobalSecondaryIndexAction() .withIndexName("StockIndex") .withKeySchema( new KeySchemaElement().withAttributeName("stock").withKeyType(KeyType.HASH), // Partition key new KeySchemaElement().withAttributeName("date").withKeyType(KeyType.RANGE)) // Sort key .withProvisionedThroughput(new ProvisionedThroughput(20L, 20L)) .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY)); 呼叫。