英文:
DynamoDB: Query items where a field has specific type
问题
我有一个在DynamoDB中的旧表,这个表包含一个非键字段 term
,通常是一个 Number
,但在某些情况下它是一个包含数字的 String
。
我想查询所有具有 String
值的该字段的项目,这似乎是一个相当复杂的任务。
我尝试了 NOT Contains (term, {S: "a"})
,它返回所有具有数字和字符串的项目。
唯一有效的解决方案是使用:
begins_with(term, {S: "0"} OR
begins_with(term, {S: "1"} OR
...
begins_with(term, {S: "9"}
我认为这不是最好的解决方案。
也许有人知道更简洁的解决方案?
英文:
I have a legacy table in DynamoDB, and this table contains a non-key field term
which is usually a Number
, but in some cases it is a String
containing a number
I want to query all items having this field a String
value, and it seems to be a pretty complex task
I tried NOT Contains (term, {S: "a"})
and it returns all items with Numbers and Strings
The only solution which works is to use
begins_with(term, {S: "0"} OR
begins_with(term, {S: "1"} OR
...
begins_with(term, {S: "9"}
I suppose that this is not the best solution
Maybe someone knows cleaner solution?
答案1
得分: 1
你应该使用过滤函数attribute_type
:
attribute_type(term, "S")
英文:
You should use the filter function attribute_type
:
attribute_type(term, "S"}
答案2
得分: 1
表有多大?你找到字符串值的动机是什么?
正如Lee所说,你可以使用filter函数,但要注意,在幕后,你正在执行对整个表的完全扫描,而不是查询。因此,根据表的大小,这可能是不可取的。不幸的是,考虑到你的问题的参数,我没有看到任何避免执行扫描的方法。
如果这是对一个大表的一次性作业,我建议设置一个Lambda或ECS任务,以慢慢扫描表并分批处理项目。可以使用LastEvaluatedKey来跟踪进度。这样做的好处是,在此过程中,你可以将字符串更新为数字(我猜这可能是你的动机)。
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
英文:
How big is the table? What's your motivation for finding the string values?
As Lee said you could use the filter function, but be aware that behind the scenes you're performing a full scan of the table, not a query. So depending on the size of your table this may be undesirable. Unfortunately, I don't see any way around performing a scan given the parameters of your problem.
If this is a one time job on a large table, I'd opt to set up a Lambda or ECS task to slowly scan through the table and process the items in batches. Progress can be kept using a LastEvaluatedKey. Upside of this is that you can update the strings to be numbers while you do this (which I suspect may be your motiviation)
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论