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

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

问题描述

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

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

参数 := &dynamodb.GetItemInput{键:map[string]*dynamodb.AttributeValue {设备编号": {S: aws.String("item_1"),},},表达式属性名称:map[string]*string{"设备标识符": "设备标识符",},表名:aws.String("DbName"),}列表,错误:= svc.GetItem(params)

解决方案

你必须使用 Query 或 Scan 操作,这是一个简单的例子,但你可以阅读更多亚马逊文档 这里

特别是查询操作

<块引用><块引用>

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

var queryInput = &dynamodb.QueryInput{表名:aws.String(dynamoRestDataTableName),KeyConditions:map[string]*dynamodb.Condition{设备编号": {比较运算符:aws.String("EQ"),AttributeValueList:[]*dynamodb.AttributeValue{{S: aws.String("aDeviceId"),},},},},}var resp, err = dynamoSvc.Query(queryInput)如果错误!= nil {返回零,错误}

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

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)

解决方案

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

In particular, Query operation

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天全站免登陆