Reputation: 133
What I want to do? -I wanted to save batch of records into DynamoDB using DynamoDBMapper.
Model Class
package model;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
@DynamoDBTable(tableName = "jobTable")
public final class MapperModel {
public String jobId;
public String jobName;
public Stringparams stringparams[];
public String url;
public String source;
public String[] target;
public String contentType;
public String dueDate;
public Metadata metadata;
/** getters start here **/
@DynamoDBHashKey(attributeName = "jobId")
public String getJobId() {
return jobId;
}
@DynamoDBAttribute(attributeName = "jobName")
public String getJobName() {
return jobName;
}
@DynamoDBAttribute(attributeName = "url")
public String getURL() {
return url;
}
@DynamoDBAttribute(attributeName = "strings")
public Stringparams[] getStrings() {
return stringparams;
}
@DynamoDBAttribute(attributeName = "source")
public String getSource() {
return source;
}
@DynamoDBAttribute(attributeName = "target")
public String[] getTarget() {
return target;
}
@DynamoDBAttribute(attributeName = "contentType")
public String getContentType() {
return contentType;
}
@DynamoDBAttribute(attributeName = "dueDate")
public String getDueDate() {
return dueDate;
}
@DynamoDBAttribute(attributeName = "metadata")
public Metadata getMetadata() {
return metadata;
}
/** setters start here **/
public void setJobId(String string) {
this.jobId = string;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public void setStrings(Stringparams[] strings) {
this.stringparams = strings;
}
public void setURL(String url) {
this.url = url;
}
public void setSource(String source) {
this.source = source;
}
public void setTarget(String[] target) {
this.target = target;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}
@DynamoDBDocument
public static final class Stringparams {
@DynamoDBAutoGeneratedKey
public String key;
public String value;
public String description;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@DynamoDBAttribute(attributeName = "value")
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@DynamoDBAttribute(attributeName = "description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@DynamoDBDocument
public static final class Metadata {
public String invoicingCategory;
public String invoicingRegion;
public String referenceURL;
public void setInvoicingCategory(String invoicingCategory) {
this.invoicingCategory = invoicingCategory;
}
public void setInvoicingRegion(String invoicingRegion) {
this.invoicingRegion = invoicingRegion;
}
public void setReferenceURL(String referenceURL) {
this.referenceURL = referenceURL;
}
@DynamoDBAttribute(attributeName = "invoicingCategory")
public String getInvoicingCategory() {
return invoicingCategory;
}
@DynamoDBAttribute(attributeName = "invoicingRegion")
public String getInvoicingRegion() {
return invoicingRegion;
}
@DynamoDBAttribute(attributeName = "referenceURL")
public String getReferenceURL() {
return referenceURL;
}
}
}
public class Test {
......
.......
public void persistRecordsToDataStore(List<MapperModel>
requestModelList)
{
AmazonDynamoDB amazonDynamoDBClient = new
AmazonDynamoDBClient();
DynamoDBMapper dynamoDBMapper = new
DynamoDBMapper(amazonDynamoDBClient);
amazonDynamoDBClient.setRegion(Region.getRegion(Regions.US_WEST_2));
boolean insertStatus = insertRecords(dynamoDBMapper, requestModelList)
}
public static boolean insertRecords(DynamoDBMapper dynamoDBMapper,
List<MapperModel> requestModelList)
{
for (MapperModel requestModel : requestModelList)
{
dynamoDBMapper.save(requestModel);
}
return true;
}
......
......
}
Question: I see an error not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted. Can anyone please help me in understanding and share the fix?
Upvotes: 8
Views: 7128
Reputation: 11
You are missing the DynamoDB Conversion for your custom class MetaData. Either you can write your own converter or can use @DynamoDBTypeConverted attribute and specify your custom class name as the parameter.
Upvotes: 1
Reputation: 16641
In my case the problem was that I did not realise that the DynamoDB Mapper automatically attempts to map all public methods. It took me ages to find this out, because the error message does not report which attribute it is failing on.
It can be solved like this:
@DynamoDBIgnore
public SomeNonMappableObject getThing() {
...
}
Upvotes: 0
Reputation: 11234
There is simpler way by use annotation @DynamoDBDocument
For example in the above java class,
@DynamoDBDocument
public class Metadata {
private Stirng aa;
private int bb;
}
Then it should work
Upvotes: 6
Reputation: 133
I have figured out the answer from amazon docs http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.ArbitraryDataMapping.html
Solution to my question
@DynamoDBTable(tableName = "jobTable")
public final class MapperModel {
public String jobId;
public String jobName;
public Stringparams stringparams[];
public String url;
public String source;
public String[] target;
public String contentType;
public String dueDate;
public Metadata metadata;
@DynamoDBHashKey(attributeName = "jobId")
public String getJobId() {
return jobId;
}
@DynamoDBAttribute(attributeName = "jobName")
public String getJobName() {
return jobName;
}
@DynamoDBAttribute(attributeName = "url")
public String getURL() {
return url;
}
**@DynamoDBTypeConverted(converter= StringparamsTypeConverter.class)**
public Stringparams[] getStrings() {
return stringparams;
}
@DynamoDBAttribute(attributeName = "source")
public String getSource() {
return source;
}
@DynamoDBAttribute(attributeName = "target")
public String[] getTarget() {
return target;
}
@DynamoDBAttribute(attributeName = "contentType")
public String getContentType() {
return contentType;
}
@DynamoDBAttribute(attributeName = "dueDate")
public String getDueDate() {
return dueDate;
}
**@DynamoDBTypeConverted(converter = MetadataTypeConverter.class)**
public Metadata getMetadata() {
return metadata;
}
/** setters start here **/
public void setJobId(String string) {
this.jobId = string;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public void setStrings(Stringparams[] strings) {
this.stringparams = strings;
}
public void setURL(String url) {
this.url = url;
}
public void setSource(String source) {
this.source = source;
}
public void setTarget(String[] target) {
this.target = target;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}
}
I am only providing code for one ObjectType Converter.
public class MetadataTypeConverter implements DynamoDBTypeConverter<String, Metadata> {
@Override
public String convert(final Metadata metadata) {
String metadataValue = null;
try {
.....
.....
your custom code
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return metadataValue;
}
@Override
public Metadata unconvert(final String jsonData) {
Metadata metadataType = null;
try {
.....
.....
your custom code
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return metadataType;
}
}
Upvotes: 4
Reputation: 1
There is an example here that uses DynamoDBTypeConverted: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.ArbitraryDataMapping.html
Upvotes: 0