Java 类com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec 实例源码

项目:grassroot-platform    文件:LocationInfoBrokerImpl.java   
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");
    }
}
项目:aws-dynamodb-examples    文件:DocumentAPIItemBinaryExample.java   
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");
}
项目:orbit-dynamodb    文件:DynamoDBStorageExtension.java   
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;
                }
            });
}
项目:java-persistence    文件:DdbIndex.java   
@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);
}
项目:Cheddar    文件:DynamoDocumentStoreTemplateTest.java   
@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());
}
项目:Cheddar    文件:DynamoDocumentStoreTemplateTest.java   
@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);
}
项目:Cheddar    文件:DynamoDocumentStoreTemplateTest.java   
@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);
}