如何将复合主键与 documentclient.batchWrite() 一起使用?
How do I use a composite primary key with documentclient.batchWrite()?
我正在尝试在 this documentation 之后进行批量删除。它给出了以下示例:
var params = {
RequestItems: { /* required */
'<TableName>': [
{
DeleteRequest: {
Key: { /* required */
'<AttributeName>': someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* '<AttributeName>': ... */
}
},
PutRequest: {
Item: { /* required */
'<AttributeName>': someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* '<AttributeName>': ... */
}
}
},
/* more items */
],
/* '<TableName>': ... */
},
ReturnConsumedCapacity: INDEXES | TOTAL | NONE,
ReturnItemCollectionMetrics: SIZE | NONE
};
documentclient.batchWrite(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
- Key — 必需 — (map) — 可序列化的 JavaScript 对象。有关受支持类型的信息,请参阅 DynamoDB Data Model
进一步查看 DynamoDB 文档,我发现 this parameter description
- Key - 唯一标识项目的主键属性值的映射。此映射中的每个条目都包含一个属性名称和一个属性值。对于每个主键,您必须提供所有键属性。例如,对于一个简单的主键,您只需要为分区键提供一个值。对于复合主键,您必须同时提供分区键和排序键的值。
我的 table 同时使用分区键和排序键,因此我必须使用复合键。我真的找不到这个请求的示例,我尝试按照上面文档中的示例进行操作,但它既不会引发错误也不会删除所需的项目。
我已尝试使用第一个示例中的以下参数:
const params = {
"RequestItems": {
"table-name": [
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": "partitionKeyValue",
"sort-key-col-name": "sortKeyValue"
}
}
},
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": "partitionKeyValue",
"sort-key-col-name": "sortKeyValue"
}
}
}
]
}
}
按照第二个示例的参数格式进行第二次尝试:
const params = {
"RequestItems": {
"table-name": [
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": {
"S": "partitionKeyValue"
},
"sort-key-col-name": {
"S": "sortKeyValue"
}
}
}
},
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": {
"S": "partitionKeyValue"
},
"sort-key-col-name": {
"S": "sortKeyValue"
}
}
}
}
]
}
}
刚回来'UnprocessedItems'
{ UnprocessedItems: {} }
我哪里错了?
第一个示例中的参数确实有效,代码中的其他内容让我失望。
我正在尝试在 this documentation 之后进行批量删除。它给出了以下示例:
var params = {
RequestItems: { /* required */
'<TableName>': [
{
DeleteRequest: {
Key: { /* required */
'<AttributeName>': someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* '<AttributeName>': ... */
}
},
PutRequest: {
Item: { /* required */
'<AttributeName>': someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* '<AttributeName>': ... */
}
}
},
/* more items */
],
/* '<TableName>': ... */
},
ReturnConsumedCapacity: INDEXES | TOTAL | NONE,
ReturnItemCollectionMetrics: SIZE | NONE
};
documentclient.batchWrite(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
- Key — 必需 — (map) — 可序列化的 JavaScript 对象。有关受支持类型的信息,请参阅 DynamoDB Data Model
进一步查看 DynamoDB 文档,我发现 this parameter description
- Key - 唯一标识项目的主键属性值的映射。此映射中的每个条目都包含一个属性名称和一个属性值。对于每个主键,您必须提供所有键属性。例如,对于一个简单的主键,您只需要为分区键提供一个值。对于复合主键,您必须同时提供分区键和排序键的值。
我的 table 同时使用分区键和排序键,因此我必须使用复合键。我真的找不到这个请求的示例,我尝试按照上面文档中的示例进行操作,但它既不会引发错误也不会删除所需的项目。
我已尝试使用第一个示例中的以下参数:
const params = {
"RequestItems": {
"table-name": [
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": "partitionKeyValue",
"sort-key-col-name": "sortKeyValue"
}
}
},
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": "partitionKeyValue",
"sort-key-col-name": "sortKeyValue"
}
}
}
]
}
}
按照第二个示例的参数格式进行第二次尝试:
const params = {
"RequestItems": {
"table-name": [
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": {
"S": "partitionKeyValue"
},
"sort-key-col-name": {
"S": "sortKeyValue"
}
}
}
},
{
"DeleteRequest": {
"Key": {
"partition-key-col-name": {
"S": "partitionKeyValue"
},
"sort-key-col-name": {
"S": "sortKeyValue"
}
}
}
}
]
}
}
刚回来'UnprocessedItems'
{ UnprocessedItems: {} }
我哪里错了?
第一个示例中的参数确实有效,代码中的其他内容让我失望。