如何在Azure表中使用日期作为分区键?

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

我正在创建一个表,每天将在批处理过程中存储大量记录。这些记录将用于给定日期的查询,因此我决定使用短日期(“MM-dd-yyyy”)作为分区键会很好。

我创建这样的实体:

ITableEntity recordEntity = new DynamicTableEntity()
{
    PartitionKey = DateTime.ParseExact((string)record["authorization_date"], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToShortDateString(),
    RowKey = Guid.NewGuid().ToString(),
    Properties =
    {
        {"customer", new EntityProperty(customer)},
        {"responseId", new EntityProperty(record["response_id"].ToString())},
        {"Date", new EntityProperty(DateTime.ParseExact((string)record["date"], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal))},
    }
};

实体已按预期创建,但它抛出了 storageException 并显示消息:“远程服务器返回错误:(400) 错误请求。”。

这就是实体的外观:

ETag [string]: null
PartitionKey [string]: "2/22/2024"
Properties [IDictionary]: Count = 11
RowKey [string]: "a5768ba3-eb0e-4eb9-97b8-38eec67bc5f3"
Timestamp [DateTimeOffset]: {1/1/0001 12:00:00 AM +00:00}

以及我如何将记录插入表中。

try
{
    TableOperation insertTransactionOperation = TableOperation.Insert(recordEntity);
    recordTable.Execute(insertTransactionOperation);
    storedRecords++;
}
catch (System.Exception ex)
{
    _logger.LogError(ex.Message);
    errorRecords++;
    throw;
}

我将partitionKey值更改为“ts”之类的普通文本字符串,并且存储成功。我认为问题是由于日期格式造成的,但我不知道如何解决。归根结底,它是一个普通的字符串。

谢谢你,

c# exception azure-storage azure-table-storage
1个回答
0
投票

您收到错误的原因是 PartitionKey 的值包含不允许的

/
字符。您可能希望在 PartitionKey 名称中使用
-
而不是
/
(相同的规则也适用于 RowKey。

请参阅此链接了解更多详细信息:https://learn.microsoft.com/en-us/rest/api/storageservices/understanding-the-table-service-data-model#characters-disallowed-in-key-领域

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