dynamodb UpdateItem using API Gateway as Proxy, error - amazon-dynamodb

I am using the AWS API Gateway as proxy to expose DynamoDB API. the UpdateItem mapping is not working, it return:
{
"__type": "com.amazon.coral.service#SerializationException"
}
Below is the mapping template and the payload:
{
"TableName": "registros",
"Key": {
"Pk": {
"S": "$input.path('$.Pk')"
},
"Sk": {
"S": "$input.path('$.Sk')"
}
},
"UpdateExpression": "set intenciones = :intenciones",
"ConditionExpression": "Pk = :Pk, Sk = :Sk,",
"ExpressionAttributeValues": {
":Pk": {"S": "$input.path('$.Pk')"},
":Sk": {"S": "$input.path('$.Sk')"},
":intenciones": {"S": "$input.path('$.intenciones')"},
},
"ReturnValues": "UPDATED_NEW"
}
Payload:
{
"Pk":"DemandaProd",
"Sk":"raerytre#22",
"intenciones":"news"
}

You don't need ConditionExpression since it's implied with Key that you'll be matching on the Pk and Sk. Try this:
{
"TableName": "registros",
"Key": {
"Pk": {
"S": "$input.path('$.Pk')"
},
"Sk": {
"S": "$input.path('$.Sk')"
}
},
"UpdateExpression": "set intenciones = :intenciones",
"ExpressionAttributeValues": {
":intenciones": { "S": "$input.path('$.intenciones')" }
},
"ReturnValues": "UPDATED_NEW"
}

Related

ConditionalCheckFailedException: The conditional request failed (Nested field)

I have a DDB entry as below
RequestId:'1234567890' // Partition key
LastScanDateTime: '2023-01-12T11:00:00.111Z'
SubjectUpdateCounter: {
Maths: 1,
English: 1
}
I'm trying to update the entry and below is the update query.
{
"TableName": "EmpDetails",
"Key": {
"RequestId": {
"S": "1234567890"
}
},
"ConditionExpression": "(#subjectUpdateCounter > :subjectUpdateCounterLimit)",
"UpdateExpression": "ADD #subjectUpdateCounter :dec SET LastScanDateTime = :LastScanDateTime,",
"ExpressionAttributeValues": {
":dec": {
"N": "-1"
},
":LastScanDateTime": {
"S": "2023-02-13T18:14:52.143Z"
},
":subjectUpdateCounterLimit": {
"N": "0"
}
},
"ReturnValues": "NONE",
"ExpressionAttributeNames": {
"#subjectUpdateCounter": "SubjectUpdateCounter.Maths"
}
}
Getting below error
ConditionalCheckFailedException: The conditional request failed
....
....
'$fault': 'client',
'$metadata': {
httpStatusCode: 400,
requestId: '12345ygfsdfagagdf',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
__type: 'com.amazonaws.dynamodb.v20120810#ConditionalCheckFailedException'
}
My current value in SubjectUpdateCounter.Maths is greater than 0, so the condition should succeed and this query should decrement the value of SubjectUpdateCounter.Maths to 0.
Why is the query throwing the above exception?
Your issue is here:
"ExpressionAttributeNames": {
"#subjectUpdateCounter": "SubjectUpdateCounter.Maths"
}
This means DynamoDB is looking for an attribute named "SubjectUpdateCounter.Maths" but there is none, as its a nested value you are looking for.
Your request should look like the following:
{
"TableName": "EmpDetails",
"Key": {
"RequestId": {
"S": "1234567890"
}
},
"ConditionExpression": "(#subjectUpdateCounter.#maths > :subjectUpdateCounterLimit)",
"UpdateExpression": "ADD #subjectUpdateCounter.#maths :dec SET LastScanDateTime = :LastScanDateTime,",
"ExpressionAttributeValues": {
":dec": {
"N": "-1"
},
":LastScanDateTime": {
"S": "2023-02-13T18:14:52.143Z"
},
":subjectUpdateCounterLimit": {
"N": "0"
}
},
"ReturnValues": "NONE",
"ExpressionAttributeNames": {
"#subjectUpdateCounter": "SubjectUpdateCounter",
"#maths":"Maths"
}
}

AWS Console Dynamodb scan search payload attribute

when I scan the aws dynamodb with the attribute name as "Asset_Name" string equals "MakingofaMusicVideo_HD", I am not getting any hits. But the payload contains the field. What's the best way to search for quick search.
Are there any other tools or projects which has a ux that enables users to search quickly?
"Asset_Name": {"S": "MakingofaMusicVideo_HD"}
Below is the payload in dynamodb
"payload": {
"M": {
"CableLabArray": {
"M": {
"ADI": {
"M": {
"Metadata": {
"M": {
"__custom:APP_DATA:2": {
"M": {
"_attributes": {
"M": {
"App": {
"S": "MOD"
},
"Value": {
"S": "CableLabsVOD1.1"
},
"Name": {
"S": "Metadata_Spec_Version"
}
}
}
}
},
"AMS": {
"M": {
"_attributes": {
"M": {
"Asset_ID": {
"S": "XXXXXXXXXXXXXXXXX"
},
"Version_Minor": {
"S": "1"
},
"Description": {
"S": "Making of a Music Video"
},
"Provider_ID": {
"S": "hs90s.ca"
},
"Creation_Date": {
"S": "2020-04-05"
},
"verb": {
"S": "EMPTY"
},
"Product": {
"S": "MOD"
},
"Version_Major": {
"S": "1"
},
"Asset_Name": {
"S": "MakingofaMusicVideo_HD"
},
"Asset_Class": {
"S": "package"
},
"Provider": {
"S": "HS90s"
}
}
}
}
}```
While DynamoDb supports storing complex attributes (e.g. maps, lists, sets, any combination of these), it does not offer search capabilities across complex attributes. You either need to store it as a top level attribute, or work it into the primary key.

Filter Expression based on a nested object DynamoDB AppSync

I'm trying to filter out a query based on a nested object (no array). I'm currently using AppSync and DynamoDB and the expression with expression values are executed correctly. But the filtering doesn't seem to work.
This is the sample data I'm trying to get (Filter by indicator.id):
Here's my query:
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "pk = :pk and begins_with(sk, :sk)",
"expressionValues": {
":pk": { "S": "tenant:5fc30406-346c-42e2-8083-fda33ab6000a" },
":sk": {
"S": "school-year:2019-2020:grades:bVgA9abd:subject:m_kpc1Ae6:indicator:"
}
}
},
"filter": {
"expression": " contains(#indicatorId, :sk1) or contains(#indicatorId, :sk2) or contains(#indicatorId, :sk3)",
"expressionNames": { "#indicatorId": "indicator" },
"expressionValues": {
":sk1": {
"M": { "id": { "S": "07c658dd-999f-4e6f-95b8-c6bae422760a" } }
},
":sk2": {
"M": { "id": { "S": "0cf9f670-e284-4a93-b297-5e4a40c50228" } }
},
":sk3": { "M": { "id": { "S": "cd7902be-6512-4b47-b29d-40aff30c73e6" } } }
}
}
}
I've also tried:
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "pk = :pk and begins_with(sk, :sk)",
"expressionValues": {
":pk": { "S": "tenant:5fc30406-346c-42e2-8083-fda33ab6000a" },
":sk": {
"S": "school-year:2019-2020:grades:bVgA9abd:subject:m_kpc1Ae6:indicator:"
}
}
},
"filter": {
"expression": " contains(#indicatorId, :sk1) or contains(#indicatorId, :sk2) or contains(#indicatorId, :sk3)",
"expressionNames": { "#indicatorId": "indicator.id" },
"expressionValues": {
":sk1": { "S": "07c658dd-999f-4e6f-95b8-c6bae422760a" },
":sk2": { "S": "0cf9f670-e284-4a93-b297-5e4a40c50228" },
":sk3": { "S": "cd7902be-6512-4b47-b29d-40aff30c73e6" }
}
}
}
I've also tried searching around StackOverflow, and Amazon forums and haven't found it directly to my problem:
How to filter by elements in an array (or nested object) in DynamoDB
Nested Query in DynamoDB returns nothing
Referring to this answer.enter link description here
According to DDB Nested Attributes doc, the filter expression should look like the following format:
"filter" : {
"expression" : "#path.#filter = :${fp}", ## filter path parent.target = :target
"expressionNames": {
"#path" : "${path}",
"#filter" : "${fp}"
},
"expressionValues" : {
":${fp}" : $util.dynamodb.toDynamoDBJson(${$target[$fp].eq}) ## :target : value to filter for
}
}

dynamodb UpdateItem json payload

I am using the AWS service proxy to expose DynamoDB as API. For PutItem it works fine, but I don't have idea how to update a array of object.
Below "paymentDetail" is the list of Map, I can post to DynamoDB, but I failed for update the JSON payload. Not sure DynamoDB support this or not, I always got this message:
{"__type": "com.amazon.coral.service#SerializationException"}
Can anybody share your ideas how to use mapping template to update JSON payload? Thanks in advance.
Here is my code, please advise and help.Please kindly let me know in case that the information is not sufficient.
--------- Request Method for UpdateItem ------------
{
"TableName": "claim",
"Key": {
"claimNumber": {
"S": "$input.params("claimNumber")"
}
},
"UpdateExpression": "set claimStatus = :claimStatus, statusUpdateDate = :statusUpdateDate, netClaimAmount = :netClaimAmount, paymentDetail = :paymentDetail, updatedAt = :updatedAt",
"ConditionExpression": "claimNumber = :claimNumber",
"ExpressionAttributeValues": {
":claimNumber": {"S": "$input.params("claimNumber")"},
":claimStatus": {"S": "$input.path("$.claimStatus")"},
":statusUpdateDate": {"S": "$input.path("$.statusUpdateDate")"},
":netClaimAmount": {"N": "$input.path("$.netClaimAmount")"},
":paymentDetail": {"L": "$input.path("$.paymentDetail")"},
":updatedAt": {"S": "$input.path("$.updatedAt")"}
},
"ReturnValues": "UPDATED_NEW"
}
----------test data for UpdateItem---------
{
"claimNumber": "000-00-000959",
"updatedAt":"2017-09-10",
"claimStatus": "close",
"statusUpdateDate": "2017-09-13",
"netClaimAmount": 60000000,
"paymentDetail": [
{
"coverage": "AAA",
"claimPaymentDate": "2017-09-03",
"claimAmount": "10000000",
"costCategory": "1",
"costType": "A"
},
{
"coverage": "BBB",
"claimPaymentDate": "2017-09-03",
"claimAmount": "20000000",
"costCategory": "2",
"costType": "B"
},
{
"coverage": "CCC",
"claimPaymentDate": "2017-09-03",
"claimAmount": "30000000",
"costCategory": "3",
"costType": "C"
}
]
}
---PutItem for your information---
#set($inputRoot = $input.path('$'))
{
"TableName": "claim",
"Item": {
"claimNumber": {
"S": "$input.path("$.claimNumber")"
},
"policyNumber": {
"S": "$input.path("$.policyNumber")"
},
"productName": {
"S": "$input.path("$.productName")"
},
"lossDate": {
"S": "$input.path("$.lossDate")"
},
"lossTime": {
"S": "$input.path("$.lossTime")"
},
"reportedDate": {
"S": "$input.path("$.reportedDate")"
},
"lossCause": {
"S": "$input.path("$.lossCause")"
},
"description": {
"S": "$input.path("$.description")"
},
"prefectureCode": {
"S": "$input.path("$.prefectureCode")"
},
"city": {
"S": "$input.path("$.city")"
},
"address": {
"S": "$input.path("$.address")"
},
"reportedByType": {
"S": "$input.path("$.reportedByType")"
},
"createdAt": {
"S": "$input.path("$.createdAt")"
},
"updatedAt": {
"S": "$input.path("$.updatedAt")"
},
"claimImageId": {
"S": "$input.path("$.claimImageId")"
},
"contact": {
"M": {
"lastName": {
"S": "$input.path("$.contact.lastName")"
},
"firstName": {
"S": "$input.path("$.contact.firstName")"
},
"postCode": {
"S": "$input.path("$.contact.postCode")"
},
"prefectureCode": {
"S": "$input.path("$.contact.prefectureCode")"
},
"city": {
"S": "$input.path("$.contact.city")"
},
"address": {
"S": "$input.path("$.contact.address")"
},
"homePhone": {
"S": "$input.path("$.contact.homePhone")"
},
"email": {
"S": "$input.path("$.contact.email")"
}
}
},
"claimStatus": {
"S": "$input.path("$.claimStatus")"
},
"statusUpdateDate": {
"S": "$input.path("$.statusUpdateDate")"
},
"netClaimAmount": {
"N": "$input.path("$.netClaimAmount")"
},
"paymentDetail": {
"L":[
#foreach($elem in $inputRoot.paymentDetail){
"M": {
"coverage": {
"S": "$elem.coverage"
},
"claimPaymentDate": {
"S": "$elem.claimPaymentDate"
},
"claimAmount": {
"S": "$elem.claimAmount"
},
"costCategory": {
"S": "$elem.costCategory"
},
"costType": {
"S": "$elem.costType"
}
}
}#if($foreach.hasNext),#end
#end
]
}
}
}
Your body mapping template seems to have issues.
Please see below the corrected one.
#set($inputRoot = $input.path('$'))
{
"TableName": "claim",
"Key": {
"claimNumber": {
"S": "$input.path("$.claimNumber")"
}
},
"UpdateExpression": "set claimStatus = :claimStatus, statusUpdateDate = :statusUpdateDate, netClaimAmount = :netClaimAmount, paymentDetail = :paymentDetail, updatedAt = :updatedAt",
"ConditionExpression": "claimNumber = :claimNumber",
"ExpressionAttributeValues": {
":claimNumber": {"S": "$input.path("$.claimNumber")"},
":claimStatus": {"S": "$input.path("$.claimStatus")"},
":statusUpdateDate": {"S": "$input.path("$.statusUpdateDate")"},
":netClaimAmount": {"N": "$input.path("$.netClaimAmount")"},
":updatedAt": {"S": "$input.path("$.updatedAt")"},
":paymentDetail": {
"L":[
#foreach($elem in $inputRoot.paymentDetail){
"M": {
"coverage": {
"S": "$elem.coverage"
},
"claimPaymentDate": {
"S": "$elem.claimPaymentDate"
},
"claimAmount": {
"S": "$elem.claimAmount"
},
"costCategory": {
"S": "$elem.costCategory"
},
"costType": {
"S": "$elem.costType"
}
}
}#if($foreach.hasNext),#end
#end
]
}
},
"ReturnValues": "UPDATED_NEW"
}

DynamoDB UpdateException: Type mismatch for attribute to update

I have the following record in DynamoDB:
{
"BusinessNo": {
"N": "12345"
},
"Metadata": {
"M": {
"MimeType": {
"S": "audio/wav"
},
"FileName": {
"S": "00032329.wav"
},
"CustomC": {
"S": "baz"
},
"CustomA": {
"S": "foo"
},
"CustomB": {
"S": "bar"
},
"Size": {
"S": "3992020323"
}
}
},
"Id": {
"S": "f0de8af3-a7f5-4d9b-ad5d-b2f150abd15e"
},
"Revision": {
"N": "2"
}
}
But when I submit the following using the update method of DynamoDB.DocumentClient from the nodejs AWS SDK (I have also tried add instead of set):
{
"TableName": "Storage_FileMetadata",
"Key": {
"Id": "f0de8af3-a7f5-4d9b-ad5d-b2f150abd15e",
"BusinessNo": "12345"
},
"ExpressionAttributeNames": {
"#m": "Metadata",
"#k": "CustomD",
"#r": "Revision"
},
"ExpressionAttributeValues": {
":r": 4,
":v": "doo-wop"
},
"UpdateExpression": "set #m.#k = :v",
"ConditionExpression": "#r < :r"
}
I get the following exception:
{
"message": "Type mismatch for attribute to update",
"code": "ValidationException",
"time": "2016-11-11T18:55:01.543Z",
"requestId": "b9d78c87-1c4d-400a-8968-d761b657cd53",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
I think I'm missing something about adding/updating nested attributes but after reading the docs I can't figure out what.
Seems that you need to send the value "BusinessNo": "12345" as a number
"Key": {
"Id": "f0de8af3-a7f5-4d9b-ad5d-b2f150abd15e",
"BusinessNo": 12345
}

Resources