[英]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.