博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java DynamoDB 增加、删除、修改、查询
阅读量:6279 次
发布时间:2019-06-22

本文共 4532 字,大约阅读时间需要 15 分钟。

准备jar包

com.amazonaws
aws-java-sdk-core
1.11.534
com.amazonaws
aws-java-sdk-dynamodb
1.11.46

准备对象:

//用户凭证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
getItemBykey(String key, String value) {
    //取索引 Index index = table.getIndex(key + "-index"); HashMap
nameMap = new HashMap
(); nameMap.put("#key", key); HashMap
valueMap = new HashMap
(); valueMap.put(":value", value);     //创建筛选条件,以map的形式传入key和value,条件只能用 = 号,其他未考证 QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#key = :value").withNameMap(nameMap) .withValueMap(valueMap); ItemCollection
items = index.query(querySpec); Iterator
iterator = items.iterator(); Item item = null; List
Users = new ArrayList
(); while (iterator.hasNext()) { item = iterator.next(); dashButtonUsers.add(new DashButtonUser(item.getString("Id"),item.getString("userName"),item.getString("telephone")); } return Users; }

根据指定条件扫描多条:

public static List
getItemByTimeRange(Long startTime, Long endTime) { Map
expressionAttributeValues = new HashMap
(); 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
users = new ArrayList
(); for (Map
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
ids) {
  //ids[0] -->{"id":"xxx","telephone":null,"name":null} dbMapper.batchDelete(ids); }

添加、修改:

//添加、修改一条public static void addOrupdateOneItem(User user) {        dbMapper.save(user);    }//添加、修改多条public static List
addOrUpdateBatch(List
users) { return dbMapper.batchSave(users); }

转载于:https://www.cnblogs.com/ddopp/p/10876768.html

你可能感兴趣的文章
翻译 | 摆脱浏览器限制的JavaScript
查看>>
闲扯下午引爆乌云社区“盗窃”乌云币事件
查看>>
02@在类的头文件中尽量少引入其他头文件
查看>>
JAVA IO BIO NIO AIO
查看>>
input checkbox 复选框大小修改
查看>>
网吧维护工具
查看>>
BOOT.INI文件参数
查看>>
vmstat详解
查看>>
新年第一镖
查看>>
unbtu使用笔记
查看>>
OEA 中 WPF 树型表格虚拟化设计方案
查看>>
Android程序开发初级教程(一) 开始 Hello Android
查看>>
使用Gradle打RPM包
查看>>
“我意识到”的意义
查看>>
淘宝天猫上新辅助工具-新品填表
查看>>
再学 GDI+[43]: 文本输出 - 获取已安装的字体列表
查看>>
nginx反向代理
查看>>
操作系统真实的虚拟内存是什么样的(一)
查看>>
hadoop、hbase、zookeeper集群搭建
查看>>
python中一切皆对象------类的基础(五)
查看>>