@Override public void put(String key, String value) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(value, "'value' must not be empty."); awaitForActive(); Item item = new Item() .withPrimaryKey(KEY, key) .withString(VALUE, value); if (this.timeToLive != null && this.timeToLive > 0) { item = item.withLong(TTL, (System.currentTimeMillis() + this.timeToLive) / 1000); } this.table.putItem(item); }
@Override public void put(String key, String value) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(value, "'value' must not be empty."); awaitForActive(); Item item = new Item() .withPrimaryKey(KEY, key) .withString(VALUE, value); if (this.timeToLive != null && this.timeToLive > 0) { item = item.withLong(TTL, (System.currentTimeMillis() + this.timeToLive) / 1000); } this.table.putItem(item); }
private static void loadSampleForums(String tableName) { Table table = dynamoDB.getTable(tableName); try { System.out.println("Adding data to " + tableName); Item item = new Item().withPrimaryKey("Name", "Amazon DynamoDB") .withString("Category", "Amazon Web Services") .withNumber("Threads", 2).withNumber("Messages", 4) .withNumber("Views", 1000); table.putItem(item); item = new Item().withPrimaryKey("Name", "Amazon S3") .withString("Category", "Amazon Web Services") .withNumber("Threads", 0); table.putItem(item); } catch (Exception e) { System.err.println("Failed to create item in " + tableName); System.err.println(e.getMessage()); } }
/** * The version marker has a primary key whose PARENT is {@code name}; * this MUST NOT be a value which represents an absolute path. * @param name name of the version marker * @param version version number * @param timestamp creation timestamp * @return an item representing a version marker. */ static Item createVersionMarker(String name, int version, long timestamp) { return new Item().withPrimaryKey(createVersionMarkerPrimaryKey(name)) .withInt(TABLE_VERSION, version) .withLong(TABLE_CREATED, timestamp); }
.withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1") .withString("ReplyDateTime", (dateFormatter.format(date3))) .withString("Message", "DynamoDB Thread 1 Reply 1 text") .withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1") .withString("ReplyDateTime", dateFormatter.format(date2)) .withString("Message", "DynamoDB Thread 1 Reply 2 text") .withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2") .withString("ReplyDateTime", dateFormatter.format(date1)) .withString("Message", "DynamoDB Thread 2 Reply 1 text") .withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2") .withString("ReplyDateTime", dateFormatter.format(date0)) .withString("Message", "DynamoDB Thread 2 Reply 2 text")
private static void loadSampleForums(String tableName) { Table table = dynamoDB.getTable(tableName); try { System.out.println("Adding data to " + tableName); Item item = new Item().withPrimaryKey("Name", "Amazon DynamoDB") .withString("Category", "Amazon Web Services") .withNumber("Threads", 2) .withNumber("Messages", 4) .withNumber("Views", 1000); table.putItem(item); item = new Item().withPrimaryKey("Name", "Amazon S3") .withString("Category", "Amazon Web Services") .withNumber("Threads", 0); table.putItem(item); } catch (Exception e) { System.err.println("Failed to create item in " + tableName); System.err.println(e.getMessage()); } }
.withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1") .withString("ReplyDateTime", (dateFormatter.format(date3))) .withString("Message", "DynamoDB Thread 1 Reply 1 text") .withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1") .withString("ReplyDateTime", dateFormatter.format(date2)) .withString("Message", "DynamoDB Thread 1 Reply 2 text") .withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2") .withString("ReplyDateTime", dateFormatter.format(date1)) .withString("Message", "DynamoDB Thread 2 Reply 1 text") .withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2") .withString("ReplyDateTime", dateFormatter.format(date0)) .withString("Message", "DynamoDB Thread 2 Reply 2 text")
private static void uploadProduct(String tableName, int productIndex) { Table table = dynamoDB.getTable(tableName); try { System.out.println("Processing record #" + productIndex); Item item = new Item() .withPrimaryKey("Id", productIndex) .withString("Title", "Book " + productIndex + " Title") .withString("ISBN", "111-1111111111") .withStringSet( "Authors", new HashSet<String>(Arrays.asList("Author1"))) .withNumber("Price", 2) .withString("Dimensions", "8.5 x 11.0 x 0.5") .withNumber("PageCount", 500) .withBoolean("InPublication", true) .withString("ProductCategory", "Book"); table.putItem(item); } catch (Exception e) { System.err.println("Failed to create item " + productIndex + " in " + tableName); System.err.println(e.getMessage()); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; try { table.putItem(new Item() .withPrimaryKey("year", year, "title", title) .withJSON("info", "{\"plot\" : \"Something happens.\"}")); System.out.println("PutItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty()); } catch (Exception e) { System.out.println("PutItem failed"); e.printStackTrace(); } } }
.withPrimaryKey("Name", "Amazon RDS") .withNumber("Threads", 0)); .withPrimaryKey("ForumName","Amazon RDS","Subject","Amazon RDS Thread 1") .withString("Message", "ElasticCache Thread 1 message") .withStringSet("Tags", new HashSet<String>(
public static void main(String[] args) throws Exception { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); JsonParser parser = new JsonFactory() .createParser(new File("moviedata.json")); Iterator<JsonNode> iter = getRootNode(parser).iterator(); ObjectNode currentNode; while (iter.hasNext()) { currentNode = (ObjectNode) iter.next(); int year = getYear(currentNode); String title = getTitle(currentNode); System.out.println("Adding movie: " + year + " " + title); table.putItem(new Item() .withPrimaryKey("year", year, "title", title) .withJSON("info", getInfo(currentNode))); } parser.close(); }
.withPrimaryKey("ForumName", "Amazon DynamoDB") .withString("Subject", "DynamoDB Thread 1") .withString("Message", "DynamoDB thread 1 message") .withPrimaryKey("ForumName", "Amazon DynamoDB") .withString("Subject", "DynamoDB Thread 2") .withString("Message", "DynamoDB thread 2 message") .withPrimaryKey("ForumName", "Amazon S3") .withString("Subject", "S3 Thread 1") .withString("Message", "S3 Thread 3 message")
.withPrimaryKey("ForumName", "Amazon DynamoDB") .withString("Subject", "DynamoDB Thread 1") .withString("Message", "DynamoDB thread 1 message") .withPrimaryKey("ForumName", "Amazon DynamoDB") .withString("Subject", "DynamoDB Thread 2") .withString("Message", "DynamoDB thread 2 message") .withPrimaryKey("ForumName", "Amazon S3") .withString("Subject", "S3 Thread 1") .withString("Message", "S3 Thread 3 message")
/** * Converts a {@link DDBPathMetadata} to a DynamoDB item. * * Can ignore {@code IS_AUTHORITATIVE} flag if {@code ignoreIsAuthFlag} is * true. * * @param meta {@link DDBPathMetadata} to convert * @param ignoreIsAuthFlag if true, ignore the authoritative flag on item * @return {@code meta} converted to DynamoDB item */ static Item pathMetadataToItem(DDBPathMetadata meta, boolean ignoreIsAuthFlag) { Preconditions.checkNotNull(meta); final FileStatus status = meta.getFileStatus(); final Item item = new Item().withPrimaryKey(pathToKey(status.getPath())); if (status.isDirectory()) { item.withBoolean(IS_DIR, true); if (!ignoreIsAuthFlag) { item.withBoolean(IS_AUTHORITATIVE, meta.isAuthoritativeDir()); } } else { item.withLong(FILE_LENGTH, status.getLen()) .withLong(MOD_TIME, status.getModificationTime()) .withLong(BLOCK_SIZE, status.getBlockSize()); } item.withBoolean(IS_DELETED, meta.isDeleted()); return item; }
public static void createItem(String threadId, String replyDateTime) throws IOException { Table table = dynamoDB.getTable(tableName); // Craft a long message String messageInput = "Long message to be compressed in a lengthy forum reply"; // Compress the long message ByteBuffer compressedMessage = compressString(messageInput.toString()); table.putItem(new Item() .withPrimaryKey("Id", threadId) .withString("ReplyDateTime", replyDateTime) .withString("Message", "Long message follows") .withBinary("ExtendedMessage", compressedMessage) .withString("PostedBy", "User A")); }
.withPrimaryKey("Id", 120) .withString("Title", "Book 120 Title") .withString("ISBN", "120-1111111111") .withPrimaryKey("Id", 121) .withString("Title", "Book 121 Title") .withString("ISBN", "121-1111111111")
/** * Applies the optional range key to the specified DynamoDB item (for * writing). * * @param item The DynamoDB item. * * @return The resulting DynamoDB item. */ Item applyOptionalRangeKey(final Item item) { if (!applyRangeKey) { return item; } Object hashKeyValue = item.get(itemTransformer.getHashKeyAttributeName()); if (hashKeyValue == null) { throw new PersistenceException("Missing hash key in transformed DynamoDB item: " + itemTransformer.getHashKeyAttributeName()); } return item.withPrimaryKey( itemTransformer.getHashKeyAttributeName(), hashKeyValue, rangeKeyName, rangeKeyValue ); } }
infoMap.put("rating", 0.0); Item item = new Item() .withPrimaryKey(new PrimaryKey("year", year, "title", title)) .withMap("info", infoMap);
@Override public boolean write(Job job) { Timer.Context context = writeTimer.time(); ObjectMapper objectMapper = new ObjectMapper(); Table table = dynamoDB.getTable(tableName); try { String jobJSON = objectMapper.writeValueAsString(job); Item item = new Item() .withPrimaryKey("JobKey", jobIdKey(job)) .withString("UniqueId", job.getUniqueID()) .withString("JobHandle", job.getJobHandle()) .withNumber("When", job.getTimeToRun()) .withString("Priority", job.getPriority().toString()) .withString("JobQueue", job.getFunctionName()) .withString("JSON", jobJSON); table.putItem(item); return true; } catch (JsonProcessingException e) { e.printStackTrace(); } finally { context.stop(); } return false; }
public static void putItem( String issueId, String title, String description, String createDate, String lastUpdateDate, String dueDate, Integer priority, String status) { Table table = dynamoDB.getTable(tableName); Item item = new Item() .withPrimaryKey("IssueId", issueId) .withString("Title", title) .withString("Description", description) .withString("CreateDate", createDate) .withString("LastUpdateDate", lastUpdateDate) .withString("DueDate", dueDate) .withNumber("Priority", priority) .withString("Status", status); table.putItem(item); }