How should I update a field of a record in Amazon Timestream?

1.4k Views Asked by At

I am trying to update a field in a record in Timestream but it keeps inserting new record instead. Here is my code:

$dimensions= [];

$dimensions[] = [
 'Dimensions' => [
    [
        'DimensionValueType' => 'VARCHAR',
        'Name' => 'id',
        'Value' => '123456',
    ],
    [
        'DimensionValueType' => 'VARCHAR',
        'Name' => 'remark',
        'Value' => 'Remark test text',
    ],
  ],
];

$query = [
'CommonAttributes' => [
    'MeasureName' => 'table_cnt',
    'MeasureValue' => 'table_cnt',
    'MeasureValueType' => 'VARCHAR',
    'Time' => '1651501311000', 
    'TimeUnit' => 'MILLISECONDS',
    'Version' => 1,
],
'DatabaseName' => 'mydb',
'Records' => $dimensions,
'TableName' => 'table',
];

$db->WriteRecords($query);

and now here how I try to upsert the same record inserted above:

$dimensions= [];

$dimensions[] = [
 'Dimensions' => [
    [
        'DimensionValueType' => 'VARCHAR',
        'Name' => 'id',
        'Value' => '123456',
    ],
    [
        'DimensionValueType' => 'VARCHAR',
        'Name' => 'remark',
        'Value' => 'New Remark test text', // new text
    ],
  ],
];

$query = [
'CommonAttributes' => [
    'MeasureName' => 'table_cnt',
    'MeasureValue' => 'table_cnt',
    'MeasureValueType' => 'VARCHAR',
    'Time' => '1651501311000',      // same time 
    'TimeUnit' => 'MILLISECONDS',
    'Version' => 2,                 // changed to version 2 when upserting
],
'DatabaseName' => 'mydb',
'Records' => $dimensions,
'TableName' => 'table',
];

$db->WriteRecords($query);

I dont understand what am I doing wrong here, and the lack of documentations and samples is making it even more difficult to find out! Thanks for any advice!

1

There are 1 best solutions below

11
On BEST ANSWER

In Timestream, the dimensions together with the time are a "key" for upserting. If you have different dimensions, it will insert a new record with the new dimensions. The version field is only used if all dimensions and time are exactly the same. Then, the measures will be updated with the new values. You can find better information here: https://docs.aws.amazon.com/timestream/latest/developerguide/data-modeling.html#data-modeling-dimensionsmeasures, under the "Choosing dimensions", the first bullet point.