簡體   English   中英

使用 GSI 和范圍鍵查詢 DynamoDB

[英]Query DynamoDB using a GSI and a range key

我有一個帶有 Hash 鍵 (id) 的表(配置文件),我在名稱上有一個 GSI,在國家/地區上有一個范圍鍵。 我想創建一個 DAO 方法,它會獲取所有具有給定名稱和國家/地區值的記錄,如下所示:List getProfileWithNameAndCountry(name, country);

最初我只是使用以下代碼僅使用名稱 (GSI) 搜索記錄,它運行良好:

Profile profile = new Profile();
profile.setCountry("name");
DynamoDBQueryExpression<Profile> queryExpression = new DynamoDBQueryExpression<Profile>()
                            .withIndexName("name-index")
                            .withHashKeyValues(profile)
                            .withConsistentRead(false);

但現在我想使用名稱 (GSI) 和國家/地區(范圍鍵)進行搜索。 我嘗試將上面的代碼修改為此,但失敗並出現異常:

Profile profile = new Profile();
profile.setCountry("name");
Condition countryCondition = new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ.toString())
                    .withAttributeValueList(new AttributeValue().withS(country));
DynamoDBQueryExpression<Profile> queryExpression = new DynamoDBQueryExpression<Profile>()
                            .withIndexName("name-index")
                            .withHashKeyValues(profile)
                            .withRangeKeyCondition("country", countryCondition)
                            .withConsistentRead(false);

我真的是 DynamoDB 的新手,我無法弄清楚如何在 java 中實現這一點。非常感謝任何幫助。

您不能將 GSI 中的 hash 鍵與表的范圍鍵結合使用。

您需要使用 hash 和范圍鍵創建 GSI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM