【问题标题】:cosmos not returning any records when using table api使用 table api 时 cosmos 不返回任何记录
【发布时间】:2018-12-05 19:20:02
【问题描述】:

如何使用table api查询数据库返回记录?

我的文档如下所示:

{
  "id": "069c2612-355a-4659-b680-048b7ef19f5c",
  "_rid": "3RtVAMJ2mQkBAAAAAAAAAA==",
  "_self": "dbs/3RtVAA==/colls/3RtVAMJ2mQk=/docs/3RtVAMJ2mQkBAAAAAAAAAA==/",
  "_etag": "\"00000000-0000-0000-8cc3-3e8f580501d4\"",
  "FieldType": "InsuranceCode",
  "TranslateFrom": "XTH",
  "TranslateTo": "removed",
  "SourcePaerty": "TMH",
  "DestinationParty": "GE",
  "Key": "/TMH/GE/InsuranceCode",
  "_attachments": "attachments/",
  "_ts": 1544032329
}

我正在尝试使用 cosmos 的 table api 简单地读取记录:

public static IEnumerable<Translation> ConnectAndFetch () {
    var connectionString = "myconnectionstring;";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse (connectionString);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient ();
    CloudTable table = tableClient.GetTableReference ("translationCollection");

    TableQuery<Translation> query = new TableQuery<Translation> ();

    return table.ExecuteQuery (query);
}

但是,不会返回任何记录!

我做错了什么?如何查询我的数据集并返回记录?

使用示例如下:

    static void Main(string[] args)
    {
        var mystuff = CosmosTableConnector.ConnectAndFetch().Take(5).ToList();
    }

你可以看到 0 个结果:

【问题讨论】:

  • 嗨,现在有更新吗?

标签: c# .net azure visual-studio-2017 azure-cosmosdb


【解决方案1】:

它尝试使用下面的 table api .net 代码来查询我在 cosmos db table api 中的数据。这个对我有用。

static void Main(string[] args)
    {

        //CloudTable table = CreateTableAsync("j").Result;
        //var result = QueryTableAsync("test").Result;
        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)
        {
            string cst = "DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***;TableEndpoint=https://***.table.cosmosdb.azure.com:443/;";
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(cst);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable table = tableClient.GetTableReference(tableName);

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

            Console.WriteLine(filter);

            var query = new TableQuery<ResponseEntity>();

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

            return result;

    }

我的数据:

输出:

根据您的示例数据,我推测您的数据存储在 Cosmos 中 SQL API,而不是 Table API。然后是不同的数据库。

如果要查询sql api,需要参考这个sdk:https://docs.microsoft.com/en-us/dotnet/api/overview/azure/cosmosdb?view=azure-dotnet

【讨论】:

  • 你为什么假设我的数据在 cosmos sql api 而不是 cosmos table api 中?我不确定你的例子有什么帮助,因为它没有做我正在做的事情
  • 因为我注意到您的示例文档包含 _etag、_ts、_self 列。据我所知,这些是 cosmos db sql api 中系统生成的属性。当然,你可以在table api中添加这些列。
  • 而且我的例子不是sql api,是table api,你试过了吗?
猜你喜欢
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
相关资源
最近更新 更多