I have a spring boot application that is connected to a kafka cluster. How can I run KSQL from java code?
Asked
Active
Viewed 4,951 times
2 Answers
6
At the moment, there is no direct way to use KSQL as a library in java. There is an open issue#734 for the same.
But you can run the KSQL statement using REST API and that implementation can be done in Spring Boot Application. A Rest call would look something like this:
POST /query HTTP/1.1
Accept: application/vnd.ksql.v1+json
Content-Type: application/vnd.ksql.v1+json
{
"ksql": "SELECT * FROM pageviews;",
"streamsProperties": {
"ksql.streams.auto.offset.reset": "earliest"
}
}
// Through Curl
curl -X "POST" "http://localhost:8088/ksql" \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d $'{
"ksql": "LIST STREAMS;",
"streamsProperties": {}
}'
You can find the documentation here :
https://docs.confluent.io/current/ksql/docs/developer-guide/api.html#rest-endpoint

Nishu Tayal
- 20,106
- 8
- 49
- 101
-
thanks. Is this an endpoint of a kafka broker? Or there should be a separate ksql java server running? – Sergei Ledvanov Jan 10 '19 at 02:32
-
This is the KSQL Server listener. Here you can see the details : https://docs.confluent.io/current/ksql/docs/installation/installing.html#starting-the-ksql-server – Nishu Tayal Jan 10 '19 at 09:34
-
How does the "listening" to messages work under a REST API? If it is Websockets it would make sense, but I see something is missing here, given the whole idea of a messaging system being Async, the call-backs are key, and REST would time-out while waiting. Isn't it? Or perhaps REST supports only for a portion of the functionality and the other part is covered by the Java client library? – SydMK Jul 03 '23 at 23:43
2
Since ksqlDB 0.10 there is now a Java client: https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-clients/java-client/

Robin Moffatt
- 30,382
- 3
- 65
- 92
-
2
-
1There're examples in the product docs I linked - I'm not a Java coder so I would rather people used those than copy & paste something that I may get wrong :) – Robin Moffatt Dec 01 '20 at 13:58