AWS开发工具包.NET DynamoDB ASYNC

问题描述 投票:0回答:1

我正在尝试使用AWS SDK for .NET Core。

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

.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;

具有自动增量字段的更新和项目

遗憾的是,这是我遇到问题的地方。旧的文档可以在原子计数器部分找到。 (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetItemCRUD.html

无效的ConditionExpression:语法错误;令牌:\“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个回答
0
投票

我能够使用以下代码更新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”的分区键为字符串。如果这对您有用,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.