Reputation: 583
The items from the stream have the data type present in the stream.
I am trying to find a library similar to dynamodb-data-types, which can unwrap the dynamoDB structure to json structure for java. My lambda is in java.
Eg: I am trying to convert
{
"prospectId": {
"S": "kjhakh23444"
},
"campaignId": {
"S": "kjhniujj123"
}
}
to
{
"prospectId": "kjhakh23444",
"campaignId": "kjhniujj123"
}
Prospect.java
@DynamoDBTable(tableName="Prospect")
public class Prospect {
private String campaignId;
private String prospectId;
public Prospect() {
}
@DynamoDBHashKey(attributeName="campaignId")
public String getCampaignId() {
return campaignId;
}
public void setCampaignId(String campaignId) {
this.campaignId = campaignId;
}
public String getProspectId() {
return prospectId;
}
public void setProspectId(String prospectId) {
this.prospectId = prospectId;
}
}
I am trying below but as per the DynamoDbStreamRecord structure, below will not work out. How can I parse DynamoDbStreamRecord so that I get the Prospect Object?
String body = new Gson().toJson(dynamodbStreamRecord.getDynamodb().getNewImage());
Prospect prospect = new Gson().fromJson(body, Prospect.class);
Please help me out.
Upvotes: 5
Views: 1564
Reputation: 21
You can use
software.amazon.awssdk.enhanced.dynamodb.document.EnhancedDocument;
EnhancedDocument.fromAttributeValueMap(your-image-from-stream).toJson();
Note that you might need to convert the StreamRecord Pojo with
List<Record> convertedRecords =
DynamodbEventTransformer.toRecordsV2(ddbEvent);
per
There are different versions from com.amazonaws.services.lambda.runtime.events.DynamodbEvent and the newer (I assume) implementation software.amazon.awssdk.services.dynamodb.model
you'll find differences in AttributeValue and StreamRecord, with builders in the services.dynamodb.model e.g.
com.amazonaws.services.lambda.runtime.events.models.dynamodb.AttributeValue;
software.amazon.awssdk.services.dynamodb.model.AttributeValue;
Upvotes: 2