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.
DynamoDB Local is available as a download (requires JRE), as an Apache Maven dependency, or as a Docker image. If you prefer to use the Amazon DynamoDB web service instead, see Setting up DynamoDB (web service).
To access DynamoDB running locally, use the --endpoint-url parameter. The following is an example of using the AWS CLI to list the tables in DynamoDB on your computer. The AWS CLI can't use the downloadable version of DynamoDB as a default endpoint. Therefore, you must specify --endpoint-url with each AWS CLI command.
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 });
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" # ] # }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With