准备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. vue全局loading组件

    本组件作用在页面加载完成前进行loader提示,提升用户体验,只需要在app.vue中引用一次,整个项目中路由切换时就可以自动进行提示(vuex版): 1. 添加vuex值和方法: import Vu ...

  2. Android 使用pl.droidsonroids.gif.GifImageView在安卓中显示动图遇到的问题

    在做一款聊天软件,其中聊天界面需要发送表情,而表情都是动图,在安卓中想要显示动图,就要借助第三方框架,我选的是pl.droidsonroids.gif.GifImageView. 使用方法如下:你在g ...

  3. scala.的Enumeration枚举示例(转)

    简介 在scala中没有枚举类型,但在标准类库中提供了Enumeration类来产出枚举.扩展Enumeration类后,调用value方法类初始化枚举中的可能值. 内部类value实际上是一个抽象类 ...

  4. iOS Programming Dynamic Type 1

    iOS Programming Dynamic Type 1  Dynamic Type is a technology introduced in iOS 7 that helps realize ...

  5. Sass的的使用二

    1.嵌套输出方式 nested Sass 提供了一种嵌套显示 CSS 文件的方式.例如 nav { ul { margin: 0; padding: 0; list-style: none; } li ...

  6. 合并百度影音的离线数据 with python 第二版 基于yield

    重新整理了一下代码. 增加了bdv,mkv的处理流程. 目前暂时支持windows平台. bdv,mkv,rmvb的不同处理流程 # -*- coding: UTF-8 -*- import os i ...

  7. Ubuntu 下更新或下载输入法(搜狗)

    ubuntu12.04的fcitx版本不支持,不满足依赖,需要更新fcitx 添加fcitx源添加fcitx源命令 : sudo add-apt-repository ppa:fcitx-team/n ...

  8. 【原创】webbluetoorh 在windows下无法显示搜索列表,在mac下正常的解决办法

    google webbluetooth在windows下不能弹出设备搜索列表提示“Web Bluetooth API is not available”,因为webbluetooth是google新推 ...

  9. automount - 配置autofs的挂载点

    命令概要(SYNOPSIS) automount [options] mount-point map-type[,format] map [map-options] 描述(DESCRIPTION) a ...

  10. SVN文件库移植(转)

     SVN文件库移植(转) 分类: 项目管理2013-04-19 11:06 161人阅读 评论(0) 收藏 举报 公司以前用的SVN是安装在windows2003下,用了一年多,现在大家觉得很慢,强烈 ...