cosmos not returning any records when using table api

谁说我不能喝 提交于 2019-12-24 01:57:10

问题


How do you query the database to return records using the table api?

My documents look like this:

{
  "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
}

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

However, no records are returned!

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

Example of usage is below:

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

And as you can see 0 results:


回答1:


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;

    }

My data :

Output:

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

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



来源:https://stackoverflow.com/questions/53639330/cosmos-not-returning-any-records-when-using-table-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!