AWS开发工具包.NET DynamoDB ASYNC

时间:2018-09-13 09:31:08

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

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

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

.NET Core AWS开发工具包使用AWS中未记录的Async方法。他们的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);

任何帮助将不胜感激。

0 个答案:

没有答案