技术控

    今日:27| 主题:49507
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Query DynamoDB Items with DynamoDBMapper

[复制链接]
▼素顔美死人 发表于 2016-10-3 22:26:35
67 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
On a previouspost we issued queries on a DynamoDB database using the low level java api.  
  Querying using the DynamoDBMapper is pretty easy.
  Issue a query using a hash key is as simple as it gets. The best candidate for a query like this would be the Users table by searching using the email hash key.
  1. public User getUser(String email) {
  2.         User user = dynamoDBMapper.load(User.class,email);
  3.         return user;
  4.     }
复制代码
Since we use only hashkey for the Users table, our result would be limited to one.
  The load function can also be used for composite keys. Therefore querying for a Logins Table Item would require a hash key and a range key.
  1. public Login getLogin(String email,Long date) {
  2.         Login login =  dynamoDBMapper.load(Login.class,email,date);
  3.         return login;
  4.     }
复制代码
Next step is to issue more complex queries using conditions. We will issue a query that will fetch the login attempts between two dates.
  1. public List<Login> queryLoginsBetween(String email, Long from, Long to) {
  2.         Map<String,String> expressionAttributesNames = new HashMap<>();
  3.         expressionAttributesNames.put("#email","email");
  4.         expressionAttributesNames.put("#timestamp","timestamp");
  5.         Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
  6.         expressionAttributeValues.put(":emailValue",new AttributeValue().withS(email));
  7.         expressionAttributeValues.put(":from",new AttributeValue().withN(Long.toString(from)));
  8.         expressionAttributeValues.put(":to",new AttributeValue().withN(Long.toString(to)));
  9.         DynamoDBQueryExpression<Login> queryExpression = new DynamoDBQueryExpression<Login>()
  10.                 .withKeyConditionExpression("#email = :emailValue and #timestamp BETWEEN :from AND :to ")
  11.                 .withExpressionAttributeNames(expressionAttributesNames)
  12.                 .withExpressionAttributeValues(expressionAttributeValues);
  13.         return dynamoDBMapper.query(Login.class,queryExpression);
  14.     }
复制代码
   We use DynamoDBQueryExpression, in the same manner that we used it in the low level api.
    The main difference is that we do not have to handle the paging at all. DynamoDBMapper will map the DynamoDB items to objects but also it will return a “lazy-loaded” collection. It initially returns only one page of results, and then makes a service call for the next page if needed.
      Last but not least querying on indexes is one of the basic actions. It is the same routine either for local or global secondary indexes.
    Keep in mind that the results fetched, depend on the projection type we specified once creating the Table. In our case the projection type is for all fields.
   
  1. public Supervisor getSupervisor(String company,String factory) {
  2.         Map<String,String> expressionAttributesNames = new HashMap<>();
  3.         expressionAttributesNames.put("#company","company");
  4.         expressionAttributesNames.put("#factory","factory");
  5.         Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
  6.         expressionAttributeValues.put(":company",new AttributeValue().withS(company));
  7.         expressionAttributeValues.put(":factory",new AttributeValue().withS(factory));
  8.         DynamoDBQueryExpression<Supervisor> dynamoDBQueryExpression = new DynamoDBQueryExpression<Supervisor>()
  9.                 .withIndexName("FactoryIndex")
  10.                 .withKeyConditionExpression("#company = :company and #factory = :factory ")
  11.                 .withExpressionAttributeNames(expressionAttributesNames)
  12.                 .withExpressionAttributeValues(expressionAttributeValues)
  13.                 .withConsistentRead(false);
  14.         List<Supervisor> supervisor = dynamoDBMapper.query(Supervisor.class,dynamoDBQueryExpression);
  15.         if(supervisor.size()>0) {
  16.             return supervisor.get(0);
  17.         } else {
  18.             return null;
  19.         }
  20.     }
复制代码
Pay extra attention to the fact that consistent read is set to false. DynamoDBQueryExpression uses by defaut consistent reads. When using a global secondary index you cannot issue a consistent read.
  You can find full source code with unit tests on    github.  
                              Reference:                          Query DynamoDB Items with DynamoDBMapperfrom ourJCG partner Emmanouil Gkatziouras at the          gkatziourablog.
友荐云推荐




上一篇:Messaging as a Service on OpenShift
下一篇:5 Practical Tips for Building Your Spring Boot API
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

映梦 发表于 2016-10-4 13:45:03
漏洞与补丁齐飞,蓝屏共死机一色!  
回复 支持 反对

使用道具 举报

朱海涛团队 发表于 2016-10-4 18:42:00
我了个去,顶了
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表