Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
Add Comment
Please, Sign In to add comment