I’m designing a DynamoDB table for storing property-related data where each property belongs to a category and subcategory.
I have three combination of primary key in sql server.
I need assistance in designing the table schema to efficiently query properties based on their Property_ID, categories_ID, and subcategories_ID.
Here are the attributes I have:
Property_Id (bigint)
Category_Id (int)
SubCategory_Id (int)
command which I have used for DynamoDB
aws dynamodb create-table
--table-name orders_table
--attribute-definitions AttributeName=Property_Id,AttributeType=N AttributeName=Category_Id,AttributeType=N AttributeName=country_code,AttributeType=N AttributeName=SubCategory_Id,AttributeType=N AttributeName=order_date,AttributeType=S
--key-schema AttributeName=Property_Id,KeyType=HASH AttributeName=Category_Id,KeyType=HASH AttributeName=SubCategory_Id,KeyType=RANGE
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
--global-secondary-indexes IndexName=OrderDateIndex,KeySchema=[{AttributeName=order_date,KeyType=HASH}],Projection={ProjectionType=ALL},ProvisionedThroughput={ReadCapacityUnits=5,WriteCapacityUnits=5}
--region us-east-1
An error occurred (ValidationException) when calling the CreateTable operation: 1 validation error detected: Value ‘[KeySchemaElement(attributeName=Property_Id, keyType=HASH), KeySchemaElement(attributeName=Category_Id, keyType=HASH), KeySchemaElement(attributeName=SubCategory_Id, keyType=RANGE)]’ at ‘keySchema’ failed to satisfy constraint: Member must have length less than or equal to 2
2
Answers
DynamoDB does not make composite keys for you, you must do so on the client side. That means you must declare your partition/sort keys as strings.
If you wanted a single composite partition key containing these 3 attributes
Property_ID, categories_ID, and subcategories_ID
then you would need to do the following:Now its up to you to create the value
PK
for each of your items, by combining the 3 attributes you require as your compound key.In DynamoDB you can have a single property of your object designated as the partition (or hash) key and a single property designed as a sort (or range) key. This means you must choose 2 properties that will play these roles. The error is raised because you are trying to use 3 properties.
One way you could handle this situation is to combine 2 of your 3 properties into one e.g. combine
Category_Id
andSubCategory_Id
into aCategory_Id_And_SubCategory_Id
property (by joining these 2 values into a string, separated by-
for example) then use this property as the sort key.You could also save the
Category_Id
andSubCategory_Id
as separate fields of your object so you can access them easily, without having to do any sort of string parsing to get your ids. In the end the schema of an object in your DynamoDB database would look similar to:This kind of schema will allow you to query your database in the following way:
startswith
condition on the sort key)If you need to query the table in different ways you can change around which values you use as keys e.g. you could use as the partition key the category id which would allow you to query all items for a category and then have the sort key be the concatenation between subcategory id and property id.