准备jar包

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.534</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.46</version>
</dependency>

准备对象:

//用户凭证
private static String AWSAccessKeyId = "xxx";
private static String AWSSecretKey = "xxx";
//表名
private static String TABLE_NAME = "xxx";
//用户凭证对象
private static AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
  public void refresh() {}
  public AWSCredentials getCredentials() {return new BasicAWSCredentials(AWSAccessKeyId, AWSSecretKey);}
};
//表的相关对象
private static AmazonDynamoDB amazonDynamoDBClient = null;
private static DynamoDBMapper dbMapper = null;
private static Table table = null;

数据库表映射对象:

@DynamoDBTable(tableName = "xxx")
public class User {
private String id = null;
private String name = null;
private String telephone = null;
public User(String id, String name, String telephone) {
super();
this.id = id;
this.name = name;
this.telephone = telephone;
}
  //主键
@DynamoDBHashKey(attributeName = "Id")
public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public User() {
}
  //配有索引 userName-index
@DynamoDBAttribute(attributeName = "userName")
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
  //配有索引 telephone-index
@DynamoDBAttribute(attributeName = "telephone")
public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
}
}

初始化对象:

amazonDynamoDBClient =  AmazonDynamoDBClientBuilder.standard().withCredentials(awsCredentialsProvider).withRegion(Regions.AP_NORTHEAST_1).build();
dbMapper = new DynamoDBMapper(amazonDynamoDBClient);
table = new DynamoDB(amazonDynamoDBClient).getTable(TABLE_NAME);

根据id查询一条:

public static user getItemById(String id) {
return dbMapper.load(User.class, id);
}

根据指定索引查询多条:

public static List<User> getItemBykey(String key, String value) {
    //取索引
Index index = table.getIndex(key + "-index");
HashMap<String, String> nameMap = new HashMap<String, String>();
nameMap.put("#key", key);
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":value", value);
    //创建筛选条件,以map的形式传入key和value,条件只能用 = 号,其他未考证
QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#key = :value").withNameMap(nameMap)
.withValueMap(valueMap);
ItemCollection<QueryOutcome> items = index.query(querySpec);
Iterator<Item> iterator = items.iterator();
Item item = null;
List<User> Users = new ArrayList<User>();
while (iterator.hasNext()) {
item = iterator.next();
dashButtonUsers.add(new DashButtonUser(item.getString("Id"),item.getString("userName"),item.getString("telephone"));
}
return Users;
}

根据指定条件扫描多条:

public static List<User> getItemByTimeRange(Long startTime, Long endTime) {
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
expressionAttributeValues.put(":startTime", new AttributeValue().withN("" + startTime));
expressionAttributeValues.put(":endTime", new AttributeValue().withN("" + endTime));
    //筛选条件
ScanRequest scanRequest = new ScanRequest().withTableName(TABLE_NAME)
.withFilterExpression("startTime >= :startTime and endTime <= :endTime")
.withExpressionAttributeValues(expressionAttributeValues);
ScanResult result = amazonDynamoDBClient.scan(scanRequest);
List<User> users = new ArrayList<User>();
for (Map<String, AttributeValue> item : result.getItems()) {
dashButtonUsers.add(new DashButtonUser(/* 略 */));
}
return users;
}

根据id删除:

//删除一条
public static void deleteItemById(String id) {
dbMapper.delete(new DashButtonUser(id, null, null, null, null, null, null));
}
//删除多条
public static void deleteBatch(List<User> ids) {
  //ids[0] -->{"id":"xxx","telephone":null,"name":null}
dbMapper.batchDelete(ids);
}

添加、修改:

//添加、修改一条
public static void addOrupdateOneItem(User user) {
dbMapper.save(user);
}
//添加、修改多条
public static List<FailedBatch> addOrUpdateBatch(List<User> users) {
return dbMapper.batchSave(users);
}

Java DynamoDB 增加、删除、修改、查询的更多相关文章

  1. [JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改

    上一次的文章,我们讲解了如何用JAVA访问MySql数据库,对数据进行增加删除修改查询.那么这次我们把具体的页面的数据库操作结合在一起,进行一次简单的学生信息操作案例. 首先我们创建一个专门用于学生管 ...

  2. Nodejs之MEAN栈开发(九)---- 用户评论的增加/删除/修改

    由于工作中做实时通信的项目,需要用到Nodejs做通讯转接功能,刚开始接触,很多都不懂,于是我和同事就准备去学习nodejs,结合nodejs之MEAN栈实战书籍<Getting.MEAN.wi ...

  3. 在Javascript操作JSON对象,增加 删除 修改

    在Javascript操作JSON对象,增加删除修改全有的,详情见代码 <script type="text/javascript"> var jsonObj2 = { ...

  4. AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层

    AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层 AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层我理解的图层的作用大概是把 ...

  5. Spring Boot 增加删除修改 批量

    1.批量删除  a.自定义Repositoy中写 前台处理https://blog.csdn.net/yhflyl/article/details/81557670首先前台先要获取所有的要删除数据的I ...

  6. php后台增加删除修改跳转页面

    第一步 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  7. ztree树形菜单的增加删除修改和换图标

    首先需要注意一点,如果有研究过树形菜单,就会发现实现删除和修改功能特别简单,但是增加却有一点复杂.造成这个现象是ztree树形菜单的历史遗留问题.大概是之前的版本没有增加这个功能,后来的版本加上了这个 ...

  8. Sql增加,删除,修改列

    1. 查看约束条件 - MySQL: SELECT * FROM information_schema.`TABLE_CONSTRAINTS` where table_name = 'book'; - ...

  9. jQuery增加删除修改tab导航特效

    HTML:         <div class="container iden_top">                <ul>             ...

随机推荐

  1. [BZOJ1088][SCOI2005]扫雷Mine DP

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1088 记录下每一个格子对应左边格子放的雷的情况,然后dp转移就好了. #include&l ...

  2. Delphi win10 asssertion failure

    Delphi2007 原来安装在Win7 下 运行正常, 自从升级到Win10 ,新建工程运行然后关闭报错, 报错信息如下: ---------------------------bds.exe - ...

  3. OJB

    OJB 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 对象关系桥(OJB)是一种对象关系映射工具,它能够完成从Java对象到关系数据库的透明存储.   英文名 OJB ...

  4. SourceTree 常用操作

    1.Sourcetree 每次拉取提交都需要输入密码(是有多个项目,他们的账户不一样) 输入以下命令: git config --global credential.helper osxkeychai ...

  5. bind - 将一个名字和一个套接字绑定到一起

    SYNOPSIS 概述 #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct s ...

  6. 边框带阴影 box-shadow

    .chosen-container-active .chosen-single { border: 1px solid #5897fb; -webkit-box-shadow: 0 0 5px rgb ...

  7. 一篇文章告你python能做什么,该不该学?好不好学?适不适合学?

    一.python好学吗?简单吗?容易学吗?没有编程的领取能学吗? 最近有很多小伙伴都在问我这些问题.在这里,我想说,python非常简单易学. 1,简单, Python 非常易于读写,开发者可以把更多 ...

  8. GCC编译链接过程

    编译链接过程 代码 #cat main.c #include <stdio.h> int add(int x, int y); int sub(int x, int y); int mul ...

  9. Python3中assert断言

    一般的用法是: assert condition 用来让程序测试这个condition,如果condition为false,那么raise一个AssertionError.逻辑上等于: if not ...

  10. linux系统查看网络连接情况

    netstat命令状态说明: CLOSED                      没有使用这个套接字[netstat 无法显示closed状态] LISTEN 套接字正在监听连接[调用listen ...