Java DynamoDB 增加、删除、修改、查询
程序员文章站
2022-04-08 23:34:24
准备jar包 准备对象: 数据库表映射对象: 初始化对象: 根据id查询一条: 根据指定索引查询多条: 根据指定条件扫描多条: 根据id删除: 添加、修改: ......
准备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对MySQL数据库进行连接、查询和修改操作方法
-
Python列表常见操作详解(获取,增加,删除,修改,排序等)
-
java对XML文件的解析、节点的增加、删除操作总结
-
PHP轻量级数据库操作类Medoo增加、删除、修改、查询例子
-
php操作MongoDB基础教程(连接、新增、修改、删除、查询)
-
WPF DataGrid显示MySQL查询信息,且可删除、修改、插入 (原发布 csdn 2018-10-13 20:07:28)
-
关于python的列表操作(一):取值,增加,修改,删除
-
Python列表常见操作详解(获取,增加,删除,修改,排序等)
-
php 数组操作(增加,删除,查询,排序)等函数说明第1/2页
-
cmd 命令行模式操作数据库 添加查询 修改 删除 ( 表 字段 数据)