Kum*_*mar 10 c# azure-storage azure-table-storage
我真的陷入了对Azure表存储的查询过滤器.我可以知道如何查询时间戳吗?当我单独查询分区键1005时,我得到了我不想要的完整表.当我添加带有时间戳的"和"条件时(尝试了很多格式,但它没有返回任何内容.下面的代码片段:
var lowerlimit = DateTime.Today.AddDays(-52).ToString("yyyy-MM-dd");
string dateRangeFilter = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "1005"),
TableOperators.And,
TableQuery.GenerateFilterCondition("TimeStamp", QueryComparisons.GreaterThanOrEqual, lowerlimit));
Run Code Online (Sandbox Code Playgroud)
Jon*_*ira 17
我可以将时间戳与Microsoft.WindowsAzure.Storage版本4.0.1.0一起使用.只是和榜样
var query = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("Level", QueryComparisons.Equal, "ERROR"),
TableOperators.And,
TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, DateTimeOffset.Now.AddDays(-20).Date));
var query2 = TableQuery.CombineFilters(query,
TableOperators.And,
TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, DateTimeOffset.Now));
var exQuery = new TableQuery<LogEntry>().Where(query2);
CloudTableClient tableClient = _storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(_tableName);
var results = table.ExecuteQuery(exQuery).Select(ent => (T) ent).ToList();
Run Code Online (Sandbox Code Playgroud)
如果这是一个诊断表(例如 WADLogsTable),我建议您将时间戳转换为分区键,然后对其进行查询。这将避免全表扫描,因为它是索引列。转换很简单,只需在刻度前面加上“0”即可。
var lowerlimitPartitionKey = DateTimeToPartitionKey(lowerlimit);
// Now query PartitionKey >= lowerlimitPartitionKey
...
Run Code Online (Sandbox Code Playgroud)
在哪里
private string DateTimeToPartitionKey(DateTime dt)
{
return "0" + dt.Ticks;
}
Run Code Online (Sandbox Code Playgroud)
时间戳可用于查询Azure表存储中的行。
但是,您需要使用TableQuery.GenerateFilterConditionForDate代替TableQuery.GenerateFilterCondition。对于任何DateTime列,TableQuery.GenerateFilterConditionForDate必须使用。
从您的代码示例,更改GenerateFilterCondition到GenerateFilterConditionForDate了时间戳过滤器:
var lowerlimit = DateTime.Today.AddDays(-52).ToString("yyyy-MM-dd");
string dateRangeFilter = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "1005"),
TableOperators.And,
TableQuery.GenerateFilterConditionForDate("TimeStamp", QueryComparisons.GreaterThanOrEqual, lowerlimit));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13915 次 |
| 最近记录: |