Created
August 23, 2012 23:00
-
-
Save sh0tt/3443086 to your computer and use it in GitHub Desktop.
DynamoDB binary data put sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package samples.dynamodb; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.nio.ByteBuffer; | |
import java.util.HashMap; | |
import java.util.Map; | |
import samples.dynamodb.model.Employee; | |
import com.amazonaws.services.dynamodb.AmazonDynamoDB; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper; | |
import com.amazonaws.services.dynamodb.model.AttributeValue; | |
import com.amazonaws.services.dynamodb.model.PutItemRequest; | |
import com.amazonaws.services.dynamodb.model.PutItemResult; | |
public class BinaryDataPutSample extends AbstractDynamoDBSample { | |
public static void main(String[] args) { | |
new BinaryDataPutSample().execute(args); | |
} | |
@Override | |
public void execute(String... args) { | |
AmazonDynamoDB client = createClient(); | |
executeBinaryDataLowLevel(client, args); | |
executeBinaryDataHighLevel(client, args); | |
} | |
private void executeBinaryDataLowLevel(AmazonDynamoDB client, | |
String... args) { | |
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); | |
{ | |
item.put("id", new AttributeValue().withN("7")); | |
item.put("time", new AttributeValue().withN("1331650800000")); | |
ByteBuffer b = createBody(); | |
item.put("picture", new AttributeValue().withB(b)); | |
} | |
PutItemRequest putItemRequest = new PutItemRequest().withTableName( | |
"employee").withItem(item); | |
PutItemResult result = client.putItem(putItemRequest); | |
Double consumedCapacityUnits = result.getConsumedCapacityUnits(); | |
System.out.println("put item:" + item + ", consumed units:" | |
+ consumedCapacityUnits); | |
} | |
private void executeBinaryDataHighLevel(AmazonDynamoDB client, | |
String... args) { | |
DynamoDBMapper mapper = new DynamoDBMapper(client); | |
Employee e = mapper.load(Employee.class, 1007L, 1331821910141L); | |
e.setPicture(createBodyBytes()); | |
mapper.save(e); | |
System.out.println("high level:" + e.toString()); | |
} | |
private ByteBuffer createBody() { | |
byte[] body = createBodyBytes(); | |
ByteBuffer buffer = ByteBuffer.allocate(body.length); | |
buffer.put(body, 0, body.length); | |
buffer.position(0); | |
return buffer; | |
} | |
private byte[] createBodyBytes() { | |
try { | |
File f = new File("C:\\picture.jpg"); | |
FileInputStream fis = new FileInputStream(f); | |
byte[] body = new byte[(int) f.length()]; | |
fis.read(body); | |
return body; | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package samples.dynamodb.model; | |
import java.util.Set; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBAttribute; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBHashKey; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBRangeKey; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBTable; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBVersionAttribute; | |
@DynamoDBTable(tableName = "employee") | |
public class Employee { | |
long id; | |
long time; | |
String firstname; | |
String lastname; | |
Set<String> role; | |
Long version; | |
byte[] picture; | |
@DynamoDBHashKey(attributeName = "id") | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
@DynamoDBRangeKey(attributeName = "time") | |
public long getTime() { | |
return time; | |
} | |
public void setTime(long time) { | |
this.time = time; | |
} | |
@DynamoDBAttribute(attributeName = "firstname") | |
public String getFirstname() { | |
return firstname; | |
} | |
public void setFirstname(String firstname) { | |
this.firstname = firstname; | |
} | |
@DynamoDBAttribute(attributeName = "lastname") | |
public String getLastname() { | |
return lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
@DynamoDBAttribute | |
public Set<String> getRole() { | |
return role; | |
} | |
public void setRole(Set<String> role) { | |
this.role = role; | |
} | |
@DynamoDBVersionAttribute | |
public Long getVersion() { | |
return version; | |
} | |
public void setVersion(Long version) { | |
this.version = version; | |
} | |
// @DynamoDBAttribute | |
// public ByteBuffer getPicture() { | |
// return picture; | |
// } | |
// | |
// public void setPicture(ByteBuffer picture) { | |
// this.picture = picture; | |
// } | |
@Override | |
public String toString() { | |
return "Employee [id=" + id + ", time=" + time + ", firstname=" | |
+ firstname + ", lastname=" + lastname + ", role=" + role | |
+ ", version=" + version + ", picture=" + picture + "]"; | |
} | |
@DynamoDBAttribute | |
public byte[] getPicture() { | |
return picture; | |
} | |
public void setPicture(byte[] picture) { | |
this.picture = picture; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment