问题描述
我有一个带有主哈希键和范围键的DynamoDB表.范围键将具有两个属性.假设这些属性名称为:name1, name2
,其值为value1, value2
I have a DynamoDB table with a primary hash key, and a range key. Range key will have two attributes. Say those attribute names are: name1, name2
, with values value1, value2
方案A :将两个属性组合为字符串,使用逗号作为定界符
主哈希键:id
范围键:value1,value2
Plan A: combine two attributes as string, use comma as delimiter
Primary hash key: id
Range key: value1,value2
缺点
1.如果某些有线值包含此定界符,则逗号可能不起作用
Cons
1. comma may not work if some wired values contain this delimiter
B计划:将地图转换为范围键的字符串
主哈希键:id
范围键:{\name1\: \value1\, \name2\: \value2\}
Plan B: convert map as String for range key
Primary hash key: id
Range key: "{\"name1\": \"value1\", \"name2\": \"value2\"}"
缺点
1.不同的SDK可能会基于相同的值生成不同的JSON字符串? (不确定),需要支持多个SDK读/写.像Java和Ruby
Cons
1. different SDK may result into different JSON String based on the same value? (Not sure), need to support multiple SDK read/write. Like Java and Ruby
那么,哪种解决方案效果更好?还是有更好的建议?
So, which solution works better? Or there are any better suggestions?
谢谢!雷
推荐答案
您处在正确的轨道上.有关关键设计的AWS文档会提出您的第一个建议,但同时也会对您所说的不利情况提出一些警告.
You're on the right track. The AWS docs regarding key design promote your first suggestion, but it also has some warnings about the situation that you refered as cons.
我不认为您可能会在使用不同的sdk解析器时遇到问题,但我也认为此处采取一些预防措施是一个很好的想法.因此,我不是使用sdk直接将json解析为字符串,而是使用自定义函数手动将值连接起来以生成确定性值,例如"name1-value1-name2-value2
"或"name1:value1-name2:value2
".
I don't think that you could have problemas with different sdk parsers, but I also think that a little bit of precautions here would be a good ideia. So instead of directly parse a json to string using the sdk, I would manually concatenate the values using a custom function to generate a deterministic value like "name1-value1-name2-value2
" or "name1:value1-name2:value2
".
这篇关于如何设置DynamoDB范围键,字符串或映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!