控制台应用程序使用Azure存储Tableapi [英] Console app to use azure storage tableapi

查看:76
本文介绍了控制台应用程序使用Azure存储Tableapi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请提供一些示例(控制台应用程序),该示例发送有关使用OData查询天蓝色表存储的http请求?

Please provide some examples (console application) on sending http request to query an azure table storage using OData?

推荐答案

更新

您可以像我的图片一样在门户网站上找到您的sastoken.

You can find your sastoken at portal like my pic.

并且,您还需要更新HttpHelper文件中的x-ms-date(必需.为请求指定协调世界时(UTC).)的值.

And u also need to update the value of x-ms-date(Required. Specifies the Coordinated Universal Time (UTC) for the request.) in HttpHelper file .

有关更多详细信息,您可以在github中下载我的演示代码(您可以下载我的文件).

For more details, you can download my demo code in github( You can download my HttpHelper file).

using Microsoft.Azure.Cosmos.Table;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using static ODatafilter.HttpHelper;

namespace ODatafilter
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Azure Cosmos Table Samples");
            string baseurl = @"https://panshubeistorage.table.core.windows.net/";
            string tbname = "People";//Console.ReadLine();
            string sastoken = @"?sv=2019-10-10&ss=************";
            string filter = @"&$filter=PartitionKey%20eq%20'Smith'%20";
            baseurl = baseurl + tbname + "()" + sastoken+filter;
            HttpResponseData data = HttpHelper.GetForOData(baseurl);
            string responseData = data.Data.Replace(".","_");
            ODataResponse odata = JsonConvert.DeserializeObject<ODataResponse>(responseData);

            foreach (ODatavalue m in odata.value)
            {
                Console.WriteLine(m.PartitionKey + "    " + m.PhoneNumber + "    " + m.RowKey + "   " + m.Email);
            }
            Console.WriteLine("Press any key to exit");
            Console.Read();
        }
        public class ODataResponse
        {
            public string odata_metadata { get; set; }
            public List<ODatavalue> value { get; set; }
        }
        public class ODatavalue {
            public string odata_type { get; set; }
            public string odata_id { get; set; }
            public string odata_etag { get; set; }
            public string odata_editLink { get; set; }
            public string Timestamp { get; set; }
            public string PartitionKey { get; set; }
            public string RowKey { get; set; }
            public string Email { get; set; }
            public string PhoneNumber { get; set; }
        }
    }
}

重要

您可以使用LinQ像支持的文档一样进行查询.

You can use LinQ to query like the document like you supported.

    static async Task Main(string[] args)
    {
        Console.WriteLine("Azure Cosmos Table Samples");
        Console.WriteLine("Query data by filter");

        CloudTable table = GetTable();

        Console.WriteLine("pls input PartitionKey:");
        string PartitionKey = Console.ReadLine();
        Console.WriteLine("pls input RowKey:");
        string RowKey = Console.ReadLine();
        //Query
        IQueryable<CustomerEntity> linqQuery = table.CreateQuery<CustomerEntity>()
        .Where(x => x.PartitionKey== PartitionKey && x.RowKey== RowKey)
        .Select(x => new CustomerEntity() { PartitionKey = x.PartitionKey, RowKey = x.RowKey, Email = x.Email, PhoneNumber= x.PhoneNumber });

        var list = linqQuery.ToList<CustomerEntity>();

        foreach (CustomerEntity m in list)
        {
            Console.WriteLine(m.PartitionKey+"    "+m.PhoneNumber+"    "+m.RowKey+"   "+m.Email);
        }
        Console.WriteLine();
        Console.WriteLine("Press any key to exit");
        Console.Read();
    }

    public static CloudTable GetTable() {
        CloudStorageAccount account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=***x=core.windows.net");
        CloudTableClient tableClient = account.CreateCloudTableClient();
        CloudTable table = tableClient.GetTableReference("People");
        return table;
    }

这篇关于控制台应用程序使用Azure存储Tableapi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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