使用AWSSDK.net(核心版本3.3.5和AWSSDK.DynamoDBv2版本3.3.1)从DynamoDB检索数据

时间:2016-12-11 12:47:44

标签: c# angularjs amazon-web-services amazon-dynamodb

我正在尝试使用扫描请求根据搜索条件从Dynamo DB检索数据。我正在按照http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetScanning.html"中提到的步骤进行操作页。我的DynamoDB表包含超过100万条记录。我知道通过使用do-while循环和ExclusiveStartKey,我们可以从dynamo DB中获取记录。但在我的情况下,我不能等到搜索过程完成,因为这将挂起我的angularJS UI。相反,我希望逐步加载数据,而不是等待搜索过程完成。我们该怎么做。?

样品申请:

var lastEvaluatedKey = new Dictionary<string, AttributeValue>(); ;
    AmazonDynamoDBClient amazonDynamoDbClient= new AmazonDynamoDBClient()
        var filterExpression = "#aws_s3_bucket = :v_aws_s3_bucket and contains(#aws_s3_key,:v_aws_s3_key)";
        var projectExpression = "#aws_s3_key,filename,#region,aws_s3_bucket,#projecttype,folder,#siteid,locationname,createdon,modifiedon";



        do
        {   
        var request = new ScanRequest
            {
                TableName = "Test1",
                ExclusiveStartKey=lastEvaluatedKey,

                FilterExpression = filterExpression,
                ExpressionAttributeNames = new Dictionary<string, string>
                            {
                              { "#region", "region" },
                              { "#siteid", "siteid" },
                              { "#projecttype", "projecttype" },
                              { "#aws_s3_key", "aws_s3_key" },
                              { "#aws_s3_bucket", "aws_s3_bucket" }                       
                            },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
                            {":v_aws_s3_bucket", new AttributeValue { S =  "sampleBucket"}},
                            {":v_aws_s3_key", new AttributeValue { S =  "92226"}}
                            },

                ConsistentRead = true,
                ProjectionExpression = projectExpression
            };

        response = amazonDynamoDbClient.Scan(request); 
    lastEvaluatedKey = response.LastEvaluatedKey;}while(lastEvaluatedKey!=null && lastEvaluatedKey.count()!=0)

我尝试执行上述请求而不使用do-while循环并保存了ExclusiveStartKey,因为它抛出了下一个请求错误&#34;提供的起始键无效:一个或多个参数值无效:Null属性值类型的值必须为true&#34;。

对此问题的任何帮助都会有所帮助......

1 个答案:

答案 0 :(得分:0)

您获得的错误似乎是因为您在请求中设置ExclusiveStartKey而未设置其参数的任何值。请注意,在收到您的回复后,您不会更新request.ExclusiveStartKey。显然,如果你不这样做,当你达到极限时,扫描将不知道在哪里再次拿起。见下文。

AmazonDynamoDBClient amazonDynamoDbClient= new AmazonDynamoDBClient()
var filterExpression = "#aws_s3_bucket = :v_aws_s3_bucket and contains(#aws_s3_key,:v_aws_s3_key)";
var projectExpression = "#aws_s3_key,filename,#region,aws_s3_bucket,#projecttype,folder,#siteid,locationname,createdon,modifiedon";
ScanRequest request = new ScanRequest
        {
            TableName = "Test1",
            FilterExpression = filterExpression,
            ExpressionAttributeNames = new Dictionary<string, string>
                        {
                          { "#region", "region" },
                          { "#siteid", "siteid" },
                          { "#projecttype", "projecttype" },
                          { "#aws_s3_key", "aws_s3_key" },
                          { "#aws_s3_bucket", "aws_s3_bucket" }                       
                        },
            ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
                        {":v_aws_s3_bucket", new AttributeValue { S =  "sampleBucket"}},
                        {":v_aws_s3_key", new AttributeValue { S =  "92226"}}
                        },

            ConsistentRead = true,
            ProjectionExpression = projectExpression
        };

    do
    {   
        response = amazonDynamoDbClient.Scan(request); 
        request.ExclusiveStartKey = response.LastEvaluatedKey;
    } while (response.lastEvaluatedKey.Count != 0);