Kusto 客户端库Kusto client library

Kusto 客户端 SDK (Kusto.Data) 公开了类似于 ADO.NET 的编程 API,因此,对于那些有 .NET 经验的人来说,使用它应该感觉很自然。The Kusto Client SDK (Kusto.Data) exposes a programmatic API similar to ADO.NET, so using it should feel natural for those experienced with .NET. 可以从指向 Kusto 引擎服务、数据库、身份验证方法等的连接字符串对象创建查询客户端 (ICslQueryProvider) 或控制命令提供程序 (ICslAdminProvider)。然后,可以通过指定适当的 Kusto 查询语言字符串来发出数据查询或控制命令,并通过返回的 IDataReader 对象获取一个或多个数据表。You create either a query client (ICslQueryProvider) or a control command provider (ICslAdminProvider) from a connection string object pointing at the Kusto engine service, database, authentication method, etc. You can then issue data queries or control commands by specifying the appropriate Kusto query language string, and get back one or more data tables via the returned IDataReader object.

更具体地说,若要创建允许对 Kusto 进行查询的类似于 ADO.NET 的客户端,请使用 Kusto.Data.Net.Client.KustoClientFactory 类上的静态方法。More concretely, to create an ADO.NET-like client allowing queries against Kusto, use static methods on the Kusto.Data.Net.Client.KustoClientFactory class. 这些方法接受连接字符串并创建线程安全的、可释放的客户端对象。These take the connection string and create a thread-safe, disposable, client object. (强烈建议不要让客户端代码创建此对象的“过多”实例,(It's strongly recommended that client code does not create "too many" instances of this object. 而应让客户端代码为每个连接字符串创建一个对象,并在必要情况下长时间保留该对象。)这样,客户端对象就可以有效缓存资源。Instead, client code should create an object per connection string and hold on to it for as long as necessary.) This allows the client object to efficiently cache resources.

通常,客户端上的所有方法都是线程安全的,但有两个例外:Dispose 和 setter 属性。In general, all methods on the clients are thread-safe with two exceptions: Dispose, and setter properties. 为了获得一致的结果,请勿以并发方式调用任一方法。For consistent results, do not invoke either methods concurrently.

下面是几个示例。Following are a few examples. 可在此处找到其他示例。Additional samples can be found here.

示例:计算行数Example: Counting Rows

以下代码演示如何计算名为 Samples 的数据库中名为 StormEvents 的表的行数:The following code demonstrates counting the rows of a table named StormEvents in a database named Samples:

var client = Kusto.Data.Net.Client.KustoClientFactory.CreateCslQueryProvider("https://help.kusto.chinacloudapi.cn/Samples;Fed=true");
var reader = client.ExecuteQuery("StormEvents | count");
// Read the first row from reader -- it's 0'th column is the count of records in MyTable
// Don't forget to dispose of reader when done.

示例:从 Kusto 群集获取诊断信息Example: Getting diagnostics info from the Kusto cluster

var kcsb = new KustoConnectionStringBuilder(cluster URI here). WithAadUserPromptAuthentication();
using (var client = KustoClientFactory.CreateCslAdminProvider(kcsb))
    var diagnosticsCommand = CslCommandGenerator.GenerateShowDiagnosticsCommand();
    var objectReader = new ObjectReader<DiagnosticsShowCommandResult>(client.ExecuteControlCommand(diagnosticsCommand));
    DiagnosticsShowCommandResult diagResult = objectReader.ToList().FirstOrDefault();
    // DO something with the diagResult    

KustoClientFactory 客户端工厂The KustoClientFactory client factory

静态类 Kusto.Data.Net.Client.KustoClientFactory 为使用 Kusto 的客户端代码的作者提供主入口点。The static class Kusto.Data.Net.Client.KustoClientFactory provides the main entry point for authors of client code that utilizes Kusto. 它提供了以下重要的静态方法:It provides the following important static methods:

方法Method 返回Returns 用途Used for
CreateCslQueryProvider ICslQueryProvider 向 Kusto 引擎群集发送查询。Sending queries to a Kusto engine cluster.
CreateCslAdminProvider ICslAdminProvider 向 Kusto 群集(任何类型)发送控制命令。Sending control commands to a Kusto cluster (of any kind).
CreateRedirectProvider IRedirectProvider 为 Kusto 请求创建重定向 HTTP 响应消息。Creating a redirect HTTP response message for a Kusto request.