1

I'm developing a simple Java app, which tries to pull data from ksqldb using Java client.

Below is the code, but StreamedQueryResult streamedQueryResult = client.streamQuery(pullQuery).get(); forever going in block state.

public class ExampleApp {

    public static String KSQLDB_SERVER_HOST = "0.0.0.0";
    public static int KSQLDB_SERVER_HOST_PORT = 8088;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Map<String, Object> properties = Collections.singletonMap(
                "auto.offset.reset", "earliest"
        );

        ClientOptions options = ClientOptions.create()
                .setHost(KSQLDB_SERVER_HOST)
                .setPort(KSQLDB_SERVER_HOST_PORT)
                .setUseTls(false)
                .setUseAlpn(true);
        Client client = Client.create(options);

        String pullQuery = "select name, countrycode from USERS_STREAM emit changes;";

        StreamedQueryResult streamedQueryResult = client.streamQuery(pullQuery).get();
        Row row = streamedQueryResult.poll();
        System.out.println("## "+ row.values());
    }
}

I've created topic:

kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic USERS
Created topic USERS.

kafka-console-producer --bootstrap-server localhost:9092 --topic USERS
>Alice,US
>John,SL
>Prateek,IND
>Mike,GB
>Zoran,GB
>Rani,Ind
>Vlad,PL
>

Also, I created users_stream, hope this is good setup to start the code.

ksql> create stream users_stream (name VARCHAR, countrycode VARCHAR) WITH (KAFKA_TOPIC='USERS', VALUE_FORMAT='DELIMITED');

 Message        
----------------
 Stream created 
----------------
ksql> select name, countrycode from USERS_STREAM emit changes;
+-----------------------------------------------------------------+-----------------------------------------------------------------+
|NAME                                                             |COUNTRYCODE                                                      |
+-----------------------------------------------------------------+-----------------------------------------------------------------+
|Prateek                                                          |IND                                                              |
|Mike                                                             |GB                                                               |
|Zoran                                                            |GB                                                               |
|Rani                                                             |Ind                                                              |
^CQuery terminated

I used confluent platform setup locally to start the confluent kafka

confluent local services start
The local commands are intended for a single-node development environment only,
NOT for production usage. https://docs.confluent.io/current/cli/index.html

Using CONFLUENT_CURRENT: /var/folders/kn/4wr9__651l37hckxvnnwt4hh0000gn/T/confluent.439906
Starting ZooKeeper
ZooKeeper is [UP]
Starting Kafka
Kafka is [UP]
Starting Schema Registry
Schema Registry is [UP]
Starting Kafka REST
Kafka REST is [UP]
Starting Connect
Connect is [UP]
Starting ksqlDB Server
ksqlDB Server is [UP]
Starting Control Center
Control Center is [UP]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186

1 Answers1

1

I was able to fixed the issue. A detailed outlined here: https://medium.com/@prateek-ashtikar512/java-and-ksqldb-example-61aae5aee14b

public class ExampleApp {

    public static String KSQLDB_SERVER_HOST = "0.0.0.0";
    public static int KSQLDB_SERVER_HOST_PORT = 8088;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Map<String, Object> properties = Collections.singletonMap(
                "auto.offset.reset", "earliest"
        );

        ClientOptions options = ClientOptions.create()
                .setHost(KSQLDB_SERVER_HOST)
                .setPort(KSQLDB_SERVER_HOST_PORT)
                .setUseTls(false)
                .setUseAlpn(true);
        Client client = Client.create(options);

        String pullQuery = "select name, countrycode from USERS_STREAM emit changes;";

        StreamedQueryResult streamedQueryResult = client.streamQuery(pullQuery, properties).get();

        for (int i = 0; i < 10; i++) {
            // Block until a new row is available
            Row row = streamedQueryResult.poll();
            if (row != null) {
                System.out.println("Received a row!");
                System.out.println("Row: " + row.values());
            } else {
                System.out.println("Query has ended.");
            }
        }
    }
}
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186