欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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);
    }