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

Is it possible to create a Global Secondary Index on a JVM-based DynamoDB Local instance

我是 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.access0(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-07-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 中根本没有提到二级索引)

有可能。我猜你得到的错误是因为你有

.withKeyType(KeyType.RANGE)

指定了两次。只有 1 个属性可以是 range/sort 键,另一个需要是哈希键。

事实证明有两个问题:(1) 缺少 HASH/partition 键和 (2) 我没有指定明确的 "provisioned throughput"。以下代码有效:

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));

注意 withProvisionedThroughput 调用。