0

我正在尝试使用适用于 .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);

任何帮助,将不胜感激。

4

1 回答 1

2

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

字符串表名=“视频”;

        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”的分区键作为字符串。让我知道这是否适合您。

于 2018-09-13T21:53:03.683 回答