Java DynamoDB 增加、删除、修改、查询
2019-05-17 00:03:08来源:博客园 阅读 ()
准备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); }
原文链接:https://www.cnblogs.com/ddopp/p/10876768.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash