我知道这个问题有点长(很抱歉)。我认为详细的说明可以更好地说明我的情况。 我从流式端点接收设备数据。流中的每个有效负载都包含多个数据实体。我创建了以下要存储在发电机中的结构
db_pk = str(msg.get('key'))
db_sk = str(msg.get('sortKey'))
area_id = str(msg.get('area'))
## other entities in following mapped_values...
mapped_values = {
startTime: {
'alert_msg': alert,
'area ': area_id,
'speed': speed,
'state': state,
'startTime': startTime,
'duration': duration,
'inspector': inspector_id
}
}
# Data item
lt_item = {
'id': db_pk,
'serialNo': db_sk,
'value_map': [mapped_values]
}
我使用以下更新语句在每个有效负载中创建传入数据的映射。
table = dynamo_db.Table('device')
table.update_item(
Key = {
'id': db_pk,
'serialNo': db_sk
},
UpdateExpression = 'SET #value_map = list_append(#value_map, :mapped_values)',
ConditionExpression = 'attribute_exists(#value_map)',
ExpressionAttributeValues = {
':mapped_values': [mapped_values]
},
ExpressionAttributeNames = {
'#value_map': 'value_map'
}
)
上面的更新表达式在dynamo中的value_map
键下创建了一个消息映射,并且每当收到新消息时,都将其追加到该映射中,如下所示:
{
"id": "KD_125",
"serialNo": "KDCRT-231"
"value_map": [
{
"2019-05-29 14:36:03": { #first msg in payload
"alert_msg": "0x12 LOGICAL ERROR AT DIRECTION SELECT SIGNAL",
"area": "TX-112",
"speed": "65",
"startTime": "2019-05-29 14:36:03",
"state": "ACTIVE",
"duration": "None"
"inspector": "None"
}
},
{
"2019-05-29 14:36:03": { # second msg, same timestamp with first one
"alert_msg": "0x12 LOGICAL ERROR AT DIRECTION SELECT SIGNAL",
"area": "None",
"speed": "None",
"startTime": "2019-05-29 14:36:03",
"state": "PASSIVE",
"duration": "1200"
"inspector" "422TX19"
}
}
#another message with different timestamp and data values
]
}
某些消息是用相同的时间戳(startTime
)发送的。如果消息中有state='ACTIVE'
,则其中包含除 duration
和 inspector
之外的所有数据;如果是state='PASSIVE'
,则包括duration, inspector
和 area
和 speed
以外的其他字符(请参见上面的示例)。 / p>
如果在有效负载中发送了相同 timestamp
,则需要替换None
值(duration
和inspector
)和{在state
状态消息中使用'ACTIVE'
状态消息中的{1}}而不是使用相同的'PASSIVE'
创建两个对象。因此,最终形状应为:
timestamp
我尝试了不同的update expressions,例如{
"id": "KD_125",
"serialNo": "KDCRT-231"
"value_map": [
{
"2019-05-29 14:36:03": {
"alert_msg": "0x12 LOGICAL ERROR AT DIRECTION SELECT SIGNAL",
"area": "TX-112",
"speed": "65",
"startTime": "2019-05-29 14:36:03",
"state": "PASSIVE", # updated from PASSIVE state msg
"duration": "1200" # updated values from PASSIVE state msg
"inspector": "422TX19" # updated values from PASSIVE state msg
}
}
# other messages with different timestamps
]
}
。此外,通过
if_not_exists
option ..但到目前为止没有成功。我希望有人可以建议我进行此更新。.