我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
我们如何根据索引更新dynamodb表(不基于primary has和range键)。
我有一个名为key_id-index
,hash是asset_id
,range是hit_id
。 我想基于key_id-index
更新表,因为我不知道更新时的那些。var paramsu = { TableName: 'asset', //IndexName: 'key_id-index', Key: { // The primary key of the item (a map of attribute name to AttributeValue) asset_id: { S: 'a' }, hit_id: { S: 'h' } // more attributes... }, AttributeUpdates: { // The attributes to update (map of attribute name to AttributeValueUpdate) key_id: { Action: 'PUT', // PUT (replace) // ADD (adds to number or set) // DELETE (delete attribute or remove from set) Value: { S: 'updated1' } } // more attribute updates: ... }, ReturnValues: 'NONE', // optional (NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW) ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES) ReturnItemCollectionMetrics: 'NONE' // optional (NONE | SIZE) }; db.updateItem(paramsu, function(err, data) { if (err) console.log(err); // an error occurred else console.log(data); // successful response });
how can we update dynamodb table based on index(not based on primary has and range key).
I have a index created by namekey_id-index
and hash isasset_id
and range ishit_id
. i want to update the table based onkey_id-index
because i wouldn't know those while updating.var paramsu = { TableName: 'asset', //IndexName: 'key_id-index', Key: { // The primary key of the item (a map of attribute name to AttributeValue) asset_id: { S: 'a' }, hit_id: { S: 'h' } // more attributes... }, AttributeUpdates: { // The attributes to update (map of attribute name to AttributeValueUpdate) key_id: { Action: 'PUT', // PUT (replace) // ADD (adds to number or set) // DELETE (delete attribute or remove from set) Value: { S: 'updated1' } } // more attribute updates: ... }, ReturnValues: 'NONE', // optional (NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW) ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES) ReturnItemCollectionMetrics: 'NONE' // optional (NONE | SIZE) }; db.updateItem(paramsu, function(err, data) { if (err) console.log(err); // an error occurred else console.log(data); // successful response });
最满意答案
你不能写入GSI。
您唯一的选择是首先从GSI读取,获取作为键的属性值(它们始终投影到索引上,请参阅GSI.Projections ),然后将数据写回表中。
You cannot write to a GSI.
Your only option is to first read from the GSI, get the attribute values that are are your keys (they are always projected onto the index, see GSI.Projections), and then write your data back to the table.
相关问答
更多-
你不能写入GSI。 您唯一的选择是首先从GSI读取,获取作为键的属性值(它们始终投影到索引上,请参阅GSI.Projections ),然后将数据写回表中。 You cannot write to a GSI. Your only option is to first read from the GSI, get the attribute values that are are your keys (they are always projected onto the index, see GSI.Pro ...
-
不幸的是,5个本地二级索引和5个全局二级索引是硬限制,无法扩展。 当您有更多(> 5)属性要从DynamoDB查询时,它无法有效实现。 您必须使用扫描来评估表中的所有项目,这是无效的。 您必须移动到其他数据库或使用AWS ElasticSearch索引搜索属性。 Unfortunately, 5 local secondary indexes and 5 global secondary indexes are hard limits which are not possible to extend. Wh ...
-
DynamoDB最佳实践,用于查询具有较少值的属性(DynamoDB best practice for querying attribute with few values)[2022-05-29]
我不会选择扫描,因为除非您的订单很少,否则它不具有成本效益或特别有效。 简而言之,您在全球二级索引中处于正确的轨道上。 (我假设你在谈论全球二级索引。还有本地二级索引,但我不知道这些对这个案例有多大帮助。 无论如何,我会创建一个GSI,其中OrderStatus作为Hash键,OrderId作为Range键。 但是,有几件事你需要注意。 1)写吞吐量。 请记住,具有相同OrderStatus的订单将写入GSI上的同一磁盘。 这就是Dynamo的工作方式,使用相同Hash密钥的文档会转到同一个地方。 这意味着 ... -
根据你的问题,我会说你如何编写你的哈希键并不重要,因为你必须使用该哈希键的确切值来查询你的表,而DynamoDB无论如何都会将它视为一个字符串。 另一件事是如果你正在编写一个范围键,那么你可能想要按如下方式编写它 john.doe_1382465533 所以你可以像这样轻松查询你的表格 hash key = whatever,range key> = john.doe_1382460000 也就是说,也许你可以通过将它直接集成到DynamoDB来摆脱你的Redis活动源,如下所示: 哈希键:用户ID 范围键 ...
-
查询DynamoDB(Querying DynamoDB)[2022-05-27]
在DynamoDB中,您可以获得带有3个API的项目: 。 扫描 (灵活但昂贵), 。 查询 (不够灵活:你必须指定一个散列,但较便宜) 。 GetItem (通过哈希表,如果你的表有一个范围) 实现你想要的唯一方法是: 使用扫描,速度慢或昂贵。 使用另一个表(B)作为前一个(A)的索引,如: B.HASH ='VALUES'B.RANGE = userid B.lastAccesTime = lastAccesTime(带有二级索引) 现在,您必须在写入时保持该索引,但您可以将其与Query操作一起使用, ... -
是的,您无法使用DynamoDb获得一系列的hash_key。 但这并不意味着你坚持使用你的用例。 我们来看'日期'用例,并说你正在构建一个日志应用程序。 你很可能每天都会得到很多记录。 如果您将当天用作hash_key,则可以将完整的时间戳作为range_key。 这样,你可以将你的查询拆分成块,并得到你想要的。 当然,要获得最佳结果,您需要很好地了解这种查询。 例如,典型的范围是什么? 使用DynamoDb以及其他关键值存储,大多数情况下,大多数情况下您的数据都是在查询的基础上进行建模,而不像SQL仅在 ...
-
它不起作用的原因是本地二级索引中的键必须与表具有相同的分区键。 因此,就您的情况而言,您的本地二级索引必须将messageId作为其HASH键,将room和userId作为RANGE键放在其各自的索引上。 并且由于你的表已经被(messageId, userId)键入(messageId, userId)所以你不需要userId本地二级索引。 (请参阅https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html )。 这种 ...
-
动机注意:使用类似DynamoDB的云存储时,我们必须了解存储模型,因为这会直接影响您的性能,可伸缩性和财务成本。 这与使用本地数据库不同,因为您不仅支付您存储的数据,还支付您对数据执行的操作。 例如,删除记录是WRITE操作,因此如果您没有有效的清理计划(并且您的情况是时间序列数据特别需要),您将支付价格。 在处理小数据量时,您的数据模型不会显示问题,但在您需要扩展时肯定会破坏您的计划。 也就是说,诸如创建(或不创建)索引,为键定义适当属性,创建表分段等决策将使整个过程变得不同。 选择DynamoDB(或 ...
-
给出一些假设,这是一个提案: 假设订阅ID是全球唯一的 假设您需要能够根据其订阅ID检索订阅 假设日期可以表示为数字(例如Julian Day Number) 表设计: 哈希键:SubscriptionID 其他属性CompanyID:S,StartDate:N,EndDate:N,ProductID:S 全球二级指数: ProductID-StartDate-Index:ProductID上的哈希值,StartDate上的范围 CompanyID-ProductID-index:CompanyID上的哈希 ...
-
使用Hash + Range键在DynamoDB中存储CloudWatch警报(Using Hash+Range key for storing CloudWatch alarm in DynamoDB)[2023-01-20]
根据您的查询模式,您看起来正确。 如果只需要一个account_id数据,请执行Query 。 您还可以在range-key上提供KeyConditionExpression ,以仅获取在给定timestamp之后发生的事件。 如果要获取account_id列表的数据,请运行“ Scan 。 (您不能执行BatchGetItem ,因为它需要hash-key和range-key)。 As per your query patterns, you approach looks correct. If you ...