在上一篇文章中,我们继续使用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

使用Java更新DynamoDB项相关推荐

  1. dynamodb java_使用Java更新DynamoDB项

    dynamodb java 在上一篇文章中,我们继续使用Java将项目插入DynamoDB. DynamoDB还支持更新项目. 我们将使用Login表获取更新示例. 发布更新时,必须指定要更新的项目的 ...

  2. dynamodb java_使用Java查询DynamoDB项

    dynamodb java 在上一篇文章中,我们继续在DynamoDB数据库上插入数据. 在本教程中,我们将对DynamoDB表发出一些基本查询. 主要规则是每个查询都必须使用哈希键. 查询的最简单形 ...

  3. 使用Java查询DynamoDB项

    在上一篇文章中,我们继续在DynamoDB数据库上插入数据. 在本教程中,我们将对DynamoDB表发出一些基本查询. 主要规则是每个查询都必须使用哈希键. 查询的最简单形式是仅使用哈希键. 我们将在 ...

  4. dynamodb java_使用Java第2部分查询DynamoDB项

    dynamodb java 在上一篇文章中,我们有机会发布了一些基本的DynamoDB查询操作. 但是,除了基本操作之外,DynamoDB api还为我们提供了一些额外的功能. 投影是具有类似选择功能 ...

  5. 使用Java第2部分查询DynamoDB项

    在上一篇文章中,我们有机会发布了一些基本的DynamoDB查询操作. 但是,除了基本操作之外,DynamoDB api还为我们提供了一些额外的功能. 投影是具有类似选择功能的功能. 您选择应从Dyna ...

  6. java 程序更新_如何关闭JAVA更新程序

    Win7系统上安装JAVA JRE或JDK后,系统就会启动一个jusched进程,定时检查JRE或JDK更新包,跟Adobe Reader.FlashPlayer一样烦人,每次开机,或是定时每天去搞个 ...

  7. java 停止更新_如何关闭JAVA更新程序

    Win7系统上安装JAVA JRE或JDK后,系统就会启动一个jusched进程,定时检查JRE或JDK更新包,跟Adobe Reader.FlashPlayer一样烦人,每次开机,或是定时每天去搞个 ...

  8. 【Linux 内核 内存管理】RCU 机制 ④ ( RCU 模式下更新链表项 list_replace_rcu 函数 | 链表操作时使用 smp_wmb() 函数保证代码执行顺序 )

    文章目录 一.RCU 模式下更新链表项 list_replace_rcu 函数 二.链表操作时使用 smp_wmb() 函数保证代码执行顺序 一.RCU 模式下更新链表项 list_replace_r ...

  9. 采用Lists.UpdateListItems方法更新列表项各种类型值的写法

    给SharePoint列表更新列表项的做法很多,最常用的就是调用Microsoft.SharePoint.dll中的对象,但是这样的程序只能在MOSS服务器上运行,如果在客户端呢,只能用Lists.U ...

最新文章

  1. 云计算岗位40个面试题
  2. c语言学习-输入一个十进制数,输出其对应的八进制数据
  3. Facebook Connect Magento Extension
  4. oracle 用户创建日期,oracle限制一个用户空闲时间
  5. 李彦宏:5年后语音和图片搜索会超文字搜索
  6. UVa 1588 - Kickdown(BUG)
  7. 简单的shell命令
  8. Hive数仓基本概念介绍
  9. asppython份额_为什么JAVA份额那么高,存在感却不如Python?
  10. 手游服务器源码 https,python手游服务端搭建(转)
  11. windows安装tomcat8
  12. 计算机报名上传图片需要flash,公务员报名上传照片时需要的flash控件是什么
  13. 动态显示姓名--汇编语言版
  14. 科大讯飞语音开发包上手体验(1)
  15. 新西兰公民在中国大陆境内在线申请护照注意事项
  16. 微信可上线类型与封杀理由
  17. 简单手绘创意思维导图,思维导图软件
  18. QMS-云质-质量管理-质量控制中的常见误区(1): Cr=0
  19. 我的世界服务器名称被占用,【求助】【紧急】【大神】水桶服务器启动后显示端口被占用...
  20. 对二叉树、节点、度之间关系的思考(附图)

热门文章

  1. 关联分析:FP-Growth算法
  2. 我猜,每个程序员对着电梯都想过调度算法吧
  3. React中的模糊匹配与精准匹配
  4. 2016蓝桥杯省赛---java---B---2(生日蜡烛)
  5. 迷宫问题---递归解决
  6. 关于文档的基本操作---ElasticSearch
  7. 本地方法(JNI)——数值参数与返回值
  8. DFS应用——查找强分支
  9. 阅读副本和Spring Data第2部分:配置基础项目
  10. Neo4j导入:java.lang.IllegalStateException:不支持在单个导入中混合指定和未指定的组所有物...