使用表api时cosmos不返回任何记录 [英] cosmos not returning any records when using table api

查看:102
本文介绍了使用表api时cosmos不返回任何记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you query the database to return records using the 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
}

我正尝试使用表api读取宇宙的记录:

I'm attempting to simply read records using the table api for cosmos:

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);
}

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

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

What am I doing wrong? How do I query my dataset and return records?

用法示例如下:

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

您将看到0个结果:

推荐答案

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

It tried below table api .net code to query my data in cosmos db table api. It works for me.

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中

Based on your sample data, I surmise that your data is stored inside the Cosmos SQL API,not Table API. Then are different database.

如果要查询sql api,则需要引用此sdk:

If you want to query sql api, you need to refer to this sdk:https://docs.microsoft.com/en-us/dotnet/api/overview/azure/cosmosdb?view=azure-dotnet

这篇关于使用表api时cosmos不返回任何记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆