com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentAPIItemBinaryExample.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #2
Source File: DynamoSpaceConfigClient.java    From xyz-hub with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the relationship between a space and their packages
 *
 * @param marker used in logs
 * @param space the spaceId which is being deleted
 */
private void deleteSpaceFromPackage(Marker marker, Space space) throws AmazonDynamoDBException {
  if (space == null) {
    return;
  }

  try {
    logger.info(marker, "Removing packages from space ID: {}", space.getId());
    final List<String> packagesList = new ArrayList<>();

    if (space.getPackages() != null) {
      packagesList.addAll(space.getPackages());
    } else {
      final GetItemSpec spec = new GetItemSpec().withPrimaryKey("id", space.getId()).withProjectionExpression("packages");
      final Item item = spaces.getItem(spec);
      if (item != null && item.isPresent("packages")) {
        packagesList.addAll(item.getList("packages"));
      }
    }

    logger.info(marker, "Packages {} to be removed from space ID: {}", packagesList, space.getId());
    for (String packageName : packagesList) {
      packages.deleteItem("packageName", packageName, "spaceId", space.getId());
    }
  } catch (AmazonDynamoDBException e) {
    logger.error(marker, "Failure deleting space ID: {} from the packages table", space.getId(), e);
    throw e;
  }
}
 
Example #3
Source File: MoviesItemOps02.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            .build();

        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");

        int year = 2015;
        String title = "The Big New Movie";

        GetItemSpec spec = new GetItemSpec().withPrimaryKey("year", year, "title", title);

        try {
            System.out.println("Attempting to read the item...");
            Item outcome = table.getItem(spec);
            System.out.println("GetItem succeeded: " + outcome);

        }
        catch (Exception e) {
            System.err.println("Unable to read item: " + year + " " + title);
            System.err.println(e.getMessage());
        }

    }
 
Example #4
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #5
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: DocumentAPIItemBinaryExample.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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");
    }