创建一个应用来运行基本查询
本文内容
适用于:✅Azure 数据资源管理器
在本文中,学习如何:
先决条件
设置开发环境 以使用 Kusto 客户端库。
运行基本查询并处理结果
在首选 IDE 或文本编辑器中,使用适合首选语言的约定创建名为“基本查询 ”的项目或文件。 然后,添加以下代码:
创建连接到帮助群集 的客户端应用。
using Kusto.Data;
using Kusto.Data.Net.Client;
namespace BasicQuery {
class BasicQuery {
static void Main(string[] args) {
var clusterUri = "https://help.chinaeast2.kusto.chinacloudapi.cn/";
var kcsb = new KustoConnectionStringBuilder(clusterUri)
.WithAadUserPromptAuthentication();
using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
}
}
}
}
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
def main():
cluster_uri = "https://help.chinaeast2.kusto.chinacloudapi.cn"
kcsb = KustoConnectionStringBuilder.with_interactive_login(cluster_uri)
with KustoClient(kcsb) as kusto_client:
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.chinaeast2.kusto.chinacloudapi.cn";
const authOptions = {
clientId: "5e39af3b-ba50-4255-b547-81abfb507c58",
redirectUri: "http://localhost:5173",
} as InteractiveBrowserCredentialInBrowserOptions;
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri, authOptions);
const kustoClient = new KustoClient(kcsb);
}
main();
注意
对于 Node.js 应用,请使用 InteractiveBrowserCredentialNodeOptions
而不是 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 BasicQuery {
public static void main(String[] args) throws Exception {
try {
String clusterUri = "https://help.chinaeast2.kusto.chinacloudapi.cn/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
try (Client kustoClient = ClientFactory.createClient(kcsb)) {
}
}
}
}
定义要运行的数据库和查询。 该查询返回导致总损失超过 1 亿美元的龙卷风的发生日期、州和总损失。
var database = "Samples";
var query = @"StormEvents
| where EventType == 'Tornado'
| extend TotalDamage = DamageProperty + DamageCrops
| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)
| where DailyDamage > 100000000
| order by DailyDamage desc";
database = "Samples"
query = "StormEvents" \
"| where EventType == 'Tornado'" \
"| extend TotalDamage = DamageProperty + DamageCrops" \
"| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)" \
"| where DailyDamage > 100000000" \
"| order by DailyDamage desc"
const database = "Samples";
const query = `StormEvents
| where EventType == 'Tornado'
| extend TotalDamage = DamageProperty + DamageCrops
| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)
| where DailyDamage > 100000000
| order by DailyDamage desc`;
String database = "Samples";
String query = "StormEvents\n" +
"| where EventType == 'Tornado'\n" +
"| extend TotalDamage = DamageProperty + DamageCrops\n" +
"| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)\n" +
"| where DailyDamage > 100000000\n" +
"| order by DailyDamage desc";
运行查询并输出结果。
using (var response = kustoClient.ExecuteQuery(database, query, null)) {
int columnNoStartTime = response.GetOrdinal("StartTime");
int columnNoState = response.GetOrdinal("State");
int columnNoDailyDamage = response.GetOrdinal("DailyDamage");
Console.WriteLine("Daily tornado damages over 100,000,000$:");
while (response.Read()) {
Console.WriteLine("{0} - {1}, {2}",
response.GetDateTime(columnNoStartTime),
response.GetString(columnNoState),
response.GetInt64(columnNoDailyDamage));
}
}
response = kusto_client.execute(database, query)
print("Daily tornado damages over 100,000,000$:")
for row in response.primary_results[0]:
print(row["StartTime"], "-", row["State"], ",", row["DailyDamage"], "$")
const response = await kustoClient.execute(database, query);
console.log("Daily tornado damages over 100,000,000$:");
for (row of response.primaryResults[0].rows()) {
console.log(row["StartTime"].toString(), "-", row["State"].toString(), ",", row["DailyDamage"].toString(), "$");
}
KustoOperationResult response = kusto_client.execute(database, query);
KustoResultSetTable primaryResults = response.getPrimaryResults();
System.out.println("Daily tornado damages over 100,000,000$:");
while (primaryResults.next()) {
System.out.println(primaryResults.getString("StartTime") + " - " + primaryResults.getString("State") + " , " + primaryResults.getString("DailyDamage"));
}
完整代码应如下所示:
using Kusto.Data;
using Kusto.Data.Net.Client;
namespace BasicQuery {
class BasicQuery {
static void Main(string[] args) {
string clusterUri = "https://help.chinaeast2.kusto.chinacloudapi.cn/";
var kcsb = new KustoConnectionStringBuilder(clusterUri)
.WithAadUserPromptAuthentication();
using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
string database = "Samples";
string query = @"StormEvents
| where EventType == 'Tornado'
| extend TotalDamage = DamageProperty + DamageCrops
| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)
| where DailyDamage > 100000000
| order by DailyDamage desc";
using (var response = kustoClient.ExecuteQuery(database, query, null)) {
int columnNoStartTime = response.GetOrdinal("StartTime");
int columnNoState = response.GetOrdinal("State");
int columnNoDailyDamage = response.GetOrdinal("DailyDamage");
Console.WriteLine("Daily tornado damages over 100,000,000$:");
while (response.Read()) {
Console.WriteLine("{0} - {1}, {2}",
response.GetDateTime(columnNoStartTime),
response.GetString(columnNoState),
response.GetInt64(columnNoDailyDamage));
}
}
}
}
}
}
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
def main():
cluster_uri = "https://help.chinaeast2.kusto.chinacloudapi.cn"
kcsb = KustoConnectionStringBuilder.with_interactive_login(cluster_uri)
with KustoClient(kcsb) as kusto_client:
database = "Samples"
query = "StormEvents" \
"| where EventType == 'Tornado'" \
"| extend TotalDamage = DamageProperty + DamageCrops" \
"| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)" \
"| where DailyDamage > 100000000" \
"| order by DailyDamage desc"
response = kusto_client.execute(database, query)
print("Daily tornado damages over 100,000,000$:")
for row in response.primary_results[0]:
print(row["StartTime"], "-", row["State"], ",", row["DailyDamage"], "$")
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.chinaeast2.kusto.chinacloudapi.cn";
const authOptions = {
clientId: "5e39af3b-ba50-4255-b547-81abfb507c58",
redirectUri: "http://localhost:5173",
} as InteractiveBrowserCredentialInBrowserOptions;
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri, authOptions);
const kustoClient = new KustoClient(kcsb);
const database = "Samples";
const query = `StormEvents
| where EventType == 'Tornado'
| extend TotalDamage = DamageProperty + DamageCrops
| where DailyDamage > 100000000
| order by DailyDamage desc`;
const response = await kustoClient.execute(database, query);
console.log("Daily tornado damages over 100,000,000$:");
for (row of response.primaryResults[0].rows()) {
console.log(row["StartTime"].toString(), "-", row["State"].toString(), ",", row["DailyDamage"].toString(), "$");
}
}
main();
注意
对于 Node.js 应用,请使用 InteractiveBrowserCredentialNodeOptions
而不是 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 BasicQuery {
public static void main(String[] args) throws Exception {
try {
String clusterUri = "https://help.chinaeast2.kusto.chinacloudapi.cn/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
try (Client kustoClient = ClientFactory.createClient(kcsb)) {
String database = "Samples";
String query = "StormEvents\n" +
"| where EventType == 'Tornado'\n" +
"| extend TotalDamage = DamageProperty + DamageCrops\n" +
"| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)\n" +
"| where DailyDamage > 100000000\n" +
"| order by DailyDamage desc";
KustoOperationResult response = kustoClient.execute(database, query);
KustoResultSetTable primaryResults = response.getPrimaryResults();
System.out.println("Daily tornado damages over 100,000,000$:");
while (primaryResults.next()) {
System.out.println(primaryResults.getString("StartTime") + " - " + primaryResults.getString("State") + " , " + primaryResults.getString("DailyDamage"));
}
}
}
}
}
运行应用
在命令行界面中,使用以下命令运行应用:
# Change directory to the folder that contains the basic queries project
dotnet run .
在 Node.js 环境中:
node basic-query.js
在浏览器环境中,使用适当的命令来运行应用。 例如,对于 Vite-React,请使用以下命令:
npm run dev
mvn install exec:java -Dexec.mainClass="<groupId>.BasicQuery"
你应会看到如下所示的结果:
Daily damages tornado with damages over 100,000,000$:
2007-02-02 00:00:00+00:00 - FLORIDA , 270004000 $
2007-03-01 00:00:00+00:00 - ALABAMA , 266853000 $
2007-05-04 00:00:00+00:00 - KANSAS , 251625000 $
2007-03-01 00:00:00+00:00 - GEORGIA , 143688000 $
使用序号位置访问列值
当查询结果中的列顺序已知时,按列在结果集中的序号位置访问列值比按列名访问列值更为有效。 (可选)在运行时,可以使用库方法根据列名确定列序号。
例如,可以修改上面的代码,以按结果集中的序号位置访问 StartTime
、State
和 DailyDamage
列的值:
在 C# 中,只能按列在结果集中的序号位置访问列值。 不能使用列名;因此代码保持不变。
int columnNoStartTime = response.GetOrdinal("StartTime");
int columnNoState = response.GetOrdinal("State");
int columnNoDailyDamage = response.GetOrdinal("DailyDamage");
Console.WriteLine("Daily tornado damages over 100,000,000$:");
while (response.Read()) {
Console.WriteLine("{0} - {1}, {2}",
response.GetDateTime(columnNoStartTime),
response.GetString(columnNoState),
response.GetInt64(columnNoDailyDamage));
}
state_col = 0
start_time_col = next(col.ordinal for col in response.primary_results[0].columns if col.column_name == "StartTime")
damage_col = 2
print("Daily damages over 100,000,000$:")
for row in response.primary_results[0]:
print(row[start_time_col], "-", row[state_col], ",", row[damage_col], "$")
const columnNoState = 0;
const columnNoStartTime = response.primaryResults[0].columns.find(c => c.name == "StartTime").ordinal;
const columnNoDailyDamage = 2;
console.log("Daily tornado damages over 100,000,000$:");
for (row of response.primaryResults[0].rows()) {
console.log(row.getValueAt(columnNoStartTime).toString(), "-", row.getValueAt(columnNoState).toString(), ",", row.getValueAt(columnNoDailyDamage).toString(), "$");
}
Integer columnNoState = 0;
Integer columnNoStartTime = primaryResults.findColumn("StartTime");
Integer columnNoDailyDamage = 2;
while (primaryResults.next()) {
System.out.println(primaryResults.getString(columnNoStartTime) + " - " + primaryResults.getString(columnNoState) + " , " + primaryResults.getString(columnNoDailyDamage));
}
使用客户端请求属性自定义查询行为
可以通过设置客户端请求属性来自定义查询的行为。 有关可用选项的详细信息,请参阅客户端请求属性 。
例如,可以替换上面代码中的 kusto_client.execute_query
调用以传递自定义请求 ID,并将查询超时设置为 1 分钟。 若要使用客户端请求属性,必须导入 ClientRequestProperties
类。
using Kusto.Data.Common;
var crp = new ClientRequestProperties();
// Set a custom client request identifier
crp.ClientRequestId = "QueryDemo" + Guid.NewGuid().ToString();
// Set the query timeout to 1 minute
crp.SetOption(ClientRequestProperties.OptionServerTimeout, "1m");
using (var response = kustoClient.ExecuteQuery(database, query, crp)) {
}
from azure.kusto.data import ClientRequestProperties
from datetime import datetime
import uuid;
crp = ClientRequestProperties()
# Set a custom client request identifier
crp.client_request_id = "QueryDemo" + str(uuid.uuid4())
# Set the query timeout to 1 minute
crp.set_option(crp.request_timeout_option_name, datetime.timedelta(minutes=1))
response = kusto_client.execute_query(database, query, crp)
import { ClientRequestProperties } from "azure-kusto-data";
import { v4 as uuidv4 } from "uuid";
const crp = new ClientRequestProperties();
// Set a custom client request identifier
crp.clientRequestId = "QueryDemo" + uuidv4();
// Set the query timeout to 1 minute
crp.setServerTimeout(1000 * 60);
const response = await kustoClient.execute(database, query, crp);
import com.microsoft.azure.kusto.data.ClientRequestProperties;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
ClientRequestProperties crp = new ClientRequestProperties();
// Set a custom client request identifier
crp.setClientRequestId("QueryDemo" + UUID.randomUUID());
// Set the query timeout to 1 minute
crp.setTimeoutInMilliSec(TimeUnit.MINUTES.toMillis(60));
KustoOperationResult response = kusto_client.execute(database, query, crp);
查询参数对于维持数据安全性和保护非常重要。 它们可以防范潜在的恶意行动者未经授权访问或损坏数据。 有关参数化查询的详细信息,请参阅查询参数声明语句 。
例如,可以修改上面的代码,以将 EventType 值和 DailyDamage 最小值作为参数传递给查询。 若要使用参数,请执行以下操作:
在查询文本中声明参数
将参数名称替换为查询文本中的属性值
在传递给 execute 方法的客户端请求属性中设置参数值
string query = @"declare query_parameters(event_type:string, daily_damage:int);
StormEvents
| where EventType == event_type
| extend TotalDamage = DamageProperty + DamageCrops
| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)
| where DailyDamage > daily_damage
| order by DailyDamage desc";
var crp = new ClientRequestProperties();
crp.SetParameter("event_type", "Flash Flood");
crp.SetParameter("daily_damage", 200000000.ToString());
using (var response = kustoClient.ExecuteQuery(database, query, crp)) {
int columnNoStartTime = response.GetOrdinal("StartTime");
int columnNoState = response.GetOrdinal("State");
int columnNoDailyDamage = response.GetOrdinal("DailyDamage");
Console.WriteLine("Daily flash flood damages over 200,000,000$:");
while (response.Read()) {
Console.WriteLine("{0} - {1}, {2}",
response.GetDateTime(columnNoStartTime),
response.GetString(columnNoState),
response.GetInt64(columnNoDailyDamage));
}
}
query = "declare query_parameters(event_type:string, daily_damage:int);"\
"StormEvents" \
"| where EventType == event_type" \
"| extend TotalDamages = DamageProperty + DamageCrops" \
"| summarize DailyDamage=sum(TotalDamages) by State, bin(StartTime, 1d)" \
"| where DailyDamage > daily_damage" \
"| order by DailyDamage desc"
crp = ClientRequestProperties()
crp.set_parameter("event_type", "Flash Flood")
crp.set_parameter("daily_damage", str(200000000))
response = kusto_client.execute_query(=database, query, crp)
print("Daily flash flood damages over 200,000,000$:")
for row in response.primary_results[0]:
print(row["StartTime"], "-", row["State"], ",", row["DailyDamage"], "$")
const query = `declare query_parameters(event_type:string, daily_damage:int);
StormEvents
| where EventType == event_type
| extend TotalDamage = DamageProperty + DamageCrops
| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)
| where DailyDamage > daily_damage
| order by DailyDamage desc`;
const crp = new ClientRequestProperties();
crp.setParameter("event_type", "Flash Flood");
crp.setParameter("daily_damage", 200000000);
const response = await kustoClient.execute(database, query, crp);
console.log("Daily flash flood damages over 200,000,000$:");
for (row of response.primaryResults[0].rows()) {
console.log(row.getValueAt(columnNoStartTime).toString(), "-", row.getValueAt(columnNoState).toString(), ",", row.getValueAt(columnNoDailyDamage).toString(), "$");
}
String query = "declare query_parameters(event_type:string, daily_damage:int);\n" +
"StormEvents\n" +
"| where EventType == event_type\n" +
"| extend TotalDamage = DamageProperty + DamageCrops\n" +
"| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)\n" +
"| where DailyDamage > daily_damage\n" +
"| order by DailyDamage desc";
ClientRequestProperties crp = new ClientRequestProperties();
crp.setParameter("event_type", "Flash Flood");
crp.setParameter("daily_damage", 200000000);
KustoOperationResult response = kusto_client.execute(database, query, crp);
KustoResultSetTable primary_results = response.getPrimaryResults();
System.out.println("Daily flash flood damages over 200,000,000$:");
while (primary_results.next()) {
System.out.println("DEBUG: " + primary_results.getString(columnNoStartTime) + " - " + primary_results.getString(columnNoState) + " , " + primary_results.getString(columnNoDailyDamage));
}
使用序号位置访问列值和参数的完整代码如下所示:
using Kusto.Data;
using Kusto.Data.Common;
using Kusto.Data.Net.Client;
namespace BasicQuery {
class BasicQuery {
static void Main(string[] args) {
string clusterUri = "https://help.chinaeast2.kusto.chinacloudapi.cn/";
var kcsb = new KustoConnectionStringBuilder(clusterUri)
.WithAadUserPromptAuthentication();
using (var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcsb)) {
string database = "Samples";
string query = @"declare query_parameters(event_type:string, daily_damage:int);
StormEvents
| where EventType == event_type
| extend TotalDamage = DamageProperty + DamageCrops
| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)
| where DailyDamage > daily_damage
| order by DailyDamage desc";
var crp = new ClientRequestProperties();
crp.ClientRequestId = "QueryDemo" + Guid.NewGuid().ToString();
crp.SetOption(ClientRequestProperties.OptionServerTimeout, "1m");
crp.SetParameter("event_type", "Flash Flood");
crp.SetParameter("daily_damage", 200000000.ToString());
using (var response = kustoClient.ExecuteQuery(database, query, crp)) {
int columnNoStartTime = response.GetOrdinal("StartTime");
int columnNoState = response.GetOrdinal("State");
int columnNoDailyDamage = response.GetOrdinal("DailyDamage");
Console.WriteLine("Daily flash flood damages over 200,000,000$:");
while (response.Read()) {
Console.WriteLine("{0} - {1}, {2}",
response.GetDateTime(columnNoStartTime),
response.GetString(columnNoState),
response.GetInt64(columnNoDailyDamage));
}
}
}
}
}
}
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder, ClientRequestProperties
from datetime import timedelta
import uuid;
def main():
cluster_uri = "https://help.chinaeast2.kusto.chinacloudapi.cn"
kcsb = KustoConnectionStringBuilder.with_interactive_login(cluster_uri)
crp = ClientRequestProperties()
crp.client_request_id = "QueryDemo" + str(uuid.uuid4())
crp.set_option(crp.request_timeout_option_name, timedelta(minutes=1))
crp.set_parameter("event_type", "Flash Flood")
crp.set_parameter("daily_damage", str(200000000))
with KustoClient(kcsb) as kusto_client:
database = "Samples"
query = "declare query_parameters(event_type:string, daily_damage:int);"\
"StormEvents" \
"| where EventType == event_type" \
"| extend TotalDamages = DamageProperty + DamageCrops" \
"| summarize DailyDamage=sum(TotalDamages) by State, bin(StartTime, 1d)" \
"| where DailyDamage > daily_damage" \
"| order by DailyDamage desc"
response = kusto_client.execute_query(database, query, crp)
state_col = 0
start_time_col = next(col.ordinal for col in response.primary_results[0].columns if col.column_name == "StartTime")
damage_col = 2
print("Daily flash flood damages over 200,000,000$:")
for row in response.primary_results[0]:
print(row[start_time_col], "-", row[state_col], ",", row[damage_col], "$")
if __name__ == "__main__":
main()
import {
Client as KustoClient,
KustoConnectionStringBuilder,
ClientRequestProperties
} from "azure-kusto-data";
import { v4 as uuidv4 } from "uuid";
async function main() {
const clusterUri = "https://help.chinaeast2.kusto.chinacloudapi.cn";
const kcsb = KustoConnectionStringBuilder.withUserPrompt(clusterUri);
const kustoClient = new KustoClient(kcsb);
const database = "Samples";
const query = `declare query_parameters(event_type:string, daily_damage:int);
StormEvents
| where EventType == event_type
| extend TotalDamage = DamageProperty + DamageCrops
| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)
| where DailyDamage > daily_damage
| order by DailyDamage desc`;
const crp = new ClientRequestProperties();
// Set a custom client request identifier
crp.clientRequestId = "QueryDemo" + uuidv4();
// Set the query timeout to 1 minute
crp.setTimeout(1000 * 60);
crp.setParameter("event_type", "Flash Flood");
crp.setParameter("daily_damage", 200000000);
const response = await kustoClient.execute(database, query, crp);
const columnNoState = 0;
const columnNoStartTime = response.primaryResults[0].columns.find(c => c.name == "StartTime").ordinal;
const columnNoDailyDamage = 2;
console.log("Daily flash flood damages over 200,000,000$:");
for (row of response.primaryResults[0].rows()) {
console.log(row.getValueAt(columnNoStartTime).toString(), "-", row.getValueAt(columnNoState).toString(), ",", row.getValueAt(columnNoDailyDamage).toString(), "$");
}
}
main();
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;
import com.microsoft.azure.kusto.data.ClientRequestProperties;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class BasicQuery {
public static void main(String[] args) throws Exception {
try {
String clusterUri = "https://help.chinaeast2.kusto.chinacloudapi.cn/";
ConnectionStringBuilder kcsb = ConnectionStringBuilder.createWithUserPrompt(clusterUri);
try (Client kustoClient = ClientFactory.createClient(kcsb)) {
String database = "Samples";
String query = "declare query_parameters(event_type:string, daily_damage:int);\n" +
"StormEvents\n" +
"| where EventType == event_type\n" +
"| extend TotalDamage = DamageProperty + DamageCrops\n" +
"| summarize DailyDamage=sum(TotalDamage) by State, bin(StartTime, 1d)\n" +
"| where DailyDamage > daily_damage\n" +
"| order by DailyDamage desc";
ClientRequestProperties crp = new ClientRequestProperties();
// Set a custom client request identifier
crp.setClientRequestId("QueryDemo" + UUID.randomUUID());
// Set the query timeout to 1 minute
crp.setTimeoutInMilliSec(TimeUnit.MINUTES.toMillis(60));
crp.setParameter("event_type", "Flash Flood");
crp.setParameter("daily_damage", 200000000);
KustoOperationResult response = kustoClient.execute(database, query, crp);
KustoResultSetTable primaryResults = response.getPrimaryResults();
Integer columnNoState = 0;
Integer columnNoStartTime = primaryResults.findColumn("StartTime");
Integer columnNoDailyDamage = 2;
System.out.println("Daily flash flood damages over 200,000,000$:");
while (primaryResults.next()) {
System.out.println("DEBUG: " + primaryResults.getString(columnNoStartTime) + " - " + primaryResults.getString(columnNoState) + " , " + primaryResults.getString(columnNoDailyDamage));
}
}
}
}
}
你应会看到如下所示的结果:
Daily flash flood damages over 200,000,000$:
2007-08-21 00:00:00+00:00 - OHIO , 253320000 $
下一步