如何使用像 postgresql 迁移那样的脚本创建或更新 dynamo db table
how to create or update dynamo db table with script like postgresql migrations do
DynamoDB
ArrayList<AttributeDefinition> attributeDefinitions = new
ArrayList<>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("ID").withAttributeType("S"));
ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
keySchema.add(new KeySchemaElement()
.withAttributeName("ID").withKeyType(KeyType.HASH));
CreateTableRequest createTableReq = new CreateTableRequest()
.withTableName("Product")
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(keySchema)
.withProvisionedThroughput(new
ProvisionedThroughput()
.withReadCapacityUnits(10L)
.withWriteCapacityUnits(5L));
CreateTableResult result = dynamodb.createTable(createTableReq);
System.out.println(result.toString());
如果我想创建一个像这样的 table 我在哪里可以把这段代码只运行一次,有没有迁移脚本工具或类似的东西?
非常感谢。
DynamoDB 是一个 NoSQL 数据库,这意味着如果您想要一种简单的查询语言与其进行交互,您就不走运了。
参见参考文献:Automating dynamodb scripts
现在可能有好消息,(如果你知道Javascript
):)
DynamoDB
为 Javascript
提供了一个简单的 SDK
,你可以创建一个 xyz.js JavaScript
文件,运行 这个脚本使用 node
添加条目的示例:
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Load credentials and set Region from JSON file
AWS.config.loadFromPath('./config.json');
// Create DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var myTable = 'TABLE_NAME';
// Add the four results for spades
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '0'}, 'imageFile' : {S: 'spad_a.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '1'}, 'imageFile' : {S: 'spad_k.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '2'}, 'imageFile' : {S: 'spad_q.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '3'}, 'imageFile' : {S: 'spad_j.png'}
}
};
post();
// Add the four results for hearts
.
.
.
// Add the four results for diamonds
.
.
.
// Add the four results for clubs
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '12'}, 'imageFile' : {S: 'club_a.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '13'}, 'imageFile' : {S: 'club_k.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '14'}, 'imageFile' : {S: 'club_q.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '15'}, 'imageFile' : {S: 'club_j.png'}
}
};
post();
function post () {
ddb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
供参考:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-lambda-ddb-setup.html
DynamoDB
ArrayList<AttributeDefinition> attributeDefinitions = new
ArrayList<>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("ID").withAttributeType("S"));
ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
keySchema.add(new KeySchemaElement()
.withAttributeName("ID").withKeyType(KeyType.HASH));
CreateTableRequest createTableReq = new CreateTableRequest()
.withTableName("Product")
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(keySchema)
.withProvisionedThroughput(new
ProvisionedThroughput()
.withReadCapacityUnits(10L)
.withWriteCapacityUnits(5L));
CreateTableResult result = dynamodb.createTable(createTableReq);
System.out.println(result.toString());
如果我想创建一个像这样的 table 我在哪里可以把这段代码只运行一次,有没有迁移脚本工具或类似的东西?
非常感谢。
DynamoDB 是一个 NoSQL 数据库,这意味着如果您想要一种简单的查询语言与其进行交互,您就不走运了。
参见参考文献:Automating dynamodb scripts
现在可能有好消息,(如果你知道Javascript
):)
DynamoDB
为 Javascript
提供了一个简单的 SDK
,你可以创建一个 xyz.js JavaScript
文件,运行 这个脚本使用 node
添加条目的示例:
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Load credentials and set Region from JSON file
AWS.config.loadFromPath('./config.json');
// Create DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var myTable = 'TABLE_NAME';
// Add the four results for spades
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '0'}, 'imageFile' : {S: 'spad_a.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '1'}, 'imageFile' : {S: 'spad_k.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '2'}, 'imageFile' : {S: 'spad_q.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '3'}, 'imageFile' : {S: 'spad_j.png'}
}
};
post();
// Add the four results for hearts
.
.
.
// Add the four results for diamonds
.
.
.
// Add the four results for clubs
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '12'}, 'imageFile' : {S: 'club_a.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '13'}, 'imageFile' : {S: 'club_k.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '14'}, 'imageFile' : {S: 'club_q.png'}
}
};
post();
var params = {
TableName: myTable,
Item: {'slotPosition' : {N: '15'}, 'imageFile' : {S: 'club_j.png'}
}
};
post();
function post () {
ddb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
供参考:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-lambda-ddb-setup.html