准备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. [BZOJ1257][CQOI2007]余数之和sum 数学+分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1257 题目所求为$$Ans=\sum_{i=1}^nk%i$$ 将其简单变形一下$$Ans ...

  2. H.264和HEVC分析软件和工具【转】

    一.264分析两大利器:264VISA和Elecard StreamEye Tools 264visa 强力的h264实时分析工具 ,能分析各种场合下的h264资源,适用于h264开发者,学习者.在图 ...

  3. android 设置跳转

    android.provider.Settings. 1.   ACTION_ACCESSIBILITY_SETTINGS :    // 跳转系统的辅助功能界面            Intent ...

  4. Cygwin, MinGW/MSYS, MinGW-W64/MSYS2

    1. Cygwin http://www.cygwin.com/ Cygwin is a large collection of GNU and Open Source tools which pro ...

  5. mysql-scott用户的表的创建

    /* 功能:创建 scott 数据库中的 dept 表 */ create table dept( deptno int unsigned auto_increment primary key COM ...

  6. zuul 网关

    1.网关的作用 网关可以拦截客户端所有请求,对该请求进行权限控制,负载均衡.日志管理.接口调用监控等操作. 1)网关对所有服务会话进行拦截 2)网关安全控制,统一异常处理,XXS.SQL注入 3)权限 ...

  7. JavaSE-20 IO序列化

    学习要点 定义 IO如何序列化 序列化 序列化:是将对象的状态存储到特定存储介质中的过程. 反序列化:从特定存储介质中的数据重新构建对象的过程. 实现了java.io.Serializable接口的类 ...

  8. hdfs深入:05、hdfs中的fsimage和edits的合并过程

    6.4.secondarynameNode如何辅助管理FSImage与Edits文件 ①:secnonaryNN通知NameNode切换editlog ②:secondaryNN从NameNode中获 ...

  9. CGContextAddArc

    这个函数让我在纸上画了半天才搞明白,把我的理解给大家分享下. void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat ra ...

  10. run loop

    Objective-C之run loop详解 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9237973 转载请注明出 ...