2017-04-23 119 views
0

是否可以运行针对亚马逊DynamoDB的请求的updateItem用,如果该项目不存在,将永远成功的条件存在。例如:如何通过条件检查,如果项目不与AWS DynamoDB的updateItem要求

long timestampNow = System.currentTimeMillis(); 

UpdateItemSpec updateItemSpec = new UpdateItemSpec() 
     .withPrimaryKey("primary_key", theKey) 
     .withReturnValues(ReturnValue.ALL_NEW) 
     .withAttributeUpdate(
       new AttributeUpdate("my_attr").put(timestampNow+SOME_DURATION)) 
     .withExpected(
       new Expected("my_attr").lt(timestampNow)); 
try { 
    UpdateItemOutcome outcome = AWS.DYNAMO_DOC.getTable("my_table").updateItem(updateItemSpec); 
    return outcome.getUpdateItemResult().getAttributes(); 

} catch (ConditionalCheckFailedException e) { 
    return null; 
} 

当我做了的项目,目前还不存在请求时,它抛出ConditionalCheckFailedException,但我想它通过条件测试,只是继续创建项目。那可能吗?

回答

0

该文档实际上拥有了一切我需要知道:

long timestampNow = System.currentTimeMillis(); 

UpdateItemSpec updateItemSpec = new UpdateItemSpec() 
     .withPrimaryKey("primary_key", theKey) 
     .withReturnValues(ReturnValue.ALL_NEW) 
     .withConditionExpression("attribute_not_exists(my_attr) or my_attr < :now") 
     .withUpdateExpression("SET my_attr = :exp") 
     .withValueMap(new ValueMap() 
       .withLong(":now", timestampNow) 
       .withLong(":exp", timestampNow+LOCK_EXPIRY_DURATION)); 
try { 
    UpdateItemOutcome outcome = AWS.DYNAMO_DOC.getTable("my_table").updateItem(updateItemSpec); 
    return outcome.getUpdateItemResult().getAttributes(); 

} catch (ConditionalCheckFailedException e) { 
    return null; 
}