扫描操作时在Dynamo db中进行分页 [英] Pagination in Dynamo db while scan operation

查看:155
本文介绍了扫描操作时在Dynamo db中进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我的Dynamo db表进行分页扫描.在我的请求中,我想从分页开始的地方发送该号码.说,例如我以start = 3和limit = 10发送请求,其中start是我希望扫描从表中的第三项开始,并且限制为最多10个项目.但是我可以用.withLimit()方法实现限制(我正在使用Java).我遵循了 aws文档.以下是我要实现的代码:

I want to scan my Dynamo db table with pagination applied to it. In my request I want to send the number from where I want pagination to get start. Say, e.g. I am sending request with start = 3 and limit = 10, where start is I want scan to start with third item in the table and limit is upto 10 items. Limit however I can implement with .withLimit() method(I am using java). I followed this aws document. Following is the code of what I want to achieve:

<Map<String, AttributeValue>> mapList = new ArrayList<>();
      AmazonDynamoDB client =AmazonDynamoDBClientBuilder.standard().build();

      Gson gson = new GsonBuilder().serializeNulls().create();

      Map<String, AttributeValue> expressionAttributeValues = new 
      HashMap<String,AttributeValue>(); 
      expressionAttributeValues.put(":name",
      newAttributeValue().withS(name));

      List<ResponseDomain> domainList = new ArrayList<>(); 
      ResponseDomain responseDomain = null;

      //lastKeyEvaluated = start
      Map<String, AttributeValue> lastKeyEvaluated = null; 
      do { 
      ScanRequest scanRequest = new 
      ScanRequest().withTableName(STUDENT_TABLE)
      .withProjectionExpression("studentId, studentName")
      .withFilterExpression("begins_with(studentName, :name)")
      .withExpressionAttributeValues(expressionAttributeValues).
      withExclusiveStartKey(lastKeyEvaluated);

      ScanResult result = client.scan(scanRequest);

      for (Map<String, AttributeValue> item : result.getItems()) { 
      responseDomain = gson.fromJson(gson.toJson(item), 
      ResponseDomain.class); 
      domainList.add(responseDomain);


      } lastKeyEvaluated = result.getLastEvaluatedKey(); 
      } while (lastKeyEvaluated!= null); 
      //lastKeyEvaluated = size

      return responseDomain;

在上面的代码中,我被卡在3个地方:

In the above code I am stuck at 3 places:

  1. 如何将lastKeyEvaluated设置为我的起始值,即3
  2. 在while条件下,如何指定我的限制,即10
  3. 当我尝试将项目从Json映射到我的域类时,遇到错误.

我误解了dynamodb中的分页概念或在代码中做错了什么.当我是新手时,任何指导都将受到高度赞赏.

Am I misinterpreting the concept of pagination in dynamodb or doing something wrong in the code. Any guidance will be highly appreciated as I am a newbie.

推荐答案

  1. 您只能通过 ExclusiveStartKey 从某个地方开始阅读.此键是表的主键.如果您知道自己的项目密钥,则可以这样使用它(例如,表主键为 studentId ):

  1. You can only start reading from some place by the ExclusiveStartKey. This key is the primary key of your table. If you know your item key, you can use it like this (e.g. your table primary key is studentId):

Map<String, AttributeValue> lastKeyEvaluated = new HashMap<String,AttributeValue>();
lastKeyEvaluated.put("studentId", new AttributeValue(STUDENTID));

  • 在dynamo中指定 limit = N 时,您正在设置它只能从表中读取 N 个项目.读取 N 个项目后,将应用所有过滤.请参见限制结果集中的项目数. 这可能会给您带来比预期少的结果.因此,您可以在代码中创建一个变量来发送请求,直到达到预期的限制并切断多余的结果为止.

  • When you specify the limit = N in dynamo you are setting that it should only read N items from the table. Any filtering is applied after the N items have been read. See Limiting the Number of Items in the Result Set. That might leave you with less results than expected. So you could create a variable in your code to send requests until you hit your expected limit and cutoff the extra results.

    int N = 10;
    List<Map<String, AttributeValue>> itemsList = new ArrayList<>();
    do {
       // scanRequest.withLimit(N)
    
       ...
    
       itemList.addAll(result.getItems());
    
       if(itemsList.size() >= N) {
           itemsList = itemsList.subList(0, N);
           break;
       }
    } while (lastKeyEvaluated != null && itemsList.size() < N);
    
    // process the itemsList
    

  • Dynamo使用其自己的json结构.请参见发电机响应语法. 您可以通过在dynamo中存储属性的方式来获取属性的值.如果 studentId 是字符串,则可能是这样的:

  • Dynamo uses it’s own json structure. See Dynamo response syntax. You can get the value of an attribute the way you stored it in dynamo. If studentId is a string then it could be something like this:

    for (Map<String, AttributeValue> item : result.getItems()) {
        responseDomain = new ResponseDomain();
        responseDomain.setId(item.get("studentId").getS());
        domainList.add(responseDomain);
    }
    

  • 这篇关于扫描操作时在Dynamo db中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆