11

How can I create in DyanamoDB a table with AttributeType Set, Map or JSON. I don't want to create the table structure by insertion (PutItem) or update of data because I need to create an index that will contains the Map, List or JSON attribute in the projection. I need to do it a creation time (CreateTable). I also prefer to use AWS CLI. Below a sample:

{
TableName : "Music",
KeySchema: [       
    { 
        AttributeName: "Artist", 
        KeyType: "HASH", //Partition key
    }
],
AttributeDefinitions: [
    { 
        AttributeName: "Artist", 
        AttributeType: "S" 
    },
    { 
        AttributeName: "instruments" 
        AttributeType: // Map or List or JSON type 
    }

],

... }

1

1 Answer 1

10

While creating the DynamoDB table, you can define only the attributes that are part of the key definitions. In other words, you can define the hash key and sort key attributes only when you create the DynamoDB table. The hash and sort key attribute must be a scalar attribute.

The document and set data types cannot be part of the key attribute. Also, you can't create the index on these attribute types.

Scalar Types – A scalar type can represent exactly one value. The scalar types are number, string, binary, Boolean, and null.

Document Types – A document type can represent a complex structure with nested attributes—such as you would find in a JSON document. The document types are list and map.

Set Types – A set type can represent multiple scalar values. The set types are string set, number set, and binary set.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, thank you for you anwser. It is possible to create table attributes with AttributeDefinition (in java for example ) new AttributeDefinition("year", ScalarAttributeType.N),new AttributeDefinition("title", ScalarAttributeType.S)). Also I'am talking about create indexes with scalar fields and with Map, List, JSON fields in the projections. Not in the index field.
they are key attributes. See you have used KeySchemaElement and Key type Hash and Partition. If you try to add the third attribute outside key definition, you would get error.
Ok. This is indeed the case. Thanks
So that is means that you need to insert data first before you can create the indexes ?
No, you can create index while creating the table as well. If you need any other attribute (non key attributes in main table) in index, you can define those attributes. You wouldn't get error for that. Would you like to accept the answer if it was helpful?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.