DynamoDB在本地计算机中创建表

sum*_*uma 18 amazon-dynamodb

我已将DynamoDB jar下载到我的本地Windows机器,并能够使用下面的命令启动服务.

java -jar DynamoDBLocal.jar -dbPath .
Run Code Online (Sandbox Code Playgroud)

我可以使用localhost:8000/shell /访问Web控制台

但是,我不知道如何创建表,有人可以给我语法和任何示例

如果我想创建具有以下细节的表,如何做和插入数据?

表:学生列:sid,名字,姓氏,地址.

感谢您的意见.

Vis*_*l R 17

文件很难理解.由于您使用的是dynamodb shell,我假设您要求使用js查询来创建表.

var params = {
TableName: 'student',
KeySchema: [ 
    { 
        AttributeName: 'sid',
        KeyType: 'HASH',
    },
],
AttributeDefinitions: [ 
    {
        AttributeName: 'sid',
        AttributeType: 'N', 
    },


],
ProvisionedThroughput: { 
    ReadCapacityUnits: 10, 
    WriteCapacityUnits: 10, 
},
};

dynamodb.createTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response

});
Run Code Online (Sandbox Code Playgroud)

在浏览器中运行上面的代码段(localhost:8000/shell /).它创建一个以'sid'作为哈希键的表.要插入:

var params = {
TableName: 'student',
Item: { // a map of attribute name to AttributeValue

    sid: 123,
    firstname : { 'S': 'abc' },
    lastname : { 'S': 'xyz' },
    address : {'S': 'pqr' },
    ReturnValues: 'NONE', // optional (NONE | ALL_OLD)
    ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
    ReturnItemCollectionMetrics: 'NONE', // optional (NONE | SIZE)
};
docClient.put(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});
Run Code Online (Sandbox Code Playgroud)

  • 看着这个已经好几天了。从字面上看,这是我第一次看到“ localhost:8000 / shell”被引用。说了很多有关该地区的文档状态! (2认同)
  • 是的,我花了一天时间才弄清楚出了什么问题。而这只是众多服务中的一项。 (2认同)
  • 您还可以使用 https://dynobase.dev/dynamodb-table-schema-design-tool/ 生成“params”,因为它的结构相当困难。 (2认同)

lvt*_*llo 15

我建议使用 docker(但也可以运行 jar):

$ docker run -d -p 8000:8000 amazon/dynamodb-local 
Run Code Online (Sandbox Code Playgroud)

然后你可以通过传入以下内容在 docker 容器中创建一个表endpoint-url

$ aws dynamodb create-table \
   --table-name UnifiedTable \
   --attribute-definitions AttributeName=pk,AttributeType=S AttributeName=sk,AttributeType=S \
   --key-schema AttributeName=pk,KeyType=HASH AttributeName=sk,KeyType=RANGE \
   --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
   --endpoint-url http://localhost:8000
Run Code Online (Sandbox Code Playgroud)

您可以像这样检查表是否存在:

$ aws dynamodb list-tables --endpoint-url http://localhost:8000

# Output:
# {
#     "TableNames": [
#         "UnifiedTable"
#     ]
# }
Run Code Online (Sandbox Code Playgroud)