【问题标题】:Unable to query by Timestamp in CosmosDB with Table API无法使用表 API 在 CosmosDB 中按时间戳查询
【发布时间】:2019-03-21 04:43:30
【问题描述】:

我正在创建一个查询以包含过去 30 天的 Cosmos 实体:

var filter = TableQuery.GenerateFilterConditionForDate(
                "Timestamp", 
                QueryComparisons.GreaterThanOrEqual,
                DateTimeOffset.Now.Date.AddDays(-30));

接下来我使用这个过滤器创建一个查询:

var query = new TableQuery<ResponseEntity>().Where(filter);

接下来我执行查询:

var result = await table.ExecuteQuerySegmentedAsync(query, null);

但是,出于某种原因,result 始终包含零 (0) 个命中。

如果我在没有任何过滤器的情况下执行查询...

var query = new TableQuery<ResponseEntity>();

...我确实得到了所有实体。

查看生成的过滤器字符串,我觉得它没问题(在使用 Cosmos 的查询构建器时与 Azure 门户中的相同):

Timestamp ge datetime'2018-09-15T22:00:00.0000000Z'

基于Timestamp的查询有什么限制吗?

编辑: 尝试切换到新的 Microsoft.Azure.Cosmos.Table NuGet 包(当前处于预览状态,版本 0.9.1),但我仍然没有得到任何结果按Timestamp过滤。

【问题讨论】:

  • 您好,我的回答对您有帮助吗?
  • @JayGong 我还没有机会尝试,但我无法立即看到任何重大差异?您是否还针对 .NET Core?您使用的是哪个版本的 Azure 存储 NuGet 包?
  • 泰德,没关系,等着你的测试。我的示例代码刚刚在 .net 控制台测试应用程序中运行,我安装的 nuget 包是 Microsoft.Azure.Storage.Common 9.0.0.1-preview 和 Microsoft.Azure.CosmosDB.Table 2.0.0。

标签: .net-core azure-storage azure-cosmosdb


【解决方案1】:

请参考我的工作代码。

代码:

using Microsoft.Azure.CosmosDB.Table;
using Microsoft.Azure.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JayGongCosmosTable
{
    class Program
    {
        static void Main(string[] args)
        {
            TableQuerySegment <ResponseEntity>  resultE=  QueryTableAsync("test").Result;
            foreach(ResponseEntity re in resultE)
            {                    
                Console.WriteLine("Timestamp:   "+re.Timestamp);
                Console.WriteLine("------------------------------------------");
            }
            Console.WriteLine("execute done");
            Console.ReadLine();
        }

        public static async Task<TableQuerySegment<ResponseEntity>> QueryTableAsync(string tableName)
        {
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString("***");
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable table = tableClient.GetTableReference(tableName);

            var filter = TableQuery.GenerateFilterConditionForDate(
                "Timestamp",
                QueryComparisons.GreaterThanOrEqual,
                //QueryComparisons.LessThanOrEqual,
                DateTimeOffset.Now.AddDays(-10).Date);

            Console.WriteLine(filter);

            var query = new TableQuery<ResponseEntity>().Where(filter);

            var result = await table.ExecuteQuerySegmentedAsync(query, null);

            return result;

        }
    }


    class ResponseEntity : TableEntity
    {    
        public string Name { get; set; }

        public DateTimeOffset logtime { get; set; }

    }
}

我的数据列表如下,没有过滤器:

如果我使用时间戳作为过滤器,它可以工作:

我想提的另一件事是,如果可能,请避免使用timestamp 间隔查询。这样的查询将导致在服务器端进行全表扫描。如果您的场景通常需要timestamp区间查询,请考虑选择timestamp作为分区键或行键以优化查询性能。


总结一下,最后,解决方案是uninstalling WindowsAzure.Storage,然后将 using 语句切换为使用来自Microsoft.Azure.CosmosDB.Table 的类型。

【讨论】:

  • 这与我所做的几乎相同,但它不起作用。我正在使用Microsoft.Azure.DocumentDB.Core 2.1.3WindowsAzure.Storage 9.3.2
  • @TedNyberg 不,不,请使用Microsoft.Azure.CosmosDB.Table。我以前用过WindowsAzure.Storage,但失败了,用Microsoft.Azure.CosmosDB.Table成功
  • Microsoft.Azure.CosmosDB.Table 包似乎不适用于 .NET Core。 ://
  • 还尝试了Microsoft.Azure.Cosmos.Table包的预览,但也没有用。
  • @TedNyberg 是的!我已经在回答中总结了解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多