-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-usage.ts
More file actions
60 lines (52 loc) · 1.69 KB
/
Copy pathquery-usage.ts
File metadata and controls
60 lines (52 loc) · 1.69 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
/**
* Streamline SQL Query Example
*
* Demonstrates using Streamline's embedded analytics engine (DuckDB)
* to run SQL queries on streaming data.
*
* Prerequisites:
* - Streamline server running
* - npm install streamline-client
*
* Run:
* npx tsx examples/query-usage.ts
*/
import { Streamline } from '../src';
async function main() {
const bootstrap = process.env.STREAMLINE_BOOTSTRAP ?? 'localhost:9092';
const httpUrl = process.env.STREAMLINE_HTTP ?? 'http://localhost:9094';
const client = new Streamline(bootstrap, { httpEndpoint: httpUrl });
await client.connect();
// Produce sample data
await client.createTopic('events', { partitions: 1 });
for (let i = 0; i < 10; i++) {
await client.produce('events', {
user: `user-${i}`,
action: 'click',
value: i * 10,
});
}
console.log('Produced 10 events');
// Simple SELECT
console.log('\n--- All events (limit 5) ---');
const rows = await client.query("SELECT * FROM topic('events') LIMIT 5");
console.log(`Rows: ${rows.length}`);
for (const row of rows) {
console.log(' ', row);
}
// Full query with metadata
console.log('\n--- Count by action ---');
const result = await client.queryFull(
"SELECT action, COUNT(*) as cnt FROM topic('events') GROUP BY action",
);
console.log(`Columns: ${result.columns?.length}, Rows: ${result.rows?.length}`);
// Query with options
console.log('\n--- With custom timeout and limit ---');
const limited = await client.queryFull(
"SELECT * FROM topic('events') ORDER BY offset DESC",
{ timeoutMs: 5000, maxRows: 3 },
);
console.log(`Returned ${limited.rows?.length} rows`);
await client.close();
}
main().catch(console.error);