aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

How to create global secondary index using dynamodb cli

Open XiSmartElf opened this issue 10 years ago • 13 comments

I think AWS really should have more and better documentation with good examples. There is no documentation example for doing this....

XiSmartElf avatar Mar 31 '16 08:03 XiSmartElf

Thanks for the feedback, we'll get some examples added.

jamesls avatar Mar 31 '16 22:03 jamesls

Can you provide an example for How to create global secondary index using dynamodb cli here? Thanks!

XiSmartElf avatar Apr 01 '16 01:04 XiSmartElf

No problem, can you clarify if you want to create a table with a GSI or add a GSI to an existing table?

jamesls avatar Apr 01 '16 23:04 jamesls

Create a table

XiSmartElf avatar Apr 06 '16 08:04 XiSmartElf

Hey jamesls can you provide an example for how to create local secondary index using dynamodb cli ?

975204 avatar Jun 16 '16 10:06 975204

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}"

krisdigitx avatar Jul 08 '16 14:07 krisdigitx

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

hariom12 avatar Jul 13 '16 14:07 hariom12

@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 avatar Jan 15 '18 09:01 Jezorko

@jezorko Is it possible to exclude assignedTo from the key-schema so that you can do a GetItem operation on the key alone?

jcoyne avatar Feb 07 '18 16:02 jcoyne

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.

fommil avatar Jul 25 '18 16:07 fommil

@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}"

sateeshvl avatar Nov 04 '19 10:11 sateeshvl

I got it. just ignore.

sateeshvl avatar Nov 04 '19 10:11 sateeshvl

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

kdaily avatar Oct 29 '20 16:10 kdaily