请选择 进入手机版 | 继续访问电脑版

技术控

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

[其他] Query DynamoDB Items with DynamoDBMapper

[复制链接]
▼素顔美死人 发表于 2016-10-3 22:26:35
85 2
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.
映梦 发表于 2016-10-4 13:45:03
漏洞与补丁齐飞,蓝屏共死机一色!  
回复 支持 反对

使用道具 举报

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

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表