-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (56 loc) · 2.07 KB
/
Copy pathProgram.cs
File metadata and controls
66 lines (56 loc) · 2.07 KB
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
// Streamline SQL Query Example
//
// Demonstrates using Streamline's embedded analytics engine (DuckDB)
// to run SQL queries on streaming data.
//
// Prerequisites:
// - Streamline server running
// - dotnet add package Streamline.Client
//
// Run:
// dotnet run
using Streamline.Client;
using Streamline.Client.Query;
var bootstrap = Environment.GetEnvironmentVariable("STREAMLINE_BOOTSTRAP") ?? "localhost:9092";
var httpUrl = Environment.GetEnvironmentVariable("STREAMLINE_HTTP") ?? "http://localhost:9094";
// Produce sample data
await using var client = new StreamlineClient(new StreamlineOptions { BootstrapServers = bootstrap });
var admin = client.CreateAdmin(httpUrl);
await admin.CreateTopicAsync("events", partitions: 1);
for (int i = 0; i < 10; i++)
{
await client.ProduceAsync("events", $"key-{i}",
$$"""{"user":"user-{{i}}","action":"click","value":{{i * 10}}}""");
}
Console.WriteLine("Produced 10 events");
// Query the data
using var queryClient = new QueryClient(httpUrl);
// Simple SELECT
Console.WriteLine("\n--- All events (limit 5) ---");
var result = await queryClient.QueryAsync("SELECT * FROM topic('events') LIMIT 5");
Console.WriteLine($"Columns: {result.Columns.Length}, Rows: {result.Rows.Length}");
foreach (var row in result.Rows)
{
Console.WriteLine($" [{string.Join(", ", row)}]");
}
// Aggregation
Console.WriteLine("\n--- Count by action ---");
result = await queryClient.QueryAsync(
"SELECT action, COUNT(*) as cnt FROM topic('events') GROUP BY action");
foreach (var row in result.Rows)
{
Console.WriteLine($" [{string.Join(", ", row)}]");
}
// Query with options
Console.WriteLine("\n--- With custom timeout and limit ---");
result = await queryClient.QueryAsync(
"SELECT * FROM topic('events') ORDER BY offset DESC",
timeoutMs: 5000,
maxRows: 3);
Console.WriteLine($"Returned {result.Rows.Length} rows");
// Explain query plan
Console.WriteLine("\n--- Query plan ---");
var plan = await queryClient.ExplainAsync(
"SELECT * FROM topic('events') WHERE value > 50");
Console.WriteLine(plan);
await admin.DisposeAsync();