private void printNodeHistoryItems(String nid, String time) { String hashKey = nid; String rangeKey = time; NodeHistory nhKey = new NodeHistory(); nhKey.setNodeID(hashKey); List<NodeHistory> latestNodeHistory; if (time != null) { Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withS(rangeKey)); DynamoDBQueryExpression<NodeHistory> queryExpression = new DynamoDBQueryExpression<NodeHistory>() .withHashKeyValues(nhKey) .withRangeKeyCondition("Time", rangeKeyCondition); latestNodeHistory = mapper.query(NodeHistory.class, queryExpression); } else { DynamoDBQueryExpression<NodeHistory> queryExpression = new DynamoDBQueryExpression<NodeHistory>().withHashKeyValues(nhKey); latestNodeHistory = mapper.query(NodeHistory.class, queryExpression); } for (NodeHistory nh : latestNodeHistory) { System.out.format( "SerialNumber=%s, Time=%s, SAValues=%s\n", nh.getNodeID(), nh.getTime(), nh.getSAValues()); } }
@Override public List<KinesisMessageModel> emit(final UnmodifiableBuffer<KinesisMessageModel> buffer) { List<KinesisMessageModel> items = buffer.getRecords(); List<KinesisMessageModel> failedItems = new ArrayList<KinesisMessageModel>(); // failedItems. DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient); KinesisMessageModel message = null; try { mapper.batchSave(items); LOG.info("Successfully emitted: " + items.size() + " items"); } catch (AmazonClientException e) { e.printStackTrace(); // } // LOG.info("Successfully emitted " + (items.size() - failedItems.size()) + " records into // DynamoDB."); // failedItems. // return failedItems; return failedItems; }
@Test public void getHashRangeItemTest() { putItemWithHashKeyAndRangeKey(); putItemWithHashKeyAndRangeKeyOverwriteItem(); TestClassWithHashRangeKey value2c = new TestClassWithHashRangeKey(); value2c.setHashCode("hash2"); value2c.setRangeCode("range2c"); value2c.setStringData("string2c"); value2c.setIntData(23); value2c.setStringSetData(set("stringSetVal2c1", "stringSetVal2c2")); value2c.setNumberSetData(set(7, 8)); mapper.save(value2c); String hashCode = "hash2"; String rangeCode = "range2"; TestClassWithHashRangeKey value = mapper.load(TestClassWithHashRangeKey.class, hashCode, rangeCode); Assert.assertNotNull("Value not found.", value); Assert.assertEquals("Wrong hashCode.", hashCode, value.getHashCode()); Assert.assertEquals("Wrong rangeCode.", rangeCode, value.getRangeCode()); Assert.assertEquals("Wrong stringData.", "string2b", value.getStringData()); Assert.assertEquals("Wrong intData.", 22, value.getIntData()); Assert.assertEquals( "Wrong stringSetData.", set("stringSetVal2b1", "stringSetVal2b2"), value.getStringSetData()); Assert.assertEquals("Wrong numberSetData.", set(5, 6), value.getNumberSetData()); }
private void createNodeHistoryItems( NodeHistory nHistoryDB, String nid, String time, String values) { nHistoryDB.setNodeID(nid); nHistoryDB.setTime(time); nHistoryDB.setSAValues(values); mapper.save(nHistoryDB); String hashKey = nid; String rangeKey = time; NodeHistory nhKey = new NodeHistory(); nhKey.setNodeID(hashKey); Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withS(rangeKey)); DynamoDBQueryExpression<NodeHistory> queryExpression = new DynamoDBQueryExpression<NodeHistory>() .withHashKeyValues(nhKey) .withRangeKeyCondition("Time", rangeKeyCondition); List<NodeHistory> latestNodeHistory = mapper.query(NodeHistory.class, queryExpression); System.out.println("Item created: "); for (NodeHistory nh : latestNodeHistory) { System.out.format( "SerialNumber=%s, Time=%s, SAValues=%s\n", nh.getNodeID(), nh.getTime(), nh.getSAValues()); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider()); client.setRegion(Region.getRegion(Regions.US_WEST_2)); DynamoDBMapper mapper = new DynamoDBMapper(client); // retrieve an object using a "normal" (non-composite) id Movie m = new Movie(); m.setTitle("foxy brown"); Movie m2 = mapper.load(m); if (m2.getExtra() != null) { System.out.println("title:" + m2.getTitle() + " extra:" + m2.getExtra()); } // retrieve an object using a composite key Artist a = new Artist(); a.setId(5); a.setName("Van Gogh"); Artist a2 = mapper.load(a); // scan through a bunch of entries in a table and print them out ScanRequest scanRequest = new ScanRequest().withTableName("Artists"); ScanResult result = client.scan(scanRequest); for (Map<String, AttributeValue> item : result.getItems()) { String o1 = item.get("id").getN(); String o2 = item.get("name").getS(); Artist artist = mapper.load(Artist.class, new Integer(o1), o2); artist.dump(); } }
@Test public void putItemWithHashKeyAndRangeKeyOverwriteItem() { try { createTable( hashRangeTableName, createStringAttributeDefinition("hashCode"), createStringAttributeDefinition("rangeCode")); } catch (ResourceInUseException riue) { // The table is already created } TestClassWithHashRangeKey value2a = new TestClassWithHashRangeKey(); value2a.setHashCode("hash2"); value2a.setRangeCode("range2"); value2a.setStringData("string2a"); value2a.setIntData(21); value2a.setStringSetData(set("stringSetVal2a1", "stringSetVal2a2")); value2a.setNumberSetData(set(3, 4)); mapper.save(value2a); TestClassWithHashRangeKey value2b = new TestClassWithHashRangeKey(); value2b.setHashCode("hash2"); value2b.setRangeCode("range2"); value2b.setStringData("string2b"); value2b.setIntData(22); value2b.setStringSetData(set("stringSetVal2b1", "stringSetVal2b2")); value2b.setNumberSetData(set(5, 6)); mapper.save(value2b); }
@Test public void deleteHashRangeItemTest() { putItemWithHashKeyAndRangeKey(); putItemWithHashKeyAndRangeKeyOverwriteItem(); String hashCode = "hash2"; String rangeCode = "range2"; TestClassWithHashRangeKey value = mapper.load(TestClassWithHashRangeKey.class, hashCode, rangeCode); Assert.assertNotNull("Value not found.", value); Assert.assertEquals("Wrong hashCode.", hashCode, value.getHashCode()); Assert.assertEquals("Wrong rangeCode.", rangeCode, value.getRangeCode()); Assert.assertEquals("Wrong stringData.", "string2b", value.getStringData()); Assert.assertEquals("Wrong intData.", 22, value.getIntData()); Assert.assertEquals( "Wrong stringSetData.", set("stringSetVal2b1", "stringSetVal2b2"), value.getStringSetData()); Assert.assertEquals("Wrong numberSetData.", set(5, 6), value.getNumberSetData()); mapper.delete(value); TestClassWithHashRangeKey value2 = mapper.load(TestClassWithHashRangeKey.class, hashCode, rangeCode); Assert.assertNull("Value2 should not be found.", value2); }
/** * Returns list of all items that are in cart for that user. * * @param userId * @return */ public List<CartItem> getCartItemsFromUserId(int userId) { // Declare mapper DynamoDBMapper mapper = this.conn.getMapper(); // Create hash key values which to form a query CartItem hashKeyValues = new CartItem(); hashKeyValues.setUserId(userId); // Form a query DynamoDBQueryExpression<CartItem> queryExpression = new DynamoDBQueryExpression<CartItem>() .withHashKeyValues(hashKeyValues) .withQueryFilterEntry( "IsOrdered", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withN("0"))); // add query filter queryExpression.setHashKeyValues(hashKeyValues); // execute that query List<CartItem> itemList = mapper.query(CartItem.class, queryExpression); for (int i = 0; i < itemList.size(); i++) { System.out.println(itemList.get(i).getUserId()); System.out.println(itemList.get(i).getCartId()); System.out.println(itemList.get(i).getProductId()); System.out.println(itemList.get(i).getQuantity()); } // return the first item (we will have only one item) return itemList; }
@Override public Profile findOne(String profileName) { AmazonDynamoDB client = null; if (System.getProperty("useProxy") != null) { ClientConfiguration conf = new ClientConfiguration() .withProxyHost(System.getProperty("proxyHost")) .withProxyPort(Integer.valueOf(System.getProperty("proxyPort"))) .withProxyUsername(System.getProperty("proxyUsername")) .withProxyPassword(System.getProperty("proxyPassword")); client = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider(), conf); } else { client = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider()); } client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)); client.setEndpoint("https://dynamodb.ap-northeast-1.amazonaws.com"); DynamoDBMapper mapper = new DynamoDBMapper(client); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); Map<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition scanCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(profileName)); scanFilter.put("profile_name", scanCondition); scanExpression.setScanFilter(scanFilter); for (Profile p : mapper.scan(Profile.class, scanExpression)) { return p; } return null; }
public List<CartItem> placeOrder(int userId) { List<CartItem> cartItems = getCartItemsFromUserId(userId); DynamoDBMapper mapper = this.conn.getMapper(); for (CartItem cartItem : cartItems) { cartItem.setIsOrdered(1); mapper.save(cartItem); } return cartItems; }
@Override public void requirePasswordChange(AppUser user, boolean require) throws NoSuchUserException { user = db.load(AppUser.class, user.getEmail()); if (user != null) { user.setPasswordChangeRequired(require); db.save(user); } else { throw new NoSuchUserException(user.getEmail()); } }
@Override public void setLocked(AppUser user, boolean locked) throws NoSuchUserException { user = db.load(AppUser.class, user.getEmail()); if (user != null) { user.setLocked(locked); db.save(user); } else { throw new NoSuchUserException(user.getEmail()); } }
/** * Saves Item into the cart. - DynamoDB. * * @param form */ public void saveCartItem(AddToCartForm form) { DynamoDBMapper mapper = this.conn.getMapper(); CartItem item = new CartItem(); item.setCartId(UUID.randomUUID().getLeastSignificantBits()); item.setProductId(form.getProductId()); item.setQuantity(form.getQuantity()); item.setUserId(form.getUserId()); item.setIsOrdered(form.getIsOrdered()); mapper.save(item); }
@Override public void revokeRole(AppUser user, String role) throws NoSuchUserException { user = db.load(AppUser.class, user.getEmail()); if (user != null) { user.revokeRole(role); db.save(user); } else { throw new NoSuchUserException(user.getEmail()); } }
/* * Deletes the specified user and all of its attribute/value pairs. */ public static void deleteUser(UserPreference deleteUserPreference) { AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { mapper.delete(deleteUserPreference); } catch (AmazonServiceException ex) { LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } }
public SimDetails displayDetails() { sim.setUserName("Pratheeth"); sim.setCountry("US"); sim.setExpiryDate("01/05/2016"); sim.setSimType("Prepaid"); mapper.save(sim); getSimDetails = mapper.load(SimDetails.class, "US", "Prasoon"); return getSimDetails; }
@Test public void utf8Test() { createGenericTable(hashTableName, "code"); TestClassWithHashKey value = new TestClassWithHashKey(); value.setCode("éáűőúöüóí"); value.setStringData("űáéúőóüöí"); mapper.save(value); TestClassWithHashKey readValue = mapper.load(TestClassWithHashKey.class, "éáűőúöüóí"); Assert.assertEquals("éáűőúöüóí", readValue.getCode()); Assert.assertEquals("űáéúőóüöí", readValue.getStringData()); }
private void updateNodeHistoryItem(String nid, String time, String values) { NodeHistory itemRetrieved; if (time != null) { itemRetrieved = mapper.load(NodeHistory.class, nid, time); itemRetrieved.setTime(time); } else itemRetrieved = mapper.load(NodeHistory.class, nid); itemRetrieved.setSAValues(values); System.out.println("Item updated: "); System.out.format( "SerialNumber=%s, Time=%s, SAValues=%s\n", itemRetrieved.getNodeID(), itemRetrieved.getTime(), itemRetrieved.getSAValues()); }
/* * Retrieves all of the attribute/value pairs for the specified user. */ public static UserPreference getUserPreference(int userNo) { AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { UserPreference userPreference = mapper.load(UserPreference.class, userNo); return userPreference; } catch (AmazonServiceException ex) { LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } return null; }
@Test public void queryWithUnknownHashRangeKey2() { putItemWithHashKeyAndRangeKey(); String hashCode = "hash2"; Condition rangeKeyCondition = new Condition(); List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>(); attributeValueList.add(new AttributeValue().withS("range2x")); attributeValueList.add(new AttributeValue().withS("range2y")); rangeKeyCondition.setAttributeValueList(attributeValueList); rangeKeyCondition.setComparisonOperator(ComparisonOperator.BETWEEN); TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey(); hashKeyTemplate.setHashCode(hashCode); DynamoDBQueryExpression<TestClassWithHashRangeKey> query = new DynamoDBQueryExpression<TestClassWithHashRangeKey>() .withHashKeyValues(hashKeyTemplate) .withRangeKeyCondition("rangeCode", rangeKeyCondition); List<TestClassWithHashRangeKey> valueList = mapper.query(TestClassWithHashRangeKey.class, query); Assert.assertNotNull("Value list is null.", valueList); Assert.assertEquals("Value list should be empty.", 0, valueList.size()); }
@Test public void queryWithHashKey() { putItemWithHashKey(); putItemWithHashKeyOverwriteItem(); String code = "hash1"; TestClassWithHashKey hashKeyTemplate = new TestClassWithHashKey(); hashKeyTemplate.setCode(code); Map<String, Condition> emptyRangeConditions = new HashMap<String, Condition>(); DynamoDBQueryExpression<TestClassWithHashKey> query = new DynamoDBQueryExpression<TestClassWithHashKey>() .withHashKeyValues(hashKeyTemplate) .withRangeKeyConditions(emptyRangeConditions); List<TestClassWithHashKey> valueList = mapper.query(TestClassWithHashKey.class, query); Assert.assertNotNull("Value list is null.", valueList); Assert.assertNotSame("Value list is empty.", 0, valueList.size()); Assert.assertEquals("Value list has more than one item.", 1, valueList.size()); TestClassWithHashKey value = valueList.get(0); Assert.assertEquals("Wrong code.", code, value.getCode()); Assert.assertEquals("Wrong stringData.", "string1", value.getStringData()); Assert.assertEquals("Wrong intData.", 1, value.getIntData()); Assert.assertEquals( "Wrong stringSetData.", set("stringSetVal1", "stringSetVal2"), value.getStringSetData()); Assert.assertEquals("Wrong numberSetData.", set(1, 2), value.getNumberSetData()); }
/** * Drain the queue of pending counts into the provided buffer and write those counts to DynamoDB. * This blocks until data is available in the queue. * * @param buffer A reusable buffer with sufficient space to drain the entire queue if necessary. * This is provided as an optimization to avoid allocating a new buffer every interval. * @throws InterruptedException Thread interrupted while waiting for new data to arrive in the * queue. */ protected void sendQueueToDynamoDB(List<HttpReferrerPairsCount> buffer) throws InterruptedException { // Block while waiting for data buffer.add(counts.take()); // Drain as much of the queue as we can. // DynamoDBMapper will handle splitting the batch sizes for us. counts.drainTo(buffer); try { long start = System.nanoTime(); // Write the contents of the buffer as items to our table List<FailedBatch> failures = mapper.batchWrite(buffer, Collections.emptyList()); long end = System.nanoTime(); LOG.info( String.format( "%d new counts sent to DynamoDB in %dms", buffer.size(), TimeUnit.NANOSECONDS.toMillis(end - start))); for (FailedBatch failure : failures) { LOG.warn( "Error sending count batch to DynamoDB. This will not be retried!", failure.getException()); } } catch (Exception ex) { LOG.error("Error sending new counts to DynamoDB. The some counts may not be persisted.", ex); } }
// range format example = (7L*24L*60L*60L*1000L) (1 week) public List<NodeHistoryEntity> findSpecificNodeHistoryInRangeTime(String nid, Long range) { String hashKey = nid; SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MMdd'T'HH:mm:ss.SSS'Z'"); Long currentTimeMilli = (new Date()).getTime(); Long rangeTimeMilli = (new Date()).getTime() - range; dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); String currentTime = dateFormatter.format(currentTimeMilli); String rangeTime = dateFormatter.format(rangeTimeMilli); NodeHistory nhKey = new NodeHistory(); nhKey.setNodeID(hashKey); List<NodeHistory> latestNodeHistory; Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.BETWEEN.toString()) .withAttributeValueList( new AttributeValue().withS(currentTime), new AttributeValue().withS(rangeTime)); DynamoDBQueryExpression<NodeHistory> queryExpression = new DynamoDBQueryExpression<NodeHistory>() .withHashKeyValues(nhKey) .withRangeKeyCondition("Time", rangeKeyCondition); latestNodeHistory = mapper.query(NodeHistory.class, queryExpression); return this.transform(latestNodeHistory); }
// returns list of items ordered so far by the user public CartItem decreaseQuantity(Long cartId) { DynamoDBMapper mapper = this.conn.getMapper(); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); Map<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition scanCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withN(cartId.toString())); scanFilter.put("CartId", scanCondition); scanExpression.setScanFilter(scanFilter); PaginatedScanList<CartItem> items = mapper.scan(CartItem.class, scanExpression); CartItem item = items.get(0); item.setQuantity(item.getQuantity() - 1); mapper.save(item); return item; }
@Test public void getUnknownHashItemTest() { createTable(hashTableName, createStringAttributeDefinition("code")); String code = "hash1x"; TestClassWithHashKey value = mapper.load(TestClassWithHashKey.class, code); Assert.assertNull("Value should not be found.", value); }
/* * Inserts ten users with userNo from 1 to 10 and random names. */ public static void insertUsers() { AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { for (int i = 1; i <= 10; i++) { UserPreference userPreference = new UserPreference(); userPreference.setUserNo(i); userPreference.setFirstName(Constants.getRandomName()); userPreference.setLastName(Constants.getRandomName()); mapper.save(userPreference); } } catch (AmazonServiceException ex) { LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } }
@Override public String requestPasswordReset(AppUser _user) throws NoSuchUserException { AppUser user = db.load(AppUser.class, _user.getEmail()); if (user == null) { throw new NoSuchUserException(_user.getEmail()); } String signedToken = SignedToken.sign( _user.getEmail(), "hello", // TODO: Use KmsDao PASS_RESET_VALIDITY_MILLIS); user.setPasswordResetToken(signedToken); db.save(user); return signedToken; }
public void removeItemFromCart(Long cartId) { DynamoDBMapper mapper = this.conn.getMapper(); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); Map<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition scanCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withN(cartId.toString())); scanFilter.put("CartId", scanCondition); scanExpression.setScanFilter(scanFilter); System.out.println("Going to retrieve item."); PaginatedScanList<CartItem> items = mapper.scan(CartItem.class, scanExpression); System.out.println("Number of items retrived. " + items.size()); System.out.println("Going to remove a cartItem with productId: " + items.get(0).getProductId()); mapper.delete(items.get(0)); }
@Test public void getUnknownHashRangeItemTest() { createTable( hashRangeTableName, createStringAttributeDefinition("hashCode"), createStringAttributeDefinition("rangeCode")); String hashCode = "hash2x"; String rangeCode = "range2"; TestClassWithHashRangeKey value = mapper.load(TestClassWithHashRangeKey.class, hashCode, rangeCode); Assert.assertNull("Value should not be found (" + hashCode + "/" + rangeCode, value); hashCode = "hash2"; rangeCode = "range2x"; value = mapper.load(TestClassWithHashRangeKey.class, hashCode, rangeCode); Assert.assertNull("Value should not be found (" + hashCode + "/" + rangeCode, value); }
public void createSimTradeTable() { CreateTableRequest createTableRequest = mapper.generateCreateTableRequest(SimDetails.class); // Table provision throughput is still required since it cannot be // specified in your POJO createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L)); // Fire off the CreateTableRequest using the low-level client amazonDynamoDBClient.createTable(createTableRequest); }