Java DynamoDB 增加、删除、修改、查询
准备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 增加、删除、修改、查询的更多相关文章
- [JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改
上一次的文章,我们讲解了如何用JAVA访问MySql数据库,对数据进行增加删除修改查询.那么这次我们把具体的页面的数据库操作结合在一起,进行一次简单的学生信息操作案例. 首先我们创建一个专门用于学生管 ...
- Nodejs之MEAN栈开发(九)---- 用户评论的增加/删除/修改
由于工作中做实时通信的项目,需要用到Nodejs做通讯转接功能,刚开始接触,很多都不懂,于是我和同事就准备去学习nodejs,结合nodejs之MEAN栈实战书籍<Getting.MEAN.wi ...
- 在Javascript操作JSON对象,增加 删除 修改
在Javascript操作JSON对象,增加删除修改全有的,详情见代码 <script type="text/javascript"> var jsonObj2 = { ...
- AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层
AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层 AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层我理解的图层的作用大概是把 ...
- Spring Boot 增加删除修改 批量
1.批量删除 a.自定义Repositoy中写 前台处理https://blog.csdn.net/yhflyl/article/details/81557670首先前台先要获取所有的要删除数据的I ...
- php后台增加删除修改跳转页面
第一步 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- ztree树形菜单的增加删除修改和换图标
首先需要注意一点,如果有研究过树形菜单,就会发现实现删除和修改功能特别简单,但是增加却有一点复杂.造成这个现象是ztree树形菜单的历史遗留问题.大概是之前的版本没有增加这个功能,后来的版本加上了这个 ...
- Sql增加,删除,修改列
1. 查看约束条件 - MySQL: SELECT * FROM information_schema.`TABLE_CONSTRAINTS` where table_name = 'book'; - ...
- jQuery增加删除修改tab导航特效
HTML: <div class="container iden_top"> <ul> ...
随机推荐
- 10.3 Implementing pointers and objects and 10.4 Representing rooted trees
Algorithms 10.3 Implementing pointers and objects and 10.4 Representing rooted trees Allocating an ...
- JSP标签 <meta.....>作用总结
<metahttp-equiv="pragma" content="no-cache"> <metahttp-equiv="cach ...
- C#运用存储过程新增一条记录并返回自动生成的ID
@Hcy黄灿奕:http://blog.sina.com.cn/iihcy 前言: 1.存储过的好处: 存储过程相对于其他的数据库访问方法有以下的优点: (1)重复使用.存储过程可以重复使用,从而可以 ...
- Android(java)学习笔记202:JNI之hello.c(c代码功能实现)指针语法解析
1. 接下来我们细讲分析一下前面一讲中,c功能实现的代码: (1)hello.c : #include <jni.h> char* getHello() { //////// return ...
- at, batch, atq, atrm - 排队、检查或删除以后要执行的作业
总览 at [-V] [-q 队列] [-f 文件] [-mldbv] 时间 at -c 作业 [作业...] atq [-V] [-q 队列] [-v] atrm [-V] 作业 [作业...] b ...
- caffe blob理解
blob数据结构是caffe中基本的数据存储单元,它主要存储的数据是网络中的中间数据变量,比如各层的输入和输出:代价函数关于网络各层参数的梯度. blob中除了存储数据外,还有一些标记数据的参数,以下 ...
- [SQL]数据分析SQL笔试题
SQL笔试题 1.请简单写出left join和join的用法区别(可举例说明): 2.求出订单表(order表)中每个客户(custid)的最近一次购买日期(要求:按custid降序排列,trans ...
- hdfs深入:02、今日课程内容大纲以及hdfs的基本实现
1.hadoop第三天课程内容 hdfs:分布式文件存储系统hdfs的架构图hdfs的副本机制以及block块hdfs的元数据信息fsimage与editshdfs的文件读写过程hdfs的javaAP ...
- jquery 应用
http://www.jq22.com/ gwj6396668@163.com
- java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表 数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...