DynamoDBMapper查询扫描表数据
来源:互联网 发布:mysql 事务 编辑:程序博客网 时间:2024/06/10 23:27
package com.askey.hyman.aws.verchk.helper;import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;import com.amazonaws.services.dynamodbv2.datamodeling.*;import com.amazonaws.services.dynamodbv2.model.AttributeValue;import java.text.SimpleDateFormat;import java.util.*;/** * DynamoDB的测试类: * 测试查询和扫描 * Created by hyman on 2017/5/25. */public class DynamoDBMapperQueryScanExample { //获取客户端 private static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); public static void main(String[] args) { //获取mapper映射 DynamoDBMapper mapper = new DynamoDBMapper(client);// GetBook(mapper,101); String forumName = "Amazon DynamoDB"; String threadSubject = "DynamoDB Thread 1";// FindRepliesInLast15Days(mapper,forumName,threadSubject);// FindRepliesPostedWithinTimePeriod(mapper, forumName, threadSubject);// FindBooksPricedLessThanSpecifiedValue(mapper, "20"); int numberOfThreads = 16; FindBicyclesOfSpecificTypeWithMultipleThreads(mapper, numberOfThreads, "Road"); } /** * * @param mapper * @param numberOfThreads * @param bicycleType */ private static void FindBicyclesOfSpecificTypeWithMultipleThreads(DynamoDBMapper mapper, int numberOfThreads, String bicycleType) { System.out.println("FindBicyclesOfSpecificTypeWithMultipleThreads: Scan ProductCatalog With Multiple Threads."); Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>(); eav.put(":val1", new AttributeValue().withS("Bicycle")); eav.put(":val2", new AttributeValue().withS(bicycleType)); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("ProductCategory = :val1 and BicycleType = :val2").withExpressionAttributeValues(eav); List<Bicycle> scanResult = mapper.parallelScan(Bicycle.class, scanExpression, numberOfThreads); for (Bicycle bicycle : scanResult) { System.out.println(bicycle); } } /** *条件扫描: * ("Price < :val1 and ProductCategory = :val2")//条件扫描表达式 * @param mapper * @param value */ private static void FindBooksPricedLessThanSpecifiedValue(DynamoDBMapper mapper, String value) { System.out.println("FindBooksPricedLessThanSpecifiedValue: Scan ProductCatalog."); Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>(); eav.put(":val1", new AttributeValue().withN(value)); eav.put(":val2", new AttributeValue().withS("Book")); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("Price < :val1 and ProductCategory = :val2")//条件表达式 .withExpressionAttributeValues(eav); List<Book> scanResult = mapper.scan(Book.class, scanExpression); for (Book book : scanResult) { System.out.println(book); } } /** *条件查询在两个时间段之间的数据 * @param mapper * @param forumName * @param threadSubject */ private static void FindRepliesPostedWithinTimePeriod(DynamoDBMapper mapper, String forumName, String threadSubject) { String partitionKey = forumName + "#" + threadSubject; System.out.println( "FindRepliesPostedWithinTimePeriod: Find replies for thread Message = 'DynamoDB Thread 2' posted within a period."); long startDateMilli = (new Date()).getTime() - (14L * 24L * 60L * 60L * 1000L); // Two // weeks // ago. long endDateMilli = (new Date()).getTime() - (7L * 24L * 60L * 60L * 1000L); // One // week // ago. SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); String startDate = dateFormatter.format(startDateMilli); String endDate = dateFormatter.format(endDateMilli); Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>(); eav.put(":val1", new AttributeValue().withS(partitionKey)); eav.put(":val2", new AttributeValue().withS(startDate)); eav.put(":val3", new AttributeValue().withS(endDate)); DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>() .withKeyConditionExpression("Id = :val1 and ReplyDateTime between :val2 and :val3")//查询表达式 .withExpressionAttributeValues(eav); List<Reply> betweenReplies = mapper.query(Reply.class, queryExpression); for (Reply reply : betweenReplies) { System.out.format("Id=%s, Message=%s, PostedBy=%s %n, PostedDateTime=%s %n", reply.getId(), reply.getMessage(), reply.getPostedBy(), reply.getReplyDateTime()); } } /** *条件查询:15天以内的信息 * @param mapper 映射器 * @param forumName * @param threadSubject */ private static void FindRepliesInLast15Days(DynamoDBMapper mapper, String forumName, String threadSubject) { System.out.println("FindRepliesInLast15Days: Replies within last 15 days."); String partitionKey = forumName + "#" + threadSubject; long twoWeeksAgoMili = (new Date()).getTime() - (15L*24L*60L*60L*1000L); Date twoWeeksAgo = new Date(); twoWeeksAgo.setTime(twoWeeksAgoMili); SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd 'T' HH:mm:ss.SSS'Z'"); dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); String twoWeeksAgoStr = dateFormatter.format(twoWeeksAgo); Map<String,AttributeValue> eav = new HashMap<>(); eav.put(":val1",new AttributeValue().withS(partitionKey)); eav.put(":val2",new AttributeValue().withS(twoWeeksAgoStr.toString())); DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>() .withKeyConditionExpression("Id = :val1 and ReplyDateTime > :val2")//查询表达式 .withExpressionAttributeValues(eav); List<Reply> latestReplies = mapper.query(Reply.class, queryExpression); for (Reply reply : latestReplies) { System.out.format("Id=%s, Message=%s, PostedBy=%s %n, ReplyDateTime=%s %n", reply.getId(), reply.getMessage(), reply.getPostedBy(), reply.getReplyDateTime()); } } /** * 根据id=101 查找Book信息 * @param mapper * @param i */ private static void GetBook(DynamoDBMapper mapper, int i) { System.out.println("GetBook: Get book Id = '101' "); System.out.println("Book table has no sort Key. you can do GetItem,but no Query"); Book book = mapper.load(Book.class,i); System.out.format("Id = %s Title = %s, ISBN = %s %n", book.getId(), book.getTitle(), book.getISBN()); } /** * book 实体类 */ @DynamoDBTable(tableName = "Book") public static class Book { private int id;//编号 private String title;//名称 private String ISBN; private int price; private int pageCount; private String productCategory; private boolean inPublication; @DynamoDBHashKey(attributeName = "Id") public int getId() { return id; } public void setId(int id) { this.id = id; } @DynamoDBAttribute(attributeName = "Title") public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @DynamoDBAttribute(attributeName = "ISBN") public String getISBN() { return ISBN; } public void setISBN(String ISBN) { this.ISBN = ISBN; } @DynamoDBAttribute(attributeName = "Price") public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @DynamoDBAttribute(attributeName = "PageCount") public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } @DynamoDBAttribute(attributeName = "ProductCategory") public String getProductCategory() { return productCategory; } public void setProductCategory(String productCategory) { this.productCategory = productCategory; } @DynamoDBAttribute(attributeName = "InPublication") public boolean isInPublication() { return inPublication; } public void setInPublication(boolean inPublication) { this.inPublication = inPublication; } @Override public String toString() { return "Boook{" + "id=" + id + ", title='" + title + '\'' + ", ISBN='" + ISBN + '\'' + ", price=" + price + ", pageCount=" + pageCount + ", productCategory='" + productCategory + '\'' + ", inPublication=" + inPublication + '}'; } public Book() { } } /** * Bicycle实体类 */ @DynamoDBTable(tableName = "ProductCatalog") public static class Bicycle { private int id; private String title; private String description; private String brand; private int price; private List<String> color; private String productCategory; private String bicycleType; public String getBicycleType() { return bicycleType; } public void setBicycleType(String bicycleType) { this.bicycleType = bicycleType; } @DynamoDBHashKey(attributeName = "Id") public int getId() { return id; } public void setId(int id) { this.id = id; } @DynamoDBAttribute(attributeName = "Title") public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @DynamoDBAttribute(attributeName = "Description") public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @DynamoDBAttribute(attributeName = "Brand") public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } @DynamoDBAttribute(attributeName = "Price") public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @DynamoDBAttribute(attributeName = "Color") public List<String> getColor() { return color; } public void setColor(List<String> color) { this.color = color; } @DynamoDBAttribute(attributeName = "ProductCategory") public String getProductCategory() { return productCategory; } public void setProductCategory(String productCategory) { this.productCategory = productCategory; } @Override public String toString() { return "Bicycle{" + "id=" + id + ", title='" + title + '\'' + ", description='" + description + '\'' + ", brand='" + brand + '\'' + ", price=" + price + ", color=" + color + ", productCategory='" + productCategory + '\'' + ", bicycleType='" + bicycleType + '\'' + '}'; } } /** * reply实体 */ @DynamoDBTable(tableName = "Reply") public static class Reply { private String id; private String replyDateTime; private String message; private String postedBy; @DynamoDBHashKey(attributeName = "Id") public String getId() { return id; } public void setId(String id) { this.id = id; } @DynamoDBRangeKey(attributeName = "ReplyDateTime") public String getReplyDateTime() { return replyDateTime; } public void setReplyDateTime(String replyDateTime) { this.replyDateTime = replyDateTime; } @DynamoDBAttribute(attributeName = "Message") public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @DynamoDBAttribute(attributeName = "PostedBy") public String getPostedBy() { return postedBy; } public void setPostedBy(String postedBy) { this.postedBy = postedBy; } @Override public String toString() { return "Reply{" + "id='" + id + '\'' + ", replyDateTime='" + replyDateTime + '\'' + ", message='" + message + '\'' + ", postedBy='" + postedBy + '\'' + '}'; } } /** * Thread 实体类 */ @DynamoDBTable(tableName = "Thread") public static class Thread { private String forumName; private String subject; private String message; private String lastPostedDateTime; private String lastPostedBy; private Set<String> tags; private int answered; private int views; private int replies; @DynamoDBHashKey(attributeName = "ForumName") public String getForumName() { return forumName; } public void setForumName(String forumName) { this.forumName = forumName; } @DynamoDBRangeKey(attributeName = "Subject") public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } @DynamoDBAttribute(attributeName = "Message") public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @DynamoDBAttribute(attributeName = "LastPostDateTime") public String getLastPostedDateTime() { return lastPostedDateTime; } public void setLastPostedDateTime(String lastPostedDateTime) { this.lastPostedDateTime = lastPostedDateTime; } @DynamoDBAttribute(attributeName = "LastPostedBy") public String getLastPostedBy() { return lastPostedBy; } public void setLastPostedBy(String lastPostedBy) { this.lastPostedBy = lastPostedBy; } @DynamoDBAttribute(attributeName = "Tags") public Set<String> getTags() { return tags; } public void setTags(Set<String> tags) { this.tags = tags; } @DynamoDBAttribute(attributeName = "Answered") public int getAnswered() { return answered; } public void setAnswered(int answered) { this.answered = answered; } @DynamoDBAttribute(attributeName = "Views") public int getViews() { return views; } public void setViews(int views) { this.views = views; } @DynamoDBAttribute(attributeName = "Replies") public int getReplies() { return replies; } public void setReplies(int replies) { this.replies = replies; } @Override public String toString() { return "Thread{" + "forumName='" + forumName + '\'' + ", subject='" + subject + '\'' + ", message='" + message + '\'' + ", lastPostedDateTime='" + lastPostedDateTime + '\'' + ", lastPostedBy='" + lastPostedBy + '\'' + ", tags=" + tags + ", answered=" + answered + ", views=" + views + ", replies=" + replies + '}'; } } /** * Forum 实体类 */ @DynamoDBTable(tableName = "Forum") public static class Forum{ private String name; private String category; private int threads; @DynamoDBHashKey(attributeName = "Name") public String getName() { return name; } public void setName(String name) { this.name = name; } @DynamoDBAttribute(attributeName = "Category") public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } @DynamoDBAttribute(attributeName = "Threads") public int getThreads() { return threads; } public void setThreads(int threads) { this.threads = threads; } @Override public String toString() { return "Forum{" + "name='" + name + '\'' + ", category='" + category + '\'' + ", threads=" + threads + '}'; } }}
阅读全文
0 0
- DynamoDBMapper查询扫描表数据
- Hbase操作:添加数据,查询数据,扫描数据,删除行,删除表
- HBase扫描表数据
- 数据压缩前后,对表进行查询时,CPU,扫描的数据块,执行时间的对比
- 扫描文件夹导入数据库,然后进行数据查询
- 解析Oracle数据扫描 Oracle SQL查询优化 总结篇
- Region中的数据操作之扫描(查询)
- sql连接查询避免全表扫描
- mysql查询导致全表扫描
- 解析Oracle数据扫描 Oracle SQL查询优化 提高局部范围数据扫描执行性能的原理
- 解析Oracle数据扫描 Oracle SQL查询优化 引导局部范围数据扫描的方法(1)
- 解析Oracle数据扫描 Oracle SQL查询优化 引导局部范围数据扫描的方法(2)
- 解析Oracle数据扫描 Oracle SQL查询优化 引导局部范围数据扫描的方法(3)
- 解析Oracle数据扫描 Oracle SQL查询优化 引导局部范围数据扫描的方法(4)
- 解析Oracle数据扫描 Oracle SQL查询优化 引导局部范围数据扫描的方法(5)
- 解析Oracle数据扫描 Oracle SQL查询优化 引导局部范围数据扫描的方法(6)
- 解析Oracle数据扫描 Oracle SQL查询优化 引导局部范围数据扫描的方法(7)
- 解析Oracle数据扫描 Oracle SQL查询优化 数据读取方式
- Java基础
- 关于常用的正则验证的js
- devops
- Unity让带有Rigidbody组件的游戏对象停止运动
- Python BeautifulSoup库 API
- DynamoDBMapper查询扫描表数据
- (开源工具)SwipeMenuListView滑动菜单的使用
- Minimum Sum LCM UVA
- linux实现磁盘配额设置
- Ronberg算法计算积分
- P2P NAT检测和穿越方式以及两者之间的关系
- 字符串类string类模板
- Java基础
- RedHat5 OpenSSL 升级步骤