Skip to content

Instantly share code, notes, and snippets.

@SaschaMoellering
Created October 22, 2014 12:38
Show Gist options
  • Save SaschaMoellering/fee91935075cc93e98c2 to your computer and use it in GitHub Desktop.
Save SaschaMoellering/fee91935075cc93e98c2 to your computer and use it in GitHub Desktop.
DynamoDB
package com.zanox.kinesis.dynamo;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.*;
/**
* Created by sascha.moellering on 17/10/2014.
*/
@DynamoDBTable(tableName="KinesisData")
public class DynamoDBBean {
private String id;
private String payload;
@DynamoDBHashKey(attributeName = "Id")
@DynamoDBAutoGeneratedKey
public String getId() { return id; }
public void setId(String id) { this.id = id; }
@DynamoDBAttribute(attributeName="payload")
public String getPayload() { return this.payload; };
public void setPayload(String payload) { this.payload = payload; };
public static void saveItem(final String payload) {
AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain());
dynamoDBClient.setRegion(Region.getRegion(Regions.EU_WEST_1));
DynamoDBBean obj = new DynamoDBBean();
obj.setPayload(payload);
// id field is null at this point
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
mapper.save(obj);
System.out.println("Object was saved with id " + obj.getId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment