简体   繁体   English

如何仅使用分区键从 aws Dynamodb 获取数据?

[英]How to get data from aws Dynamodb with using partition key only?

I am using aws-sdk-go library for DynamoDb connectivity in Golang.我在 Golang 中使用 aws-sdk-go 库进行 DynamoDb 连接。

My DynamoDb table have a Partition key DeviceId (String) and a Sort Key Time (Number).我的 DynamoDb 表有一个分区键 DeviceId(字符串)和一个排序键时间(数字)。 How can I write GetItemInput to get all data with a specific DeviceId?如何编写 GetItemInput 以获取具有特定 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
}

Query operation can be used in that case在这种情况下可以使用查询操作
Following is one generic example for the same以下是相同的一个通用示例

compositeKey := entity.GetPrimaryKey(inputVar)
expressionAttributeValues := map[string]*dynamodb.AttributeValue{
    ":v1": {
        S: aws.String(compositeKey.PartitionKey.Data.(string)),
    },
}
queryInput := dynamodb.QueryInput{
    TableName:                 &d.TableName,
    KeyConditionExpression:    aws.String("id = :v1"),
    ExpressionAttributeValues: expressionAttributeValues,
}
queryOutput, err := d.DdbSession.Query(&queryInput)
if err != nil {
    log.Error("error in fetching records ", err)
    return nil, err
}

// unmarshal the query output - items to interface

err = dynamodbattribute.UnmarshalListOfMaps(queryOutput.Items, &yourInterface)

You can use getItem in JavaScript SDK v2 like this您可以像这样在 JavaScript SDK v2 中使用 getItem

const params = {
    TableName: 'tableName',
    Key: {
        id: { S: id },
    },
};

const result = await dynamoDb.getItem(params).promise();
if (result.Item === undefined) {
    throw new Error('not found');
}
const item = AWS.DynamoDB.Converter.unmarshall(result.Item)

If JavaScript SDK can do this, I assume golang SDK can also.如果 JavaScript SDK 可以做到这一点,我假设 golang SDK 也可以。 If not, that means AWS doesn't take all languages equally?如果不是,那意味着 AWS 不会平等对待所有语言?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 boto3(lambda) 对 AWS dynamodb 表进行分组并获取分区键的最新值? - how to group AWS dynamodb table and get latest value of partition key using boto3(lambda)? 在 DynamoDB 中使用 AWS Cognito UserID 作为分区键 - Using AWS Cognito UserID as partition key in DynamoDB AWS DynamoDB 表分区键 - AWS DynamoDB table Partition Key aws Dynamodb 通过 aws 管理控制台中的分区键获取多个项目 - aws Dynamodb get multiple items by partition key in aws management console AWS DynamoDB 如何使用不带范围键的 CreateMultiTableBatchGet 从多个表中检索数据 - AWS DynamoDB how to retrieve data from multiple tables using CreateMultiTableBatchGet without Range Key DynamoDB 中相同分区键的数据分布 - Same partition key's data distribution in DynamoDB 使用 AWS Amplify 从 DynamoDB 查询数据 - Querying data from DynamoDB using AWS Amplify 在 DynamoDB 中使用 json 作为排序键/分区键值的好习惯吗? - Is using json as sort key/partition key value good practice in DynamoDB? 使用不是分区键的属性从 dynamoDB 检索项目的最佳方法 - Best way to retrieve an item from dynamoDB using attribute which is not partition key 如何使用 AWS Go SDK 仅更新 DynamoDB 中的单个字段 - How to only update a single field in DynamoDB using AWS Go SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM