您好希望连续添加到一个子项目,该子项目最初被声明为空,之后将被更新。
这是代码-
dynamodb = boto3.resource('dynamodb',endpoint_url='http://localhost:8000')
# Creating the Table
table = dynamodb.create_table(
TableName='TableN',
KeySchema=[
{
'AttributeName': 'name',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'name',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 1000,
'WriteCapacityUnits': 1000
}
)
# Updating with the Initial Values
table = dynamodb.Table('TableN')
IntValues = {
"datapoints": [],
"attributes": {"host": "server1", "customer": "Ast1"},
"name": "B001_P001"
}
table.put_item(Item=IntValues)
现在,我想用一组值更新“数据点”-
datapoint1 = {"x1":1,"x2":"OK","x3":"23.123"}
datapoint2 = {"x1":3,"x2":"ON","x3":"56.123"}
datapoint3 = {"x1":5,"x2":"OFF","x3":"78.123"}
因此最终值应类似于-
{
"name" : "B001_P001",
"datapoints" : [
{
"x1" : 1,
"x2" : "OK",
"x3":"23.123"
},
{
"x1" : 1,
"x2" : "OK",
"x3":"23.123"
},
{
"x1" : 1,
"x2" : "OK",
"x3":"23.123"
}]
"attributes": {"host": "server1", "customer": "Ast1"}
}
如何在不删除或重新创建整个项目的情况下更新现有子项目?
答案 0 :(得分:0)
我已使用update_item更新每个数据点-
在使用put_item创建了有问题的初始更新后,我使用了update_item来更新属性。
我已经使用for循环来传递各个数据点-
ulist = datapoint1
table.update_item(
Key={'name':'TableN'},
UpdateExpression= 'SET #datapoints = list_append(#datapoints,:val1)',
ExpressionAttributeNames={'#datapoints': 'datapoints'},
ExpressionAttributeValues={':val1':ulist}
)