-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathAdvancedQuery.java
82 lines (69 loc) · 3.47 KB
/
AdvancedQuery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import com.microsoft.azure.kusto.data.ClientFactory;
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.ClientRequestProperties;
import com.microsoft.azure.kusto.data.http.HttpClientProperties;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.KustoResultSetTable;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class AdvancedQuery {
public static void main(String[] args) {
try {
ConnectionStringBuilder csb = ConnectionStringBuilder.createWithAadApplicationCredentials(
System.getProperty("clusterPath"),
System.getProperty("appId"),
System.getProperty("appKey"),
System.getProperty("appTenant"));
HttpClientProperties properties = HttpClientProperties.builder()
.keepAlive(true)
.maxKeepAliveTime(120)
.maxConnectionsTotal(40)
.build();
Client client = ClientFactory.createClient(csb, properties);
String database = System.getProperty("dbName");
String newLine = System.getProperty("line.separator");
// Create a table named Events with 100 rows of data.
String tableCommand = String.join(newLine,
".set-or-replace Events <|",
"range x from 1 to 100 step 1",
"| extend ts = totimespan(strcat(x,'.00:00:00'))",
"| project timestamp = now(ts), eventName = strcat('event ', x)");
client.executeMgmt(database, tableCommand);
// Query for an event where the name is "event 1".
// Utilize ClientRequestProperties to pass query parameters to protect against injection attacks.
// We should expect to receive 1 row based on the data we created above.
ClientRequestProperties clientRequestProperties = new ClientRequestProperties();
clientRequestProperties.setParameter("eventNameFilter", "event 1");
String query = String.join(newLine,
"declare query_parameters(eventNameFilter:string);",
"Events",
"| where eventName == eventNameFilter");
KustoOperationResult results = client.executeQuery(database, query, clientRequestProperties);
KustoResultSetTable mainTableResult = results.getPrimaryResults();
System.out.printf("Kusto sent back %s rows.%n", mainTableResult.count());
// Iterate values
List<Event> events = new ArrayList<>();
while (mainTableResult.next()) {
events.add(new Event(mainTableResult.getKustoDateTime("timestamp"), mainTableResult.getString("eventName")));
}
System.out.println(events.get(0).toString());
} catch (Exception e) {
e.printStackTrace();
}
}
private static class Event {
LocalDateTime _timestamp;
String _eventName;
public Event(LocalDateTime timestamp, String eventName) {
_timestamp = timestamp;
_eventName = eventName;
}
public String toString() {
return String.format("Timestamp: %s, Event Name: %s", _timestamp, _eventName);
}
}
}