Applies to: ✅ Microsoft Fabric ✅ Azure Data Explorer
In this article, you learn how to:
- Create your first client app
- Use interactive authentication
- Run a basic query that prints Hello Kusto!
Prerequisites
Set up your development environment to use the Kusto client library.
Create your app
In your preferred IDE or text editor, create a project or file named hello kusto using the convention appropriate for your preferred language. Then add the following code:
Add the Kusto client and string builder classes.
using Kusto.Data;
using Kusto.Data.Net.Client;
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
import { Client as KustoClient, KustoConnectionStringBuilder } from "azure-kusto-data";
import { InteractiveBrowserCredentialInBrowserOptions } from "@azure/identity";
Note
For Node.js apps, use InteractiveBrowserCredentialNodeOptions
instead of InteractiveBrowserCredentialInBrowserOptions
.
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.ClientFactory;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.KustoResultSetTable;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
Define an empty function named main
and call it.
namespace HelloKusto {
class HelloKusto {
static void Main(string[] args) {
}
}
}
def main():
if __name__ == "__main__":
main()
async function main()
{
}
main();
public class HelloKusto
{
public static void main(String[] args) throws Exception {
try {
}
}
}
Create a connection string builder object that defines the cluster URI and sets the authentication mode to interactive. For more information about the cluster URI, see Kusto connection strings.
var clusterUri = "https://help.kusto.windows.net/";
var kcsb = new KustoConnectionStringBuilder(clusterUri).WithAadUserPromptAuthentication();
cluster_uri = "https://help.kusto.windows.net"
kcsb = KustoConnectionStringBuilder.with_interactive_login(cluster_uri)
The clientId
and redirectUri
are from the Microsoft Entra app registration you created in the Prerequisites section of Set up your development environment.
const clusterUri = "https://help.kusto.windows.net";
const authOptions = {
clientId: "00001111-aaaa-2222-bbbb-3333cccc4444",
redirectUri: "http://localhost:5173",
} as InteractiveBrowserCredentialInBrowserOptions;
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri, authOptions);
Note
For Node.js apps, use InteractiveBrowserCredentialNodeOptions
instead of InteractiveBrowserCredentialInBrowserOptions
.
String clusterUri = "https://help.kusto.windows.net/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
Note
For interactive authentication, you need a Microsoft account or a Microsoft Entra user identity. An Azure subscription isn't required.
In C#, the interactive authentication process may not prompt the user if:
- The user is already authenticated on the device
- There is an existing Kusto.Explorer or Azure Data Explorer web UI authentication on the device
Create a client object that uses the connection string builder object to connect to the cluster.
Note
We highly recommended that you cache and reuse the Kusto client instance. Frequently recreating Kusto clients may lead to performance degradation in your application and increased load on your cluster.
using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
}
with KustoClient(kcsb) as kusto_client:
const kustoClient = new KustoClient(kcsb);
try (Client kustoClient = ClientFactory.createClient(kcsb)) {
}
Define the database and query to run. The query prints Hello Kusto! in a column named Welcome.
var database = "Samples";
var query = "print Welcome='Hello Kusto!'";
database = "Samples"
query = "print Welcome='Hello Kusto!'"
const database = "Samples";
const query = "print Welcome='Hello Kusto!'";
String database = "Samples";
String query = "print Welcome='Hello Kusto!'";
Run the query and print the result.
using (var response = kustoClient.ExecuteQuery(database, query, null)) {
response.Read();
int columnNo = response.GetOrdinal("Welcome");
Console.WriteLine(response.GetString(columnNo));
}
response = kusto_client.execute(database, query)
print(response.primary_results[0][0]["Welcome"])
const response = await kustoClient.execute(database, query);
console.log(response.primaryResults[0][0]["Welcome"].toString());
KustoOperationResult response = kustoClient.execute(database, query);
KustoResultSetTable primary_results = response.getPrimaryResults();
primary_results.next();
System.out.println(primary_results.getString("Welcome"));
Note
The query output is returned in the response as an object that contains one or more tables, comprised of one more more rows and columns.
The format of the object depends on the client library language.
The print kusto query returns a single table with one row and column.
The response is a DataReader object. You can reference the result, as follows:
- Use the Read() method to read the first row
- Use the GetString() method to get the value of the first column
The response in the primary results JSON object. The object contains an array of tables, which in turn contains an array of rows. Each row contains data organized into a dictionary of columns. You can reference the result, as follows:
- The first array index
[0]
references the first table
- The second array index
[0]
references the first row
- The dictionary key
["Welcome"]
references the Welcome column
The response is a KustoOperationResult object. You can reference the result, as follows:
- Use the getPrimaryResults() method to get the primary results table
- the next() method to read the first row
- the getString() method to get the value of the first column
The complete code should look like this:
using Kusto.Data;
using Kusto.Data.Net.Client;
namespace HelloKusto {
class HelloKusto {
static void Main(string[] args) {
string clusterUri = "https://help.kusto.windows.net/";
var kcsb = new KustoConnectionStringBuilder(clusterUri).WithAadUserPromptAuthentication();
using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
string database = "Samples";
string query = "print Welcome='Hello Kusto!'";
using (var response = kustoClient.ExecuteQuery(database, query, null)) {
response.Read();
int columnNo = response.GetOrdinal("Welcome");
Console.WriteLine(response.GetString(columnNo));
}
}
}
}
}
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
def main():
cluster_uri = "https://help.kusto.windows.net"
kcsb = KustoConnectionStringBuilder.with_interactive_login(cluster_uri)
with KustoClient(kcsb) as kusto_client:
database = "Samples"
query = "print Welcome='Hello Kusto!'"
response = kusto_client.execute(database, query)
print(response.primary_results[0][0]["Welcome"])
if __name__ == "__main__":
main()
import { Client as KustoClient, KustoConnectionStringBuilder } from "azure-kusto-data/";
import { InteractiveBrowserCredentialInBrowserOptions } from "@azure/identity";
async function main()
{
const clusterUri = "https://help.kusto.windows.net";
const authOptions = {
clientId: "00001111-aaaa-2222-bbbb-3333cccc4444",
redirectUri: "http://localhost:5173",
} as InteractiveBrowserCredentialInBrowserOptions;
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri, authOptions);
const kustoClient = new KustoClient(kcsb);
const database = "Samples";
const query = "print Welcome='Hello Kusto!'";
const response = await kustoClient.execute(database, query);
console.log(response.primaryResults[0][0]["Welcome"].toString());
}
main();
Note
For Node.js apps, use InteractiveBrowserCredentialNodeOptions
instead of InteractiveBrowserCredentialInBrowserOptions
.
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.ClientFactory;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.KustoResultSetTable;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
public class HelloKusto {
public static void main(String[] args) throws Exception {
try {
String clusterUri = "https://help.kusto.windows.net/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
try (Client kustoClient = ClientFactory.createClient(kcsb)) {
String database = "Samples";
String query = "print Welcome='Hello Kusto!'";
KustoOperationResult response = kustoClient.execute(database, query);
KustoResultSetTable primaryResults = response.getPrimaryResults();
primaryResults.next();
System.out.println(primaryResults.getString("Welcome"));
}
}
}
}
Run your app
In a command shell, use the following command to run your app:
# Change directory to the folder that contains the hello world project
dotnet run .
In a Node.js environment:
node hello-kusto.js
In a browser environment, use the appropriate command to run your app. For example, for Vite-React:
npm run dev
mvn install exec:java -Dexec.mainClass="<groupId>.HelloKusto"
You should see a result similar to the following:
Hello Kusto!
Next step