如何从DynamoDB中使用分区键获取复合主键的记录。

huangapple go评论65阅读模式
英文:

how to get the records using partition key from a composite primary key in dynamodb

问题

插入代码

Item item_to_insert = new Item().withPrimaryKey("LatLong", key, "location_code", key)
    .withJSON("location_address", jsonW.toString())
    .withString("version", "1");
PutItemOutcome outcome = table.putItem(item_to_insert);

检索

GetItemSpec i_spec = new GetItemSpec()
    .withPrimaryKey("LatLong", key, "location_code", key)
    .withProjectionExpression("location_address")
    .withConsistentRead(true);

现在我想仅使用分区键 LatLong 属性检索记录。有任何方法可以做到这一点吗?

英文:

I am using java to insert and retrieve records in DynamoDB.

Insertion Code

                    Item item_to_insert = new Item().withPrimaryKey("LatLong", key,"location_code",key)
                        .withJSON("location_address", jsonW.toString())
                        .withString("version","1");
                PutItemOutcome outcome = table.putItem(item_to_insert);

Retrieving

                GetItemSpec i_spec = new GetItemSpec()
                        .withPrimaryKey("LatLong", key,"location_code",key)
                        .withProjectionExpression("location_address")
                        .withConsistentRead(true);

Now I want to retrieve the records just using LatLong attribute which is partition key. Any Idea how to do this??

答案1

得分: 1

GetItem 获取特定项目,需要您指定完整的主键。如果您想检索所有具有共同分区键的项目,我认为您要找的方法是 Query 方法。

在此处查看更多信息:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html

代码将类似于以下内容:

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("Id = :v_id")
    .withValueMap(new ValueMap()
    .withString(":v_id", "Amazon DynamoDB#DynamoDB Thread 1"));
英文:

GetItem fetches a specific item, which requires you to specify the complete primary key. If you want to retrieve all items with a common partition key, I believe the method you're looking for is the Query method.

See more here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html

The code would look something like this:

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("Id = :v_id")
    .withValueMap(new ValueMap()
    .withString(":v_id", "Amazon DynamoDB#DynamoDB Thread 1"));

huangapple
  • 本文由 发表于 2020年10月5日 14:03:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64203179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定