DynamoDB Local使用(一)


使用 DynamoDB Local 可以不用访问 DynamoDB Web,实现本地的测试,节省预置的吞吐量、数据存储和数据传输费用。
参照: 设置 DynamoDB Local (可下载版本).
官方提供了三种方式

  • jar包本地运行
  • Maven依赖运行
  • Docker配置

本节测试第一种方式。

前提

  • JRE 6.x版本 以上

下载

参照: 计算机上的 DynamoDB(可下载版本).
下载,解压后到目录。

启动

// PowerShell
PS C:\Users\user> cd C:\Work\dynamodb-local
PS C:\Work\dynamodb-local> java -D"java.library.path=./DynamoDBLocal_lib" -jar DynamoDBLocal.jar -sharedDb
Initializing DynamoDB Local with the following configuration:
Port:   8000
InMemory:       false
DbPath: null
SharedDb:       true
shouldDelayTransientStatuses:   false
CorsParams:     *
主要命令选项 概要
-help 帮助
-sharedDb DynamoDB 将创建一个名为 shared-local-instance.db 的数据库文件。连接到 DynamoDB 的每个程序都将访问此文件,实现协作共享。使用AWS-CLI或者工具包访问DB时,创建DB,启动时会看不到该文件。省略 -sharedDb,则数据库文件出现在应用程序配置中时,将命名为 myaccesskeyid_region.db(包含 AWS 访问密钥 ID 和区域)
-inMemory DynamoDB 完全不会编写任何数据库文件。所有数据将被写入内存中。不能同时指定 -dbPath 和 -inMemory
-dbPath DynamoDB 写入数据库文件的目录。如果您未指定此选项,则文件将写入到当前目录。
-optimizeDbBeforeStartup 在计算机上启动 DynamoDB 之前优化底层数据库表。必须指定 -dbPath 参数,以便 DynamoDB 可找到其数据库文件
-cors 跨源资源共享 (CORS) 的支持。必须提供特定域的逗号分隔“允许”列表。-cors 的默认设置是星号 (*),这将允许公开访问。
-delayTransientStatuses 使 DynamoDB 为某些操作引入延迟。DynamoDB (下载版本) 几乎可以即时执行某些任务,如针对表和索引的创建/更新/删除操作。
-port 默认情况下,DynamoDB 使用端口 8000

命令行访问

启动后,可以通过本地终端节点(http://localhost:8000)默认8000端口,访问DB,使用参数
–endpoint-url
使用 AWS-CLI访问DynamoDB
详细命令参照 Available Commands
或者AWS API Documentation

create-table

//创建计算机上的 DynamoDB 的表
aws dynamodb create-table
–table-name Music
–attribute-definitions
AttributeName=Artist,AttributeType=S
AttributeName=SongTitle,AttributeType=S
–key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE
–provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
–endpoint-url http://localhost:8000

自动返回Json格式的结果

{
    "TableDescription": {
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/Music",
        "AttributeDefinitions": [
            {
                "AttributeName": "Artist",
                "AttributeType": "S"
            },
            {
                "AttributeName": "SongTitle",
                "AttributeType": "S"
            }
        ],
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "WriteCapacityUnits": 1,
            "LastIncreaseDateTime": 0.0,
            "ReadCapacityUnits": 1,
            "LastDecreaseDateTime": 0.0
        },
        "TableSizeBytes": 0,
        "TableName": "Music",
        "BillingModeSummary": {
            "LastUpdateToPayPerRequestDateTime": 0.0,
            "BillingMode": "PROVISIONED"
        },
        "TableStatus": "ACTIVE",
        "KeySchema": [
            {
                "KeyType": "HASH",
                "AttributeName": "Artist"
            },
            {
                "KeyType": "RANGE",
                "AttributeName": "SongTitle"
            }
        ],
        "ItemCount": 0,
        "CreationDateTime": 1551148098.869
    }
}

list-tables

//列出计算机上的 DynamoDB 中的表
aws dynamodb list-tables --endpoint-url http://localhost:8000

结果

{
    "TableNames": [
        "Music"
    ]
}

describe-table

//列出计算机上的 DynamoDB 表的具体信息
aws dynamodb describe-table --table-name “Music” --endpoint-url http://localhost:8000

结果

内容和表创建后返回的内容一样

update-table

//update-table
aws dynamodb update-table --endpoint-url http://localhost:8000 --table-name test --provisioned-throughput ReadCapacityUnits=2,WriteCapacityUnits=2

返回表的描述信息,可以确认变更内容

{
    "TableDescription": {
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/test",
        "AttributeDefinitions": [
            {
                "AttributeName": "testId",
                "AttributeType": "S"
            }
        ],
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "WriteCapacityUnits": 2,
            "LastIncreaseDateTime": 0.0,
            "ReadCapacityUnits": 2,
            "LastDecreaseDateTime": 0.0
        },
        "TableSizeBytes": 0,
        "TableName": "test",
        "BillingModeSummary": {
            "LastUpdateToPayPerRequestDateTime": 0.0,
            "BillingMode": "PROVISIONED"
        },
        "TableStatus": "ACTIVE",
        "KeySchema": [
            {
                "KeyType": "HASH",
                "AttributeName": "testId"
            }
        ],
        "ItemCount": 0,
        "CreationDateTime": 1551149210.869
    }
}

delete-table

//delete-table
aws dynamodb delete-table --endpoint-url http://localhost:8000 --table-name test2

结果

返回删除对象表的JSON描述信息

put-item

–item后面可以直接写JSON格式字符串或者做成文件导入也可以

// DynamoDB 中的表里添加数据
aws dynamodb put-item --endpoint-url http://localhost:8000 --table-name test --item ‘{“testId”: {“S”: “1” } }’ --return-consumed-capacity TOTAL
或者
aws dynamodb put-item --table-name “Music” --item file://C:\Work\dynamodb-local\put-item.json --return-consumed-capacity TOTAL --endpoint-url http://localhost:8000
结果

{
    "ConsumedCapacity": {
        "CapacityUnits": 1.0,
        "TableName": "Music"
    }
}

get-item

//获取 DynamoDB 中表的数据
aws dynamodb get-item --endpoint-url http://localhost:8000 --table-name test --key ‘{“testId”: {“S”: “1” } }’
结果

{
    "Item": {
        "testId": {
            "S": "1"
        }
    }
}

delete-item

//delete-item
aws dynamodb delete-item --endpoint-url http://localhost:8000 --table-name test --key ‘{“testId”: {“S”: “1” } }’

query

//query
aws dynamodb query --endpoint-url http://localhost:8000 --table-name Music --projection-expression “SongTitle” --key-condition-expression “Artist = :v1” --expression-attribute-values file://C:\Work\kou\dynamodb-local\expression-attributes.json

结果

{
    "Count": 1,
    "Items": [
        {
            "SongTitle": {
                "S": "Call Me Today"
            }
        }
    ],
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

Browser访问

http://localhost:8000/shell/

put-item

AWS.config.endpoint = new AWS.Endpoint('http://localhost:8000');
var dynamodb = new AWS.DynamoDB();
var params = {
    TableName: 'Persons',
    Item:{
        'Id':{N: '2'},
        'Name':{S: 'Daniel'},
        'Gender':{S: 'male'},
        'Height':{N: '175.5'}
    }
};
dynamodb.putItem(params);

get-item

AWS.config.endpoint = new AWS.Endpoint('http://localhost:8000');
var dynamodb = new AWS.DynamoDB();
var params = {
    TableName: 'test',
    Key:{
        'testId':{S: '1'}
    }
};
dynamodb.getItem(params, function(err,data){
	if(err){
		console.log(err);
	} else {
		console.log(data);
	}
});

猜你喜欢

转载自blog.csdn.net/oblily/article/details/87933033
今日推荐