2011-02-13 26 views
3

我想一個新的項目添加到亞馬遜的SimpleDB域只有在有沒有使用相同的項目名稱另一個項目。的SimpleDB:只插入項目,如果它不存在

我知道如何做到這一點的一個屬性。但我想進行檢查的項目名稱,以確保它是獨特的,它不會覆蓋現有項目 - 無需額外的選擇查詢,當然。

實施例用於檢查屬性:

https://sdb.amazonaws.com/ 
?Action=PutAttributes 
&DomainName=MyDomain 
&ItemName=JumboFez 
&Attribute.1.Name=quantity 
&Attribute.1.Value=14 
&Attribute.1.Replace=true 
&Expected.1.Name=quantity 
&Expected.1.Exists=false 
&AWSAccessKeyId=[valid access key id] 
[...] 

按照FAQ這應該是可能的:

「這些語義還可以用於實現的功能,如計數器,插入一個項目只如果它不存在[...]「

回答

-1

您可以生成基於curren的項目名稱時間戳。 並使用類似%的方法來獲取結果。

2

你是正確的,你可以使用條件放來實現這一SimpleDB Docs conditional puts

本質上的項目,你知道你將有可能與條件更新存在確認被用來確保把只有當屬性不針對特定ITEMNAME存在發生任何屬性。

在Java中使用亞馬遜的Java SDK,這將是這個樣子: -

// add a conditional check to ensure that the specified profile does not already 
// exist - if there's a timestamp, then this profile already exists 
UpdateCondition condition = new UpdateCondition("quantity", null, false); 

final PutAttributesRequest request = new PutAttributesRequest().withDomainName("MyDomain"); 
request.setItemName("JumboFez"); 
request.setAttributes(attributes); 
request.setExpected(condition); 

sdb.putAttributes (request); 

基本上降低到你寫什麼,會做你想要只要你永遠不會有它有一個有效的項目是什麼沒有'數量'屬性。

可能值得注意的是,在您的示例中,您已將替換設置爲true,並且如果您使用條件式插入來確保項目是新項目,則不需要設置replace = true(根據文檔和經驗證據,替換=真實成本超過替換=假,需要更長時間)。

+0

這不回答他的問題都沒有。他想知道如何在itemName本身上應用條件更新語義。你所描述的只是如何在任何其他屬性上做到這一點 – Senkwe 2013-12-01 06:37:20

相關問題