I think AWS really should have more and better documentation with good examples. There is no documentation example for doing this....
Thanks for the feedback, we'll get some examples added.
Can you provide an example for How to create global secondary index using dynamodb cli here? Thanks!
No problem, can you clarify if you want to create a table with a GSI or add a GSI to an existing table?
Create a table
Hey jamesls can you provide an example for how to create local secondary index using dynamodb cli ?
aws dynamodb --region=eu-west-1 create-table --endpoint-url http://localhost:8000 --table-name mytable --attribute-definitions AttributeName=key,AttributeType=S AttributeName=assignedTo,AttributeType=S AttributeName=roles,AttributeType=S --key-schema AttributeName=key,KeyType=HASH AttributeName=assignedTo,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 --global-secondary-indexes IndexName=Index,KeySchema=["{AttributeName=assignedTo,KeyType=HASH}","{AttributeName=roles,KeyType=RANGE}"],Projection="{ProjectionType=INCLUDE ,NonKeyAttributes=["key"]}",ProvisionedThroughput="{ReadCapacityUnits=10,WriteCapacityUnits=10}"
Example to create a global secondary index as below:-
var params = {
TableName: "General_Tab",
AttributeDefinitions:[
{AttributeName: "DATASOURCE", AttributeType: "S"},
{AttributeName: "event_time", AttributeType: "N"}
],
GlobalSecondaryIndexUpdates: [
{
Create: {
IndexName: "DATASOURCE_event_timeIndex",
KeySchema: [
{AttributeName: "DATASOURCE", KeyType: "HASH"}, //Partition key
{AttributeName: "event_time", KeyType: "RANGE"}, //Sort key
],
Projection: {
"ProjectionType": "ALL"
},
ProvisionedThroughput: {
"ReadCapacityUnits": 1,"WriteCapacityUnits": 1
}
}
}
]
};
dynamodb.updateTable(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
@krisdigitx a little more readable version:
aws dynamodb create-table \
--region=eu-west-1 \
--endpoint-url http://localhost:8000 \
--table-name mytable \
--attribute-definitions \
AttributeName=key,AttributeType=S \
AttributeName=assignedTo,AttributeType=S \
AttributeName=roles,AttributeType=S \
--key-schema \
AttributeName=key,KeyType=HASH \
AttributeName=assignedTo,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 \
--global-secondary-indexes IndexName=Index,\
KeySchema=["{AttributeName=assignedTo,KeyType=HASH}","{AttributeName=roles,KeyType=RANGE}"],\
Projection="{ProjectionType=INCLUDE ,NonKeyAttributes=["key"]}",\
ProvisionedThroughput="{ReadCapacityUnits=10,WriteCapacityUnits=10}"
Of course the whole KeySchema
has to be a single-string-parameter...
@jezorko Is it possible to exclude assignedTo
from the key-schema
so that you can do a GetItem
operation on the key
alone?
I wasn't able to figure out how to add a secondary global index with update-table
. I had to create a JSON file and load that. The documentation doesn't even have a "shorthand" example and the shorthand I created failed to parse, although it was exactly the same as the JSON.
@krisdigitx a little more readable version:
aws dynamodb create-table \ --region=eu-west-1 \ --endpoint-url http://localhost:8000 \ --table-name mytable \ --attribute-definitions \ AttributeName=key,AttributeType=S \ AttributeName=assignedTo,AttributeType=S \ AttributeName=roles,AttributeType=S \ --key-schema \ AttributeName=key,KeyType=HASH \ AttributeName=assignedTo,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 \ --global-secondary-indexes IndexName=Index,\ KeySchema=["{AttributeName=assignedTo,KeyType=HASH}","{AttributeName=roles,KeyType=RANGE}"],\ Projection="{ProjectionType=INCLUDE ,NonKeyAttributes=["key"]}",\ ProvisionedThroughput="{ReadCapacityUnits=10,WriteCapacityUnits=10}"
Of course the whole
KeySchema
has to be a single-string-parameter...
Hi, Thanks for example. I am trying to create a table with two global secondary indexes and I keep getting below error.
An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Some AttributeDefinitions are not used. AttributeDefinitions: [tt, key, assignedTo, roles], keys used: [tt, key, assignedTo]
My create table command.
aws dynamodb create-table \
--region=eu-west-2 \
--table-name test \
--attribute-definitions \
AttributeName=key,AttributeType=S \
AttributeName=assignedTo,AttributeType=S \
AttributeName=roles,AttributeType=S \
AttributeName=tt,AttributeType=S \
--key-schema \
AttributeName=key,KeyType=HASH \
AttributeName=assignedTo,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--global-secondary-indexes IndexName=Index,\
KeySchema=["{AttributeName=roles,KeyType=HASH}"],\
Projection="{ProjectionType=INCLUDE ,NonKeyAttributes=["key"]}",\
ProvisionedThroughput="{ReadCapacityUnits=1,WriteCapacityUnits=1}",\
--global-secondary-indexes IndexName=Index2,\
KeySchema=["{AttributeName=tt,KeyType=HASH}"],\
Projection="{ProjectionType=INCLUDE ,NonKeyAttributes=["key"]}",\
ProvisionedThroughput="{ReadCapacityUnits=1,WriteCapacityUnits=1}"
I got it. just ignore.
Hello all, the AWS DynamoDB documentation has been updated with an example for this use case.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GCICli.html
Most helpful comment
@krisdigitx a little more readable version:
Of course the whole
KeySchema
has to be a single-string-parameter...