日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

dynamodb java_使用Java更新DynamoDB项

發布時間:2023/12/3 java 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dynamodb java_使用Java更新DynamoDB项 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

dynamodb java

在上一篇文章中,我們繼續使用Java將項目插入DynamoDB。 DynamoDB還支持更新項目。

我們將使用Login表獲取更新示例。
發布更新時,必須指定要更新的項目的主鍵。

public void updateName(String email,String fullName) {Map<String,AttributeValue> attributeValues = new HashMap<>();attributeValues.put("email",new AttributeValue().withS(email));attributeValues.put("fullname",new AttributeValue().withS(fullName));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).addKeyEntry("email",new AttributeValue().withS(email)).addAttributeUpdatesEntry("fullname",new AttributeValueUpdate().withValue(new AttributeValue().withS(fullName)));UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}

我們可以使用條件更新來處理更高級的語句。 有條件的更新可以在許多情況下為我們提供幫助,例如處理并發更新。

我們可以通過使用普通表達式來實現。

public void updateConditionallyWithExpression(String email,String fullName,String prefix) {Map<String, AttributeValue> key = new HashMap<>();key.put("email", new AttributeValue().withS(email));Map<String, AttributeValue> attributeValues = new HashMap<>();attributeValues.put(":prefix", new AttributeValue().withS(prefix));attributeValues.put(":fullname", new AttributeValue().withS(fullName));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).withUpdateExpression("set fullname = :fullname").withConditionExpression("begins_with(fullname,:prefix)").withExpressionAttributeValues(attributeValues);UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}

或通過指定屬性。

public void updateConditionallyWithAttributeEntries(String email, String fullName, String prefix){Map<String,AttributeValue> key = new HashMap<>();key.put("email",new AttributeValue().withS(email));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).addAttributeUpdatesEntry("fullname",new AttributeValueUpdate().withValue(new AttributeValue().withS(fullName)).withAction(AttributeAction.PUT)).addExpectedEntry("fullname",new ExpectedAttributeValue().withValue(new AttributeValue().withS(prefix)).withComparisonOperator(ComparisonOperator.BEGINS_WITH));UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}

另一個功能是原子計數器。 我們可以發布DynamoDB項目的更新并增加屬性值。 我們將添加一個額外的字段,稱為count。 另外,我們將添加另一個更新功能。 一旦調用,該函數將更新指定的字段,但也會增加計數器屬性。 因此,counter屬性將表示對特定項目執行了多少次更新。

public void addUpdateCounter(String email) {Map<String,AttributeValue> key = new HashMap<>();key.put("email",new AttributeValue().withS(email));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).addAttributeUpdatesEntry("counter",new AttributeValueUpdate().withValue(new AttributeValue().withN("0")).withAction(AttributeAction.PUT));UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}public void updateAndIncreaseCounter(String email,String fullname) {Map<String,AttributeValue> key = new HashMap<>();key.put("email",new AttributeValue().withS(email));UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(TABLE_NAME).withKey(key).addAttributeUpdatesEntry("fullname",new AttributeValueUpdate().withValue(new AttributeValue().withS(fullname)).withAction(AttributeAction.PUT)).addAttributeUpdatesEntry("counter",new AttributeValueUpdate().withValue(new AttributeValue().withN("1")).withAction(AttributeAction.ADD));UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);}

您可以在github上找到源代碼。

翻譯自: https://www.javacodegeeks.com/2016/08/update-dynamodb-items-java.html

dynamodb java

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的dynamodb java_使用Java更新DynamoDB项的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。