How to set limit of matching items returned by DynamoDB using Java?

后端 未结 3 806
孤城傲影
孤城傲影 2021-01-04 20:29

In my Android app, I want to query data from DynamoDB. There will be a thousand matching items, but I just want to get only the first 10 of them. I don\'t know how to set th

3条回答
  •  情书的邮戳
    2021-01-04 20:58

    This is how you can limit a query result using aws-java-sdk ver 1.10.61

    import com.amazonaws.AmazonClientException;
    import com.amazonaws.auth.AWSCredentialsProvider;
    import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
    import com.amazonaws.regions.Region;
    import com.amazonaws.regions.Regions;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsyncClient;
    import com.amazonaws.services.dynamodbv2.document.DynamoDB;
    import com.amazonaws.services.dynamodbv2.document.Item;
    import com.amazonaws.services.dynamodbv2.document.ItemCollection;
    import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
    import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec; 
    
    
    
    AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
    AmazonDynamoDBAsyncClient client = new AmazonDynamoDBAsyncClient(provider);
    Region region = Region.getRegion(Regions.fromName(DYNAMODB_REGION));
    client.setRegion(region);
    dynamoDB = new DynamoDB(client);
    
    QuerySpec querySpec = new QuerySpec();
    
    /* MAX 1 item */
    querySpec.setMaxResultSize(1);
    
    querySpec.withHashKey("myPartitionKeyName", partitionKeyValue);
    
    ItemCollection query = dynamoDB.getTable(DYNAMODB_TABLE).query(querySpec);
    

提交回复
热议问题