How to use the console consumer to read non-string primitive keys and values

Question:

How do you specify key and value deserializers when running the Kafka console consumer?

Edit this page

Example use case:

You want to inspect or debug records written to a topic. Each record key and value is a long and double, respectively. In this tutorial, you'll learn how to specify key and value deserializers with the console consumer.

Hands-on code example:

Run it

Prerequisites

1

This tutorial installs Confluent Platform using Docker. Before proceeding:

  • • Install Docker Desktop (version 4.0.0 or later) or Docker Engine (version 19.03.0 or later) if you don’t already have it

  • • Install the Docker Compose plugin if you don’t already have it. This isn’t necessary if you have Docker Desktop since it includes Docker Compose.

  • • Start Docker if it’s not already running, either by starting Docker Desktop or, if you manage Docker Engine with systemd, via systemctl

  • • Verify that Docker is set up properly by ensuring no errors are output when you run docker info and docker compose version on the command line

Initialize the project

2

To get started, make a new directory anywhere you’d like for this project:

mkdir console-consumer-primitive-keys-values && cd console-consumer-primitive-keys-values

Get Confluent Platform

3

Next, create the following docker-compose.yml file to obtain Confluent Platform (for Kafka in the cloud, see Confluent Cloud).

---
version: '2'

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:7.3.0
    hostname: zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
  broker:
    image: confluentinc/cp-kafka:7.3.0
    hostname: broker
    container_name: broker
    depends_on:
      - zookeeper
    ports:
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0

  schema-registry:
    image: confluentinc/cp-schema-registry:7.3.0
    hostname: schema-registry
    container_name: schema-registry
    depends_on:
      - broker
    ports:
      - "8081:8081"
    environment:
      SCHEMA_REGISTRY_HOST_NAME: schema-registry
      SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:9092'

  ksqldb-server:
    image: confluentinc/cp-ksqldb-server:7.3.0
    hostname: ksqldb
    container_name: ksqldb
    depends_on:
      - broker
    ports:
      - "8088:8088"
    environment:
      KSQL_LISTENERS: http://0.0.0.0:8088
      KSQL_BOOTSTRAP_SERVERS: broker:9092
      KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
      KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
      KSQL_KSQL_SCHEMA_REGISTRY_URL: http://schema-registry:8081
      KSQL_KSQL_HIDDEN_TOPICS: '^_.*'
      # Setting KSQL_KSQL_CONNECT_WORKER_CONFIG enables embedded Kafka Connect
      KSQL_KSQL_CONNECT_WORKER_CONFIG: "/connect/connect.properties"
      # Kafka Connect config below
      KSQL_CONNECT_BOOTSTRAP_SERVERS: "broker:9092"
      KSQL_CONNECT_REST_ADVERTISED_HOST_NAME: 'ksqldb'
      KSQL_CONNECT_GROUP_ID: ksqldb-kafka-connect-group-01
      KSQL_CONNECT_CONFIG_STORAGE_TOPIC: _ksqldb-kafka-connect-group-01-configs
      KSQL_CONNECT_OFFSET_STORAGE_TOPIC: _ksqldb-kafka-connect-group-01-offsets
      KSQL_CONNECT_STATUS_STORAGE_TOPIC: _ksqldb-kafka-connect-group-01-status
      KSQL_CONNECT_KEY_CONVERTER: org.apache.kafka.connect.converters.LongConverter
      KSQL_CONNECT_VALUE_CONVERTER: org.apache.kafka.connect.converters.DoubleConverter
      KSQL_CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: '1'
      KSQL_CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: '1'
      KSQL_CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: '1'
      KSQL_CONNECT_LOG4J_APPENDER_STDOUT_LAYOUT_CONVERSIONPATTERN: "[%d] %p %X{connector.context}%m (%c:%L)%n"
      KSQL_CONNECT_PLUGIN_PATH: '/usr/share/java,/home/appuser/confluent-hub-components/,/data/connect-jars'

    command:
      # In the command section, $ are replaced with $$ to avoid the error 'Invalid interpolation format for "command" option'
      - bash
      - -c
      - |
        echo "Installing connector plugins"
        mkdir -p /home/appuser/confluent-hub-components/
        confluent-hub install --no-prompt --component-dir /home/appuser/confluent-hub-components/ --worker-configs /dev/null mdrogalis/voluble:0.3.0
        #
        echo "Launching ksqlDB"
        /etc/confluent/docker/run &

         echo "Waiting for Kafka Connect to start listening on localhost ⏳"
        while : ; do
          curl_status=$$(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors)
          echo -e $$(date) " Kafka Connect listener HTTP state: " $$curl_status " (waiting for 200)"
          if [ $$curl_status -eq 200 ] ; then
            break
          fi
          sleep 5
        done

        echo -e "\n--\n+> Creating Data Generator source"
        curl -X PUT http://localhost:8083/connectors/example/config \
         -i -H "Content-Type: application/json" -d'{
            "connector.class": "io.mdrogalis.voluble.VolubleSourceConnector",
            "genkp.example.with" : "#{Number.randomNumber}",
            "genvp.example.with" : "#{Address.latitude}",
            "topic.example.records.exactly" : 10,
            "transforms": "CastLong,CastDouble",
            "transforms.CastLong.type": "org.apache.kafka.connect.transforms.Cast$$Key",
            "transforms.CastLong.spec": "int64",
            "transforms.CastDouble.type": "org.apache.kafka.connect.transforms.Cast$$Value",
            "transforms.CastDouble.spec": "float64",
            "key.converter": "org.apache.kafka.connect.converters.LongConverter",
            "key.converter.schemas.enable" : "false",
            "value.converter": "org.apache.kafka.connect.converters.DoubleConverter",
            "value.converter.schemas.enable" : "false",
            "tasks.max": 1
        }'

        sleep infinity

Currently, the console producer only writes strings into Kafka, but we want to work with non-string primitives and the console consumer. So in this tutorial, your docker-compose.yml file will also create a source connector embedded in ksqldb-server to populate a topic with keys of type long and values of type double.

And launch it by running:

docker compose up -d

After you’ve run the docker compose up -d command, wait 30 seconds to a 1 minute before executing the next step.

Start an initial console consumer

4

Now you’ll use a topic created in the previous step. Your focus here is reading values on the command line with the console consumer. The records have the format of key = Long and value = Double.

First let’s open a new terminal window and start a shell in the broker container:

docker exec -it broker bash

Now let’s start up a console consumer to read some records. Run this command in the container shell:

kafka-console-consumer --topic example --bootstrap-server broker:9092 \
 --from-beginning \
 --property print.key=true \
 --property key.separator=" : "

After the consumer starts up, you’ll get some output, but nothing readable is on the screen. You should see something similar to this:

!? : @'?u_?mY
J? : ?(?,???
?c : @T?????
?? : @S{??ދ
?? : @F!?u??
? : ??{??%??
#f : @S??
?A
 : ?T5Ni?^?
 : ?κ?e
 : @>ֈ&???
 

The output looks like this because you are consuming records with a Long key and a Double value, but you haven’t provided the correct deserializer for longs or doubles.

Close the consumer with a Ctrl+C command, but keep the container shell open.

Specify key and value deserializers

5

Now let’s update your command to the console consumer to specify the deserializer for keys and values.

In the same window of your previous console consumer run this updated command in the container shell:

kafka-console-consumer --topic example --bootstrap-server broker:9092 \
 --from-beginning \
 --property print.key=true \
 --property key.separator=" : " \
 --key-deserializer "org.apache.kafka.common.serialization.LongDeserializer" \
 --value-deserializer "org.apache.kafka.common.serialization.DoubleDeserializer"

After the consumer starts you should see readable numbers similar to this:

8666 : 11.914958
19146 : -12.034799
34659 : 83.75128
310944 : 76.023163
302796 : 44.264754
374486 : 1.0302151
69428755 : 79.296206
4 : -80.832911
4 : -2.2259418
7 : 30.838015
Processed a total of 10 messages

Now you know how to configure a console consumer to handle primitive types - Double, Long, Float, Integer and Short.

Strings are the default value so you don’t have to specify a deserializer for those.

Clean up

6

You’re all done now!

Go back to your open windows and stop any console consumers with a CTRL+C then close the container shells with a Ctrl+D command. Then you can shut down the docker container by running:

docker compose down

Deploy on Confluent Cloud

Run your app to Confluent Cloud

1

Instead of running a local Kafka cluster, you may use Confluent Cloud, a fully-managed Apache Kafka service.

  1. Sign up for Confluent Cloud, a fully-managed Apache Kafka service.

  2. After you log in to Confluent Cloud Console, click on Add cloud environment and name the environment learn-kafka. Using a new environment keeps your learning resources separate from your other Confluent Cloud resources.

  3. From the Billing & payment section in the Menu, apply the promo code CC100KTS to receive an additional $100 free usage on Confluent Cloud (details).

  4. Click on LEARN and follow the instructions to launch a Kafka cluster and to enable Schema Registry.

Confluent Cloud

Next, from the Confluent Cloud Console, click on Clients to get the cluster-specific configurations, e.g. Kafka cluster bootstrap servers and credentials, Confluent Cloud Schema Registry and credentials, etc., and set the appropriate parameters in your client application.

Now you’re all set to run your streaming application locally, backed by a Kafka cluster fully managed by Confluent Cloud.