DynamoDB create tables in local machine

我怕爱的太早我们不能终老 提交于 2020-12-29 08:49:29

问题


I have downloaded DynamoDB jars to my local windows machine and able to start service using command below.

java -jar DynamoDBLocal.jar -dbPath .

i can access the web console using localhost:8000/shell/

However, I am not sure how to create table, can someone give me the syntax and any examples

if I want to create table with below details, how to do and insert the data?

Table: student columns: sid, firstname, lastname, address.

Appreciate your inputs.


回答1:


The documentation can be a bit difficult to understand. Since you are using the dynamodb shell, I'll assume you are asking for a JavaScript query to create the table.

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 the above snippet in the browser at the local db shell

http://localhost:8000/shell/

It creates a table with 'sid' as hash key.

To insert:

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
});



回答2:


I would recommend using docker (but running the jar is also possible):

$ docker run -d -p 8000:8000 amazon/dynamodb-local 

Then you can create a table in the docker container by passing in the 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

You can check if the table exists like this:

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

# Output:
# {
#     "TableNames": [
#         "UnifiedTable"
#     ]
# }



回答3:


You can view the API documentation here: http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/Welcome.html

Or you can as well use aws-cli for your local DynamoDB installation: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.CLI.html#Tools.CLI.UsingWithDDBLocal




回答4:


aws.config.update({
 dynamodb: {
      endpoint: 'http://localhost:8000'
 },
}


来源:https://stackoverflow.com/questions/34984880/dynamodb-create-tables-in-local-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!