3

I have a spring boot application that is connected to a kafka cluster. How can I run KSQL from java code?

Matthias J. Sax
  • 59,682
  • 7
  • 117
  • 137
Sergei Ledvanov
  • 2,131
  • 6
  • 29
  • 52

2 Answers2

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