DynamoDBMapper用于条件保存

我正在使用DynamoDBMapper,并且当且仅当hashkey和range键组合不存在时,才想有条件地保存。 我知道有一些方法可以使用UUID来减少碰撞的可能性,但我想通过使用条件保存来保护自己。

我遇到了这篇使用DynamoDBSaveExpression的 文章但是我无法指定条件是“hashkey AND rangekey”不能存在。 API指定了withConditionalOperator方法,但我无法在我的课程中看到这个。 我也在这里使用最新的aws java sdk。

有关如何有条件保存的任何建议? 或者我可能做错了什么?

 DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression(); Map expectedAttributes = ImmutableMap.builder() .put("hashKey", new ExpectedAttributeValue(false)) .put("rangeKey", new ExpectedAttributeValue(false)) .build(); saveExpression.setExpected(expectedAttributes); saveExpression.setConditionalOperator(ConditionalOperator.AND); try { dynamoDBMapper.save(objectToSave, saveExpression); } catch (ConditionalCheckFailedException e) { //Handle conditional check } 

这使用public ExpectedAttributeValue(Boolean exists)构造函数,它只是在内部调用setExists