We create tables in DynamoDB to store and organize data in the cloud. Tables help keep data safe and easy to find.
0
0
Table creation with AWS SDK in DynamoDB
Introduction
When you want to save user information for a web app.
When you need to store product details for an online store.
When you want to keep track of orders or transactions.
When you need a fast and scalable database for your app.
When you want to organize data by categories or keys.
Syntax
DynamoDB
const AWS = require('aws-sdk'); AWS.config.update({ region: 'us-east-1' }); const dynamodb = new AWS.DynamoDB(); const params = { TableName: 'YourTableName', KeySchema: [ { AttributeName: 'PrimaryKey', KeyType: 'HASH' } ], AttributeDefinitions: [ { AttributeName: 'PrimaryKey', AttributeType: 'S' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }; dynamodb.createTable(params, function(err, data) { if (err) console.log(err); else console.log('Table created:', data); });
KeySchema defines the primary key for the table.
ProvisionedThroughput sets how much read and write capacity the table has.
Examples
This example creates a table named 'Users' with 'UserId' as the primary key of type string.
DynamoDB
const params = {
TableName: 'Users',
KeySchema: [
{ AttributeName: 'UserId', KeyType: 'HASH' }
],
AttributeDefinitions: [
{ AttributeName: 'UserId', AttributeType: 'S' }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};This example creates a table named 'Orders' with a composite key: 'OrderId' as the partition key and 'OrderDate' as the sort key.
DynamoDB
const params = {
TableName: 'Orders',
KeySchema: [
{ AttributeName: 'OrderId', KeyType: 'HASH' },
{ AttributeName: 'OrderDate', KeyType: 'RANGE' }
],
AttributeDefinitions: [
{ AttributeName: 'OrderId', AttributeType: 'S' },
{ AttributeName: 'OrderDate', AttributeType: 'S' }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 5
}
};Sample Program
This program creates a DynamoDB table named 'Books' with 'ISBN' as the primary key. It sets read and write capacity to 5 units each.
DynamoDB
const AWS = require('aws-sdk'); AWS.config.update({ region: 'us-east-1' }); const dynamodb = new AWS.DynamoDB(); const params = { TableName: 'Books', KeySchema: [ { AttributeName: 'ISBN', KeyType: 'HASH' } ], AttributeDefinitions: [ { AttributeName: 'ISBN', AttributeType: 'S' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }; dynamodb.createTable(params, function(err, data) { if (err) { console.log('Error:', err.message); } else { console.log('Table created:', data.TableDescription.TableName); } });
OutputSuccess
Important Notes
Make sure your AWS credentials and region are set correctly before running the code.
Table creation can take a few seconds; the callback confirms when it's ready.
Use meaningful table and key names to keep your data organized.
Summary
DynamoDB tables store data with a primary key to organize it.
Use AWS SDK to create tables by defining name, keys, and capacity.
Check the output to confirm the table was created successfully.