dynamoDB数据库

dynamoDB数据库首先安装 AWSCLI 安装后 在 cmd 输入 awsversion 检验是否安装成功然后 在 cmd 输入 awsconfigure 设置 aws 的密钥最后使用一下代码进行数据库的 CRUD 操作如果能帮助大家请手动点赞 packagecom skysoft modules controller importcom amazonaws services dynamodbv2 AmazonDyn

  • 首先安装AWSCLI
  • 安装后;在cmd输入 aws --version 检验是否安装成功
  • 然后;在cmd输入:aws configure设置aws的密钥
  • 最后使用一下代码进行数据库的CRUD操作
  • 如果能帮助大家请手动点赞
package com.skysoft.modules.controller; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.*; import com.amazonaws.services.dynamodbv2.document.spec.*; import com.amazonaws.services.dynamodbv2.document.utils.NameMap; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.model.*; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; / * 创建表Movies * Created by hyq on 2017/5/16. */ public class MoviesCreateTable { public static void main(String[] args) { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); //建立连接 DynamoDB dynamoDB = new DynamoDB(client); //表名 String tableName = "Movies"; try { System.out.println("Attempting to create table; please wait..."); //创建表 Table table = dynamoDB.createTable(tableName, Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // KeySchemaElement 用于主键的属性 new KeySchemaElement("title", KeyType.RANGE)), // Sort key Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N), new AttributeDefinition("title", ScalarAttributeType.S)),//键架构属性的数据类型。 new ProvisionedThroughput(10L, 10L)); table.waitForActive(); System.out.println("Success. Table status: " + table.getDescription().getTableStatus()); } catch (Exception e) { System.err.println("Unable to create table: "); System.err.println(e.getMessage()); } } / * 数据加载 */ @Test public void MoviesLoadData() throws IOException { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); //建立连接 DynamoDB dynamoDB = new DynamoDB(client); //获取Movies表 Table table = dynamoDB.getTable("Movies"); //获取json文件 JsonParser parser = new JsonFactory().createParser(new File("E:\\IdeaWorkSpace\\dynamodbdemo\\src\\main\\resources\\test.json")); JsonNode rootNode = new ObjectMapper().readTree(parser); Iterator 
  
    iter = rootNode 
   .iterator() 
   ; ObjectNode currentNode 
   ; //遍历json对象实现循环添加 while (iter 
   .hasNext()) { currentNode = (ObjectNode) iter 
   .next() 
   ; int year = currentNode 
   .path( 
   "year") 
   .asInt() 
   ; String title = currentNode 
   .path( 
   "title") 
   .asText() 
   ; try { table 
   .putItem(new Item() 
   .withPrimaryKey( 
   "year", year, 
   "title", title) 
   .withJSON( 
   "info", currentNode 
   .path( 
   "info") 
   .toString())) 
   ; System 
   .out 
   .println( 
   "PutItem succeeded: " + year + 
   " " + title) 
   ; } catch (Exception e) { System 
   .err 
   .println( 
   "Unable to add movie: " + year + 
   " " + title) 
   ; System 
   .err 
   .println(e 
   .getMessage()) 
   ; 
   break 
   ; } } System 
   .out 
   .println( 
   "test success") 
   ; parser 
   .close() 
   ; } 
   / * 添加操作 * 创建一个新的项目 * 您将新项目添加到Movies表中 */ @Test public void MoviesCreateItem() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
   .standard() 
   .build() 
   ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
   ; //获取Movies表 Table table = dynamoDB 
   .getTable( 
   "Movies") 
   ; //参数 int year = 
   2015 
   ; String title = 
   "The Big New Movie" 
   ; //info信息 final Map 
   
     infoMap = new HashMap 
    
      () 
     ; infoMap 
     .put( 
     "plot", 
     "Nothing happens at all.") 
     ; infoMap 
     .put( 
     "rating", 
     0) 
     ; //添加 try { System 
     .out 
     .println( 
     "Adding a new item...") 
     ; PutItemOutcome outcome = table 
     .putItem(new Item() 
     .withPrimaryKey( 
     "year", year, 
     "title", title) 
     .withMap( 
     "info", infoMap)) 
     ; System 
     .out 
     .println( 
     "PutItem succeeded:\n" + outcome 
     .getPutItemResult()) 
     ; } catch (Exception e) { System 
     .err 
     .println( 
     "Unable to add item: " + year + 
     " " + title) 
     ; System 
     .err 
     .println(e 
     .getMessage()) 
     ; } } 
     / * getItem方法从Movies表中读取该项 */ @Test public void MoviesGetItem() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
     .standard() 
     .build() 
     ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
     ; //获取Movies表 Table table = dynamoDB 
     .getTable( 
     "Movies") 
     ; int year = 
     2015 
     ; String title = 
     "The Big New Movie" 
     ; GetItemSpec spec = new GetItemSpec() 
     .withPrimaryKey( 
     "year", year, 
     "title", title) 
     ; try { System 
     .out 
     .println( 
     "Attempting to read the item...") 
     ; Item outcome = table 
     .getItem(spec) 
     ; System 
     .out 
     .println( 
     "GetItem succeeded: " + outcome) 
     ; } catch (Exception e) { System 
     .err 
     .println( 
     "Unable to read item: " + year + 
     " " + title) 
     ; System 
     .err 
     .println(e 
     .getMessage()) 
     ; } } 
     / * 您可以使用该updateItem方法修改现有项目。 * 可以更新现有属性的值,添加新属性或删除属性 */ @Test public void MoviesUpdateItem() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
     .standard() 
     .build() 
     ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
     ; //获取Movies表 Table table = dynamoDB 
     .getTable( 
     "Movies") 
     ; int year = 
     2015 
     ; String title = 
     "The Big New Movie" 
     ; UpdateItemSpec updateItemSpec = new UpdateItemSpec() 
     .withPrimaryKey( 
     "year", year, 
     "title", title) 
     .withUpdateExpression( 
     "set info.rating = :r, info.plot=:p, info.actors=:a") 
     .withValueMap(new ValueMap() 
     .withNumber( 
     ":r", 
     5.5) 
     .withString( 
     ":p", 
     "Everything happens all at once.") 
     .withList( 
     ":a", Arrays 
     .asList( 
     "Larry", 
     "Moe", 
     "Curly"))) 
     .withReturnValues(ReturnValue 
     .UPDATED_NEW) 
     ; try { System 
     .out 
     .println( 
     "Updating the item...") 
     ; UpdateItemOutcome outcome = table 
     .updateItem(updateItemSpec) 
     ; System 
     .out 
     .println( 
     "UpdateItem succeeded:\n" + outcome 
     .getItem() 
     .toJSONPretty()) 
     ; } catch (Exception e) { System 
     .err 
     .println( 
     "Unable to update item: " + year + 
     " " + title) 
     ; System 
     .err 
     .println(e 
     .getMessage()) 
     ; } } 
     / * 原子计数器: * 您可以使用该updateItem方法来 * 增加或减少现有属性的值, * 而不会影响其他写入请求 */ @Test public void MoviesContentItem() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
     .standard() 
     .build() 
     ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
     ; //获取Movies表 Table table = dynamoDB 
     .getTable( 
     "Movies") 
     ; int year = 
     2015 
     ; String title = 
     "The Big New Movie" 
     ; UpdateItemSpec updateItemSpec = new UpdateItemSpec() 
     .withPrimaryKey( 
     "year", year, 
     "title", title) 
     .withUpdateExpression( 
     "set info.rating = info.rating + :val")//每次+ 
     1 
     .withValueMap(new ValueMap() 
     .withNumber( 
     ":val", 
     1)) 
     .withReturnValues(ReturnValue 
     .UPDATED_NEW) 
     ; try { System 
     .out 
     .println( 
     "Incrementing an atomic counter...") 
     ; UpdateItemOutcome outcome = table 
     .updateItem(updateItemSpec) 
     ; System 
     .out 
     .println( 
     "UpdateItem succeeded:\n" + outcome 
     .getItem() 
     .toJSONPretty()) 
     ; } catch (Exception e) { System 
     .err 
     .println( 
     "Unable to update item: " + year + 
     " " + title) 
     ; System 
     .err 
     .println(e 
     .getMessage()) 
     ; } } 
     / * 以下程序显示如何使用UpdateItem条件。 * 如果条件求值为true,则更新成功; 否则,不执行更新。 */ @Test public void updateItem() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
     .standard() 
     .build() 
     ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
     ; //获取Movies表 Table table = dynamoDB 
     .getTable( 
     "Movies") 
     ; int year = 
     2015 
     ; String title = 
     "The Big New Movie" 
     ; UpdateItemSpec updateItemSpec = new UpdateItemSpec() 
     .withPrimaryKey(new PrimaryKey( 
     "year", year, 
     "title", title)) 
     .withUpdateExpression( 
     "remove info.actors[0]") 
     .withConditionExpression( 
     "size(info.actors) > :num") 
     .withValueMap(new ValueMap() 
     .withNumber( 
     ":num", 
     1)) 
     .withReturnValues(ReturnValue 
     .UPDATED_NEW) 
     ; // Conditional update (we expect this to fail) try { System 
     .out 
     .println( 
     "Attempting a conditional update...") 
     ; UpdateItemOutcome outcome = table 
     .updateItem(updateItemSpec) 
     ; System 
     .out 
     .println( 
     "UpdateItem succeeded:\n" + outcome 
     .getItem() 
     .toJSONPretty()) 
     ; } catch (Exception e) { System 
     .err 
     .println( 
     "Unable to update item: " + year + 
     " " + title) 
     ; System 
     .err 
     .println(e 
     .getMessage()) 
     ; } } 
     / * 您可以使用该deleteItem方法通过指定其主键来删除一个项目。 * 如果不满足条件,您可以选择提供一个ConditionExpression以防止项目删除。 */ @Test public void deleteItem() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
     .standard() 
     .build() 
     ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
     ; //获取Movies表 Table table = dynamoDB 
     .getTable( 
     "Movies") 
     ; int year = 
     2015 
     ; String title = 
     "The Big New Movie" 
     ; DeleteItemSpec deleteItemSpec = new DeleteItemSpec() 
     .withPrimaryKey(new PrimaryKey( 
     "year", year, 
     "title", title)) 
     .withConditionExpression( 
     "info.rating <= :val") 
     .withValueMap(new ValueMap() 
     .withNumber( 
     ":val", 
     8.0)) 
     ; // Conditional delete (we expect this to fail) try { System 
     .out 
     .println( 
     "Attempting a conditional delete...") 
     ; table 
     .deleteItem(deleteItemSpec) 
     ; System 
     .out 
     .println( 
     "DeleteItem succeeded") 
     ; } catch (Exception e) { System 
     .err 
     .println( 
     "Unable to delete item: " + year + 
     " " + title) 
     ; System 
     .err 
     .println(e 
     .getMessage()) 
     ; } } 
     / * 查询 */ @Test public void MoviesQuery() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
     .standard() 
     .build() 
     ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
     ; //获取Movies表 Table table = dynamoDB 
     .getTable( 
     "Movies") 
     ; HashMap 
     
       nameMap = new HashMap 
      
        () 
       ; nameMap 
       .put( 
       "#yr", 
       "year") 
       ; HashMap 
       
         valueMap = new HashMap 
        
          () 
         ; valueMap 
         .put( 
         ":yyyy", 
         2013) 
         ; QuerySpec querySpec = new QuerySpec() 
         .withKeyConditionExpression( 
         "#yr = :yyyy") 
         .withNameMap(nameMap) 
         .withValueMap(valueMap) 
         ; ItemCollection 
         
           items = null 
          ; Iterator 
          
            iterator = null 
           ; Item item = null 
           ; try { System 
           .out 
           .println( 
           "Movies from 2013") 
           ; items = table 
           .query(querySpec) 
           ; iterator = items 
           .iterator() 
           ; while (iterator 
           .hasNext()) { item = iterator 
           .next() 
           ; System 
           .out 
           .println(item 
           .getNumber( 
           "year") + 
           ": " + item 
           .getString( 
           "title")) 
           ; } } catch (Exception e) { System 
           .err 
           .println( 
           "Unable to query movies from 2013") 
           ; System 
           .err 
           .println(e 
           .getMessage()) 
           ; } // valueMap 
           .put( 
           ":yyyy", 
           1992) 
           ; // valueMap 
           .put( 
           ":letter1", 
           "A") 
           ; // valueMap 
           .put( 
           ":letter2", 
           "L") 
           ; // // querySpec 
           .withProjectionExpression( 
           "#yr, title, info.genres, info.actors[0]") // 
           .withKeyConditionExpression( 
           "#yr = :yyyy and title between :letter1 and :letter2") 
           .withNameMap(nameMap) // 
           .withValueMap(valueMap) 
           ; // // try { // System 
           .out 
           .println( 
           "Movies from 1992 - titles A-L, with genres and lead actor") 
           ; // items = table 
           .query(querySpec) 
           ; // // iterator = items 
           .iterator() 
           ; // while (iterator 
           .hasNext()) { // item = iterator 
           .next() 
           ; // System 
           .out 
           .println(item 
           .getNumber( 
           "year") + 
           ": " + item 
           .getString( 
           "title") + 
           " " + item 
           .getMap( 
           "info")) 
           ; // } // // } catch (Exception e) { // System 
           .err 
           .println( 
           "Unable to query movies from 1992:") 
           ; // System 
           .err 
           .println(e 
           .getMessage()) 
           ; // } } 
           / * 扫描 * ProjectionExpression 指定扫描结果中所需的属性。 * FilterExpression 指定只返回满足条件的项的条件。所有其他项目都被丢弃。 */ @Test public void MoviesScan() { //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
           .standard() 
           .build() 
           ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
           ; //获取Movies表 Table table = dynamoDB 
           .getTable( 
           "Movies") 
           ; ScanSpec scanSpec = new ScanSpec() 
           .withProjectionExpression( 
           "#yr, title, info.rating") 
           .withFilterExpression( 
           "#yr between :start_yr and :end_yr") 
           .withNameMap(new NameMap() 
           .with( 
           "#yr", 
           "year")) 
           .withValueMap(new ValueMap() 
           .withNumber( 
           ":start_yr", 
           2011) 
           .withNumber( 
           ":end_yr", 
           2015)) 
           ; try { ItemCollection 
           
             items = table 
            .scan(scanSpec) 
            ; Iterator 
            
              iter = items 
             .iterator() 
             ; while (iter 
             .hasNext()) { Item item = iter 
             .next() 
             ; System 
             .out 
             .println(item 
             .toString()) 
             ; } } catch (Exception e) { System 
             .err 
             .println( 
             "Unable to scan the table:") 
             ; System 
             .err 
             .println(e 
             .getMessage()) 
             ; } } 
             / * 删表 跑路 */ public void deleteTeble(){ //加载驱动 AmazonDynamoDB client = AmazonDynamoDBClientBuilder 
             .standard() 
             .build() 
             ; //建立连接 DynamoDB dynamoDB = new DynamoDB(client) 
             ; //获取Movies表 Table table = dynamoDB 
             .getTable( 
             "Movies") 
             ; try { System 
             .out 
             .println( 
             "Attempting to delete table; please wait...") 
             ; table 
             .delete() 
             ; table 
             .waitForDelete() 
             ; System 
             .out 
             .print( 
             "Success.") 
             ; } catch (Exception e) { System 
             .err 
             .println( 
             "Unable to delete table: ") 
             ; System 
             .err 
             .println(e 
             .getMessage()) 
             ; } } } 
             
            
           
          
         
        
       
      
     
    
  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213052.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午6:39
下一篇 2026年3月18日 下午6:40


相关推荐

  • 学习 Web 开发技术的16个最佳教程网站和博客

    学习 Web 开发技术的16个最佳教程网站和博客互联网经过这么多年的发展,已经出现了众多的Web开发技术,像.Net/Java/PHP/Python/Ruby等等。对于Web开发人员来说,不管是初学者还是有一定经验的开发人员都需要时刻学

    2022年8月6日
    5
  • vb程序设计教程第4版龚沛曾 实验答案解析

    vb程序设计教程第4版龚沛曾 实验答案解析这里只是个人对书中题目的解答,并不代表最优代码。仅供参考。有哪里错误或者不足的地方还望指出,Thanks♪(・ω・)ノ以及不要脸地求探讨求点赞。嘿嘿这里使用的是《vb程序设计教程(第四版)——龚沛曾》:实验3(主要考察分支选择结构。1—7考察select和if语句,8用到choose函数,9—11以控件option和check为主)3.1:OptionExpl…

    2022年10月7日
    5
  • GoogleMaps_键盘网站

    GoogleMaps_键盘网站在Google地球中使用键盘/鼠标导航首先要明白导航过程中的三个中心,视野中心,相机视角,鼠标锁定位置。还要明白3D视图和俯视图、地平面视图的区别,因为在海拔为0时将进入地平面视图,上下的操作将变为拉近和推远。中间的位置为视野中心,可以通过Ctrl+Shif+左箭头/右箭头来触发显示,如果要展示的对象不在视野中心,可以通过Alt+左箭头/右箭头进行对象位置微调。-/+的中心为视野中心。相机视角可以通过Ctrl触发,为可以通过左箭头/右箭头控制水平方向旋转,上箭头/下箭头控制上下方向旋

    2025年11月19日
    7
  • RRT基本概念

    RRT基本概念原文地址快速探索随机树 RRT 是一种通过随机构建空间填充树来有效搜索非凸 高维空间的算法 树是从搜索空间中随机抽取的样本逐步构建的 并且本质上倾向于朝向大部分未探测区域生长 RRT 由 StevenM LaValle 和 JamesJ KuffnerJr 1 2 开发 它们可以轻松处理障碍物和差分约束 非完整和动力学 的问题 并被广泛应用于自主机器人运动规划 RRT 可以被看作是一种为

    2026年3月17日
    1
  • 自己动手写操作系统在线阅读_如何理解写作是一个整体系统

    自己动手写操作系统在线阅读_如何理解写作是一个整体系统最近开始看《自己动手写操作系统》,虽然很早以前就读过一点点,但一直没有机会动手实践。本着光说不练假把式的原则,今天动手实践了开头的一部分。(说得这么正经其实你就是看了一点点吧!囧)废话不多说,在这里做一个小小的总结。实验环境:操作系统:win7旗舰版64位 汇编编译器:NASM 2.12.02虚拟机:VirtualPC_2007软盘/虚拟软盘写引导扇区工具:Flopp

    2022年8月30日
    5
  • pycharm导包错误

    pycharm导包错误如果自己包的名称和 python 自有或者第三方包的名称相同 那首先就会从自己的包里面去找 就 import 错误

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号