DynamoDB 属于AWS 专有的 NoSQL 数据库服务。其实和Mongod类似。
由于DynamoDB 属于AWS的,需要下载专有的工具操作。
在 DynamoDB 中,表、项目和属性是您使用的核心组件。表 是项目 的集合,而每个项目是属性 的集合。DynamoDB 使用主键来唯一标识表中的每个项目,并且使用二级索引来提供更大的查询灵活性。您可以使用 DynamoDB 流 捕获 DynamoDB 表中的数据修改事件。
操作 DynamoDB 需要提前装 AWS Command Line Interface (AWS CLI)之后便可以 命令行管理多个 AWS 服务并通过脚本自动执行这些服务。
安装 AWS CLI 工具 , 提前安装好Python 中的pip,然后执行 pip install awscli ,安装成功之后提示
➜ DynamoDB_install aws --version
aws-cli/1.16.121 Python/2.7.15rc1 Linux/4.15.0-45-generic botocore/1.12.111
➜ DynamoDB_install aws configure
AWS Access Key ID [****************ZDBQ]:
AWS Secret Access Key [****************XknV]:
Default region name [us-west-2]:
Default output format [json]:
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
aws dynamodb list-tables --endpoint-url http://localhost:8000
以上步骤便可以通过命令行操作数据库
Node.js 和 DynamoDB
通过API的方式操作DynamoDB数据库
npm install aws-sdk
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
//新建DynamoDB对象
var dynamodb = new AWS.DynamoDB();
// year – 分区键。AttributeType 为 N,表示数字。
// title – 排序键。AttributeType 为 S,表示字符串。
// 表的内容
var params = {
TableName : "Movies",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
// DynamoDB 中创建表
dynamodb.createTable(params, function(err, data) {
if (err) {
console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
node MoviesCreateTable.js
以上只是学习DynamoDB过程中的笔记,更加详细可以参考官网更详细的教程