我一直在阅读很多文档,特别是AWS.DynamoDB.DocumentClient
,我正在试图弄清楚如何创建一个具有特定主键的Table / DocumentClient,如id
。我的理解是主键是必需的,“二级索引”是嵌套属性,但我没有看到任何构造函数参数实际指定id
将是主键。
如何指定id
应该是Table / DocumentClient的主键?
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html
请注意 主键是必需的。此代码添加一个具有主键(年份,标题)和信息属性的项目。 info属性存储样本JSON,提供有关影片的更多信息。
答案 0 :(得分:0)
好的,我没有意识到创建表的架构是如此错综复杂。通过本地DynamoDB的试错,它告诉我KeySchema必须存在AttributeDefinitions。此键模式用于指定“主要”键名称,而类型位于完全独立的字段中。
构造函数对象不填充表,它只设置初始分区(primary?)键和可选的排序键,这将形成一个复合键。
var AWS = require('aws-sdk');
AWS.config.update({
region: 'us-west-2',
endpoint: 'http://localhost:8000'
});
var db = new AWS.DynamoDB();
var awaitTable = db.describeTable({ TableName: 'app-content' }).promise();
awaitTable.catch(e => {
if (e.statusCode === 400) {
return db.createTable({
TableName: 'app-content',
KeySchema: [
{ 'AttributeName' : 'fooPartitionKeyName', 'KeyType' : 'HASH' },
{ 'AttributeName' : 'barSortKeyName', 'KeyType' : 'RANGE' }
],
AttributeDefinitions: [
{ 'AttributeName' : 'fooPartitionKeyName', 'AttributeType' : 'S' },
{ 'AttributeName' : 'barSortKeyName', 'AttributeType' : 'S' }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5,
},
}).promise();
} else { return Promise.reject('Unknown error fetching table.') }
}).then(table => {
console.log('table!', table);
});
答案 1 :(得分:0)
KeySchema: [
{
AttributeName: 'ID',
KeyType: 'HASH'
},
这应该可行吗?