如何仅使用分区键从AWS Dynamodb获取数据? [英] How to get data from aws Dynamodb with using partition key only?

查看:218
本文介绍了如何仅使用分区键从AWS Dynamodb获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用aws-sdk-go库在Golang中进行DynamoDb连接。

I am using aws-sdk-go library for DynamoDb connectivity in Golang.

我的DynamoDb表具有一个分区键DeviceId(字符串)和一个排序键时间(数字)。如何编写GetItemInput以获取具有特定DeviceId的所有数据?

My DynamoDb table have a Partition key DeviceId (String) and a Sort Key Time (Number). How can I write GetItemInput to get all data with a specific DeviceId?

params := &dynamodb.GetItemInput{

    Key:    map[string]*dynamodb.AttributeValue {
        "DeviceId": {
            S: aws.String("item_1"),
        },
    },
    ExpressionAttributeNames: map[string]*string{
        "DeviceId": "DeviceId",
    },
    TableName:  aws.String("DbName"), 
}

list, err := svc.GetItem(params)


推荐答案

您必须使用查询或扫描操作,这是一个简单的示例,但是您可以在Amazon文档此处

You have to use Query or Scan operation, this is a simple example but you can read more on Amazon documentation here

特别是查询操作



查询操作仅使用主键属性值在表或二级索引中查找项目

A Query operation finds items in a table or a secondary index using only primary key attribute values




var queryInput = &dynamodb.QueryInput{
    TableName: aws.String(dynamoRestDataTableName),
    KeyConditions: map[string]*dynamodb.Condition{
        "DeviceId": {
            ComparisonOperator: aws.String("EQ"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    S: aws.String("aDeviceId"),
                },
            },
        },
    },
}

var resp, err = dynamoSvc.Query(queryInput)
if err != nil {
    return nil, err
}

这篇关于如何仅使用分区键从AWS Dynamodb获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆