【问题标题】:AWS SDK .NET DynamoDB ASYNCAWS 开发工具包 .NET DynamoDB ASYNC
【发布时间】:2018-09-14 05:53:03
【问题描述】:

我正在尝试使用适用于 .NET Core 的 AWS 开发工具包。

  • 创建一个表格来统计视频的观看次数。
  • 添加一天的观看次数。
  • 增加一天的现有计数。
  • 查询视频在两个日期之间的视频计数。

.NET Core AWS SDK 使用 AWS 中未记录的异步方法。他们的 github 页面上有一个功能请求来实现这一点......但它是从去年开始的。 (https://github.com/aws/aws-sdk-net/issues/787)

创建表格

这工作并在 AWS 控制台上创建一个表。

var ctRequest = new CreateTableRequest
{
    AttributeDefinitions = new List<AttributeDefinition>()
    {
        new AttributeDefinition
        {
            AttributeName = "ViewUid",
            AttributeType = ScalarAttributeType.S
        },
        new AttributeDefinition
        {
            AttributeName = "ViewDate",
            AttributeType = ScalarAttributeType.S
        }
    },
    KeySchema = new List<KeySchemaElement>
    {
        new KeySchemaElement
        {
            AttributeName = "ViewUid",
            KeyType = KeyType.HASH //Partition key
        },
        new KeySchemaElement
        {
            AttributeName = "ViewDate",
            KeyType = KeyType.RANGE
        }
    },
    ProvisionedThroughput = new ProvisionedThroughput
    {
        ReadCapacityUnits = 5,
        WriteCapacityUnits = 6
    },
    TableName = _settings.AWSDynamoDBViewCountTable
};

var response = _client.CreateTableAsync(ctRequest).Result;

自动增加字段的更新和项目

很遗憾,这就是我遇到问题的地方。旧文档可在 Atomic Counter 部分下找到。 (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetItemCRUD.html)

无效的条件表达式:语法错误;令牌:\"SET\",附近:\"SET VC\"

var viewData = new Document();
viewData["ViewUid"] = videoUid; //Table entry UID
viewData["VideoId"] = videoId;  // Video ID
viewData["ViewDate"] = date;
viewData["ViewCount"] = 0;
//Document result = await _viewCountTable.PutItemAsync(viewData);

Expression expr = new Expression();
expr.ExpressionStatement = "SET #VC = #VC + :val";
expr.ExpressionAttributeValues[":val"] = 1;
expr.ExpressionAttributeNames["#VC"] = "ViewCount";
var updateConfig = new UpdateItemOperationConfig() { 
    ConditionalExpression = expr,
    ReturnValues = ReturnValues.UpdatedNewAttributes
};

var result = await _viewCountTable.UpdateItemAsync(viewData, updateConfig);
return result;

查询日期范围

获取一个视频在某个日期范围内的观看次数。

string queryTimeSpanStartString = dateFrom.ToString(AWSSDKUtils.ISO8601DateFormat);
string queryTimeSpanEndString = dateTo.ToString(AWSSDKUtils.ISO8601DateFormat);
var request = new QueryRequest
{
    TableName = _settings.AWSDynamoDBViewCountTable,
    KeyConditions = new Dictionary<string, Condition>()
    {
        {
            "VideoId",  new Condition()
            {
                ComparisonOperator = "EQ",
                AttributeValueList = new List<AttributeValue>()
                {
                    new AttributeValue { S = videoId }
                }
            }
        },
        {
            "ViewDate",
            new Condition
            {
                ComparisonOperator = "BETWEEN",
                AttributeValueList = new List<AttributeValue>()
                {
                    new AttributeValue { S = queryTimeSpanStartString },
                    new AttributeValue { S = queryTimeSpanEndString }
                }
            }
        }
    }
};

var response = await _client.QueryAsync(request);

任何帮助将不胜感激。

【问题讨论】:

    标签: c# amazon-dynamodb aws-sdk-net


    【解决方案1】:

    我能够使用以下代码更新 ViewCount:

    string tableName = "videos";

            var request = new UpdateItemRequest
            {
                Key = new Dictionary<string, AttributeValue>() { { "ViewUid", new AttributeValue { S = "replaceVideoIdhere" } } },
                ExpressionAttributeNames = new Dictionary<string, string>()
                {
                    {"#Q", "ViewCount"}
                },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    {":incr", new AttributeValue {N = "1"}}
                },
                UpdateExpression = "SET #Q = #Q + :incr",
                TableName = tableName
            };
    
            var response = await _dynamoDbClient.UpdateItemAsync(request);
    

    我创建了一个名为“videos”的表,其中一个名为“ViewUid”的分区键作为字符串。让我知道这是否适合您。

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多