Difference between revisions of "Dynamodb"
From John Freier
(Created page with "DynamoDB is an AWS product that has a stand alone app for development but is really suppose to be built out in AWS where they handle the administration/OPS. This is a NoSQL d...") |
|||
| Line 2: | Line 2: | ||
This is a NoSQL database. | This is a NoSQL database. | ||
| + | |||
| + | == Partition Key and Sort Key == | ||
| + | There are two types of keys that can be used to define a primary key. | ||
| + | |||
| + | Partition keys are the how the database separates data in the backend. | ||
| + | |||
| + | Sort keys allow you to sort data by a secondary column. | ||
| + | |||
| + | If more sorting is required, take a look at using Local Global Index. | ||
| + | |||
| + | == Secondary Global Index == | ||
| + | This allows you to create a super table from the base table, to allow for different keys and sorting when using the `query` command. | ||
| + | |||
| + | Example | ||
| + | var AWS = require("aws-sdk"); | ||
| + | AWS.config.update({ | ||
| + | region: "local", | ||
| + | endpoint: "http://localhost:8000" | ||
| + | }); | ||
| + | var dynamodb = new AWS.DynamoDB(); | ||
| + | var params = { | ||
| + | TableName: "statements", | ||
| + | AttributeDefinitions: [{ | ||
| + | AttributeName: "id", | ||
| + | AttributeType: "S" | ||
| + | }, | ||
| + | { | ||
| + | AttributeName: "time", | ||
| + | AttributeType: "N" | ||
| + | }, | ||
| + | { | ||
| + | AttributeName: "userId", | ||
| + | AttributeType: "S" | ||
| + | } | ||
| + | ], | ||
| + | KeySchema: [{ | ||
| + | AttributeName: "id", | ||
| + | KeyType: "HASH" | ||
| + | }, //Partition key | ||
| + | { | ||
| + | AttributeName: "userId", | ||
| + | KeyType: "RANGE" | ||
| + | }, //Sort key | ||
| + | ], | ||
| + | GlobalSecondaryIndexes: [{ | ||
| + | IndexName: 'statement_user_index', | ||
| + | KeySchema: [{ | ||
| + | AttributeName: 'userId', | ||
| + | KeyType: 'HASH', | ||
| + | }, | ||
| + | { | ||
| + | AttributeName: 'time', | ||
| + | KeyType: 'RANGE', | ||
| + | } | ||
| + | ], | ||
| + | Projection: { | ||
| + | ProjectionType: 'ALL' | ||
| + | }, | ||
| + | ProvisionedThroughput: { | ||
| + | WriteCapacityUnits: 5, | ||
| + | ReadCapacityUnits: 5 | ||
| + | } | ||
| + | }], | ||
| + | ProvisionedThroughput: { | ||
| + | ReadCapacityUnits: 5, | ||
| + | WriteCapacityUnits: 5 | ||
| + | } | ||
| + | }; | ||
| + | dynamodb.createTable(params, function(err, data) { | ||
| + | if (err) { | ||
| + | console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); | ||
| + | } else { | ||
| + | console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2)); | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | |||
| + | == Local Global Index == | ||
| + | Local global index, creates a super table on top of the base table to allow for more sorting abilities. | ||
Revision as of 11:58, 5 January 2023
DynamoDB is an AWS product that has a stand alone app for development but is really suppose to be built out in AWS where they handle the administration/OPS.
This is a NoSQL database.
Partition Key and Sort Key
There are two types of keys that can be used to define a primary key.
Partition keys are the how the database separates data in the backend.
Sort keys allow you to sort data by a secondary column.
If more sorting is required, take a look at using Local Global Index.
Secondary Global Index
This allows you to create a super table from the base table, to allow for different keys and sorting when using the `query` command.
Example
var AWS = require("aws-sdk");
AWS.config.update({
region: "local",
endpoint: "http://localhost:8000"
});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName: "statements",
AttributeDefinitions: [{
AttributeName: "id",
AttributeType: "S"
},
{
AttributeName: "time",
AttributeType: "N"
},
{
AttributeName: "userId",
AttributeType: "S"
}
],
KeySchema: [{
AttributeName: "id",
KeyType: "HASH"
}, //Partition key
{
AttributeName: "userId",
KeyType: "RANGE"
}, //Sort key
],
GlobalSecondaryIndexes: [{
IndexName: 'statement_user_index',
KeySchema: [{
AttributeName: 'userId',
KeyType: 'HASH',
},
{
AttributeName: 'time',
KeyType: 'RANGE',
}
],
Projection: {
ProjectionType: 'ALL'
},
ProvisionedThroughput: {
WriteCapacityUnits: 5,
ReadCapacityUnits: 5
}
}],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
Local Global Index
Local global index, creates a super table on top of the base table to allow for more sorting abilities.