DynamoDB:查询具有特定类型的字段的项目。

huangapple go评论68阅读模式
英文:

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")

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions

英文:

You should use the filter function attribute_type:

attribute_type(term, "S"}

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions

答案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

huangapple
  • 本文由 发表于 2023年8月9日 00:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定