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

项目:outland    文件:DefaultGroupStorage.java   
@Override public Void create(Group group) {
  Item item = preparePutItem(group);

  PutItemSpec putItemSpec = new PutItemSpec()
      .withItem(item)
      .withConditionExpression("attribute_not_exists(#ns_key)")
      .withNameMap(new NameMap().with("#ns_key", HASH_KEY));

  Table table = dynamoDB.getTable(groupTableName);
  final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> {
    try {
      return table.putItem(putItemSpec);
    } catch (ConditionalCheckFailedException e) {
      throwConflictAlreadyExists(group);
      return null;
    }
  };
  return putItem(group, putItemOutcomeSupplier);
}
项目:serverless-cf-analysis    文件:CreateAthenaPartitionsBasedOnS3EventWithDDB.java   
private boolean tryAddMissingPartition(String dyanmoDBTaableName,DynamoDB dynamoDBClient, Partition partition){

        Table ddbTable= dynamoDBClient.getTable(dyanmoDBTaableName);

        Item item=new Item()
                .withPrimaryKey("PartitionSpec",partition.spec())
                .withString("PartitionPath",partition.path())
                .withString("PartitionName", partition.name());

        PutItemSpec itemSpec=new PutItemSpec()
                .withItem(item)
                .withConditionExpression("attribute_not_exists(#ps)")
                .withNameMap(new NameMap()
                        .with("#ps","PartitionSpec"));

        try{
            ddbTable.putItem(itemSpec);
            System.out.println("Item was added to the table.PartitionSpec="+partition.spec()+"; Path="+partition.path());
            return true;
        }
        catch(ConditionalCheckFailedException e){
            System.out.println(e.toString());
            System.out.println("Item already exists. PartitionSpec="+partition.spec()+"; Path="+partition.path());
            return false;
        }
    }
项目:outland    文件:DefaultFeatureStorage.java   
@Override public Void createFeature(Feature feature) {

    final String key = feature.getKey();
    final String group = feature.getGroup();
    final Item item = preparePutItem(feature);

    final PutItemSpec putItemSpec = new PutItemSpec()
        .withItem(item)
        .withConditionExpression("attribute_not_exists(#featurekey)")
        .withNameMap(new NameMap().with("#featurekey", RANGE_KEY));

    final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> {
      try {
        return dynamoDB.getTable(featureTableName).putItem(putItemSpec);
      } catch (ConditionalCheckFailedException e) {
        logger.error("err=conflict_feature_already_exists feature_key={} {}", feature.getKey(),
            e.getMessage());
        throwConflictAlreadyExists(feature);
        return null;
      }
    };

    final DynamoDbCommand<PutItemOutcome> cmd = new DynamoDbCommand<>("createFeature",
        putItemOutcomeSupplier,
        () -> {
          throw new RuntimeException("createFeature");
        },
        hystrixWriteConfiguration,
        metrics);

    final PutItemOutcome outcome = cmd.execute();

    logger.info("{} /dynamodb_put_item_result=[{}]",
        kvp("op", "createFeature", HASH_KEY, group, RANGE_KEY, key, "result", "ok"),
        outcome.getPutItemResult().toString());

    return null;
  }
项目:java-persistence    文件:DdbIndex.java   
@Override
public T putItem(T item) {
    PutItemSpec req = new PutItemSpec()
        .withItem(_encryption.encrypt(toItem(item)))
        .withReturnValues(ReturnValue.ALL_OLD);
    return fromItem(
        _encryption.decrypt(
            maybeBackoff(false, () -> _putItem.putItem(req)).getItem()),
        _clazz);
}
项目:Cheddar    文件:DynamoDocumentStoreTemplateTest.java   
@Test
public void shouldCreate_withItem() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Item mockTableItem = mock(Item.class);
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    // When
    final StubItem returnedItem = dynamoDocumentStoreTemplate.create(stubItem);

    // Then
    final ArgumentCaptor<PutItemSpec> getItemRequestCaptor = ArgumentCaptor.forClass(PutItemSpec.class);
    verify(mockTable).putItem(getItemRequestCaptor.capture());

    final PutItemSpec spec = getItemRequestCaptor.getValue();
    assertEquals(itemId.value(), spec.getItem().get("id"));

    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 shouldCreate_withItemWithNullProperty() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);
    stubItem.setStringProperty2(null);

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Item mockTableItem = mock(Item.class);
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    // When
    dynamoDocumentStoreTemplate.create(stubItem);

    // Then
    final ArgumentCaptor<PutItemSpec> getItemRequestCaptor = ArgumentCaptor.forClass(PutItemSpec.class);
    verify(mockTable).putItem(getItemRequestCaptor.capture());

    final PutItemSpec spec = getItemRequestCaptor.getValue();
    assertFalse(spec.getItem().hasAttribute("stringProperty2"));
}
项目:Cheddar    文件:DynamoDocumentStoreTemplateTest.java   
@Test
public void shouldNotCreate_withItem() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Item mockTableItem = mock(Item.class);
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    doThrow(RuntimeException.class).when(mockTable).putItem(any(PutItemSpec.class));
    RuntimeException thrownException = null;

    // When
    try {
        dynamoDocumentStoreTemplate.create(stubItem);
    } catch (final RuntimeException runtimeException) {
        thrownException = runtimeException;
    }

    // Then
    assertNotNull(thrownException);
}
项目:Cheddar    文件:DynamoDocumentStoreTemplateTest.java   
@Test
public void shouldUpdate_withItem() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);
    final StubItem previousStubItem = generateRandomStubItem(itemId);
    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    final Table mockTable = mock(Table.class);
    final Item mockTableItem = mock(Item.class);
    final PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.addComponent("id", itemId.value());
    final Item previousItem = mock(Item.class);

    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);
    when(mockDynamoDBClient.getTable(schemaName + "." + tableName)).thenReturn(mockTable);
    when(mockTable.getItem(any(PrimaryKey.class))).thenReturn(previousItem);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    when(previousItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(previousStubItem));
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    // When
    final StubItem returnedItem = dynamoDocumentStoreTemplate.update(stubItem);

    // Then
    final ArgumentCaptor<PutItemSpec> putItemRequestCaptor = ArgumentCaptor.forClass(PutItemSpec.class);
    verify(mockTable).putItem(putItemRequestCaptor.capture());
    final PutItemSpec spec = putItemRequestCaptor.getValue();
    assertEquals(itemId.value(), spec.getItem().get("id"));
    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 shouldNotUpdate_withPutItemException() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);
    final StubItem previousStubItem = generateRandomStubItem(itemId);
    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    final Table mockTable = mock(Table.class);
    final Item mockTableItem = mock(Item.class);
    final PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.addComponent("id", itemId.value());
    final Item previousItem = mock(Item.class);

    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);
    when(mockDynamoDBClient.getTable(schemaName + "." + tableName)).thenReturn(mockTable);
    when(mockTable.getItem(any(PrimaryKey.class))).thenReturn(previousItem);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    when(previousItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(previousStubItem));
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));
    when(mockTable.putItem(any(PutItemSpec.class))).thenThrow(ConditionalCheckFailedException.class);

    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    // When
    OptimisticLockException thrownException = null;
    try {
        dynamoDocumentStoreTemplate.update(stubItem);
    } catch (final OptimisticLockException optimisticLockException) {
        thrownException = optimisticLockException;
    }

    // Then
    assertNotNull(thrownException);
}
项目:aws-dynamodb-examples    文件:MoviesItemOps02.java   
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";

        final Map<String, Object> infoMap = new HashMap<String, Object>();
        infoMap.put("plot",  "Nothing happens at all.");
        infoMap.put("rating",  0.0);
        Item item = new Item()
            .withPrimaryKey(new PrimaryKey("year", year, "title", title))
            .withMap("info", infoMap);

        // Attempt a conditional write.  We expect this to fail.

        PutItemSpec putItemSpec = new PutItemSpec()
            .withItem(item)
            .withConditionExpression("attribute_not_exists(#yr) and attribute_not_exists(title)")
            .withNameMap(new NameMap()
                .with("#yr", "year"));

        System.out.println("Attempting a conditional write...");
        try {
            table.putItem(putItemSpec);
            System.out.println("PutItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty());
        } catch (ConditionalCheckFailedException e) {
            e.printStackTrace(System.err);
            System.out.println("PutItem failed");
        }
    }
项目:outland    文件:DefaultFeatureStorage.java   
@Override public Void
updateFeature(Feature feature, FeatureVersion previousVersion) {
  logger.info("{}",
      kvp("op", "updateFeature", HASH_KEY, feature.getGroup(), RANGE_KEY, feature.getKey()));

  final String key = feature.getKey();
  final String group = feature.getGroup();
  final Item item = preparePutItem(feature);

  final PutItemSpec putItemSpec = new PutItemSpec()
      .withItem(item)
      .withExpected(
          new Expected("version_timestamp").eq(previousVersion.getTimestamp()),
          new Expected("version_counter").eq(previousVersion.getCounter())
      );

  final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> {
    try {
      return dynamoDB.getTable(featureTableName).putItem(putItemSpec);
    } catch (ConditionalCheckFailedException e) {
      logger.error("err=conflict_feature_version_mismatch feature_key={} {}", feature.getKey(),
          e.getMessage());
      throwConflictVersionMismatch(feature);
      return null;
    }
  };

  final DynamoDbCommand<PutItemOutcome> cmd = new DynamoDbCommand<>("updateFeature",
      putItemOutcomeSupplier,
      () -> {
        throw new RuntimeException("updateFeature");
      },
      hystrixWriteConfiguration,
      metrics);

  final PutItemOutcome outcome = cmd.execute();

  logger.info("{} /dynamodb_update_item_result=[{}]",
      kvp("op", "updateFeature", HASH_KEY, group, RANGE_KEY, key, "result", "ok"),
      outcome.getPutItemResult().toString());

  return null;
}