private List<String> getFromDynamo(String dataSetLabel, String field, boolean sort) { DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); Table geoApiTable = dynamoDB.getTable("geo_apis"); GetItemSpec spec = new GetItemSpec() .withPrimaryKey("data_set_label", dataSetLabel) .withProjectionExpression(field); try { log.info("trying to read the data set info with key {}, field {}", dataSetLabel, field); Item outcome = geoApiTable.getItem(spec); log.info("got the outcome, looks like: {}", outcome); Set<String> resultSet = outcome.getStringSet(field); return resultSet == null ? new ArrayList<>() : sort ? resultSet.stream().sorted().collect(Collectors.toList()) : new ArrayList<>(resultSet); } catch (Exception e) { log.error("Error!", e); throw new IllegalArgumentException("No results for that dataset and field"); } }
public static void retrieveItem(String threadId, String replyDateTime) throws IOException { Table table = dynamoDB.getTable(tableName); GetItemSpec spec = new GetItemSpec() .withPrimaryKey("Id", threadId, "ReplyDateTime", replyDateTime) .withConsistentRead(true); Item item = table.getItem(spec); // Uncompress the reply message and print String uncompressed = uncompressString(ByteBuffer.wrap(item.getBinary("ExtendedMessage"))); System.out.println("Reply message:\n" + " Id: " + item.getString("Id") + "\n" + " ReplyDateTime: " + item.getString("ReplyDateTime") + "\n" + " PostedBy: " + item.getString("PostedBy") + "\n" + " Message: " + item.getString("Message") + "\n" + " ExtendedMessage (uncompressed): " + uncompressed + "\n"); }
public Task<Boolean> readState(final RemoteReference<?> reference, final Object state, final Class<?> stateClass) { final ObjectMapper mapper = dynamoDBConnection.getMapper(); final String tableName = getTableName(RemoteReference.getInterfaceClass(reference), stateClass); final String itemId = generateDocumentId(reference, stateClass); return DynamoDBUtils.getTable(dynamoDBConnection, tableName) .thenApply(table -> { GetItemSpec getItemSpec = new GetItemSpec() .withPrimaryKey(DynamoDBUtils.FIELD_NAME_PRIMARY_ID, itemId) .withConsistentRead(true); return table.getItem(getItemSpec); }) .thenApply(item -> { if (item != null) { readStateInternal(state, stateClass, item, mapper); return true; } else { return false; } }); }
@Override public <V> V getItem(Object hk, Object rk, Collection<String> attrNames, Class<V> type) { GetItemSpec spec = new GetItemSpec() .withPrimaryKey(toPrimaryKey(asIndexKey(hk, rk))); if ( null != attrNames ) { spec.withAttributesToGet(attrNames.toArray(new String[attrNames.size()])); } Item item = maybeBackoff(true, () -> _getItem.getItem(spec)); if ( null == item ) return null; return fromItem(_encryption.decrypt(item), type); }
@Test public void shouldRead_withItemIdAndItemClass() throws Exception { // Given final ItemId itemId = new ItemId(randomId()); final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName); final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration); when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations); final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate( mockDatabaseSchemaHolder); dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient); final Table mockTable = mock(Table.class); when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable); final Item mockTableItem = mock(Item.class); when(mockTable.getItem(any(GetItemSpec.class))).thenReturn(mockTableItem); final StubItem stubItem = generateRandomStubItem(itemId); when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem)); // When final StubItem returnedItem = dynamoDocumentStoreTemplate.read(itemId, StubItem.class); // Then final ArgumentCaptor<GetItemSpec> getItemRequestCaptor = ArgumentCaptor.forClass(GetItemSpec.class); verify(mockTable).getItem(getItemRequestCaptor.capture()); final GetItemSpec spec = getItemRequestCaptor.getValue(); assertEquals(1, spec.getKeyComponents().size()); assertEquals(itemId.value(), spec.getKeyComponents().iterator().next().getValue()); assertEquals(itemId.value(), returnedItem.getId()); assertEquals(stubItem.getStringProperty(), returnedItem.getStringProperty()); assertEquals(stubItem.getStringProperty2(), returnedItem.getStringProperty2()); assertEquals(stubItem.getStringSetProperty(), returnedItem.getStringSetProperty()); }
@Test public void shouldNotRead_withNonExistentItemExceptionNoItem() throws Exception { // Given final ItemId itemId = new ItemId(randomId()); final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName); final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration); when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations); final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate( mockDatabaseSchemaHolder); dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient); final Table mockTable = mock(Table.class); when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable); when(mockTable.getItem(any(GetItemSpec.class))).thenReturn(null); NonExistentItemException thrownException = null; // When try { dynamoDocumentStoreTemplate.read(itemId, StubItem.class); } catch (final NonExistentItemException nonExistentItemException) { thrownException = nonExistentItemException; } // Then assertNotNull(thrownException); }
@Test public void shouldNotRead_withNonExistentItemExceptionNoContent() throws Exception { // Given final ItemId itemId = new ItemId(randomId()); final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName); final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration); when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations); final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate( mockDatabaseSchemaHolder); dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient); final Table mockTable = mock(Table.class); when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable); final Item mockTableItem = mock(Item.class); when(mockTable.getItem(any(GetItemSpec.class))).thenReturn(mockTableItem); when(mockTableItem.toJSON()).thenReturn(""); NonExistentItemException thrownException = null; // When try { dynamoDocumentStoreTemplate.read(itemId, StubItem.class); } catch (final NonExistentItemException nonExistentItemException) { thrownException = nonExistentItemException; } // Then assertNotNull(thrownException); }