spring-data-dynamodb
spring-data-dynamodb copied to clipboard
Composite attributes as hash / range keys
Hi!
we'd like to use partition keys / range keys that do not correspond to only one attribute. AWS DynamoDb calls these "composite attributes" for the key.
Hash keys and range keys are showcased in that testcase: https://github.com/derjust/spring-data-dynamodb/blob/master/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/PlaylistId.java . But in the examples hash and range keys always map to one attribute.
But how would we make something like that work:
@DynamoDBHashKey
public String getHashKey() {
return invoiceNumber + randomsuffix;
}
@DynamoDBRangeKey
public String getRangeKey() {
return client + transactionid;
}
This is basically taken from the AWS documentation: https://aws.amazon.com/blogs/database/choosing-the-right-dynamodb-partition-key/
We'd need something similar, but we are not sure how to implement that. We could always fall back to the AWS library, but it would be nicer if we could leverage the awesome spring-data-dynamodb library :)
Thanks!
Hi! It's up to you to 'assemble' the Hash/Range value. There is also an wiki article about this topic.
It's totally up to you to have a values joint. Just ensure to not return null
and that both are stable (once created, the key/hash doesn't change anymore. Otherwise you end up with both entities in the table).
Obviously it highly depends on your scenario, but having a random suffix might cause problems: How do you know it if you want to get those entities? On the other hand if you have a pure CRUD application and never access the ID from the outside world that might be fine. On the other hand, it can be helpful that external calls can derive the hash/range key based on business values and you save yourself a scan operation. But as I said: Highly depending on your scenario.
Do you have any specific questions? spring-data-dynamodb just allows you to use DynamoDB with spring syntax - no magic included :)
Thanks for the answer! For now we switched to using the lower level AWS library. Seems we need more advanced features and then it feels a lot more natural using the lib directly and not through Spring-Data (setting consistency, controlling queries in a very fine grained way etc). In any case - keep up the great work!
Ah! And it is very cool that you are also using all aws annotations - that way it is very easy to switch between both worlds!
Just to expand on this - it would be nice if I could use a composite key as the hash key or range key, i.e. I have a requirement to have a range key w/ a DynamoDBTypeConverter annotation and I am not sure if there is anyway to do it.