-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicUsage.kt
More file actions
71 lines (59 loc) · 2.03 KB
/
Copy pathBasicUsage.kt
File metadata and controls
71 lines (59 loc) · 2.03 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
67
68
69
70
71
/**
* Basic Streamline Kotlin SDK usage example.
*
* Prerequisites:
* 1. Start a Streamline server: streamline --playground
* 2. Run this example: gradle run (or copy into your project)
*
* Demonstrates: connecting, producing, consuming, admin operations,
* and SQL queries.
*/
package io.streamline.examples
import io.streamline.sdk.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.take
suspend fun main() {
// -- Configuration --
val config = StreamlineConfiguration(
url = "ws://localhost:9092",
autoReconnect = true,
maxRetries = 5,
)
// -- Admin Client (HTTP API) --
val admin = AdminClient("http://localhost:9094")
// Create a topic
admin.createTopic("kotlin-demo", partitions = 3)
println("✓ Created topic 'kotlin-demo'")
// List topics
val topics = admin.listTopics()
println("Topics: ${topics.map { it.name }}")
// -- Streaming Client (WebSocket) --
val client = StreamlineClient(config)
client.connect()
println("✓ Connected to Streamline")
// Produce messages
for (i in 1..5) {
client.produce("kotlin-demo", key = "user-$i", value = """{"event":"click","count":$i}""")
}
println("✓ Produced 5 messages")
// Consume messages using Flow
println("Consuming messages:")
client.messages("kotlin-demo").take(5).collect { msg ->
println(" topic=${msg.topic} key=${msg.key} value=${msg.value}")
}
// -- SQL Query --
val result = admin.query("SELECT * FROM `kotlin-demo` LIMIT 3")
println("Query result: ${result.rowCount} rows, columns: ${result.columns}")
// -- Consumer Groups --
val groups = admin.listConsumerGroups()
println("Consumer groups: ${groups.map { it.id }}")
// -- Server Info --
val info = admin.serverInfo()
println("Server: v${info.version}, uptime=${info.uptime}s, topics=${info.topicCount}")
// Cleanup
admin.deleteTopic("kotlin-demo")
client.disconnect()
client.close()
admin.close()
println("✓ Done")
}