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

Then create a schema file, primitives.json, in the console-consumer-primitive-keys-values directory so the DatagenConnector can generate the records needed for this tutorial:

{
  "type": "record",
  "name": "primitives",
  "namespace": "io.confluent.avro.random.generator",
  "fields":
    [
      { "name": "key_field", "type": "long" },
      { "name": "value_field", "type": "double" }
    ]
}

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:
  broker:
    image: confluentinc/cp-kafka:7.4.1
    hostname: broker
    container_name: broker
    ports:
    - 29092:29092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT,CONTROLLER:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_PROCESS_ROLES: broker,controller
      KAFKA_NODE_ID: 1
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@broker:29093
      KAFKA_LISTENERS: PLAINTEXT://broker:9092,CONTROLLER://broker:29093,PLAINTEXT_HOST://0.0.0.0:29092
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_LOG_DIRS: /tmp/kraft-combined-logs
      CLUSTER_ID: MkU3OEVBNTcwNTJENDM2Qk
  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: ^_.*
      KSQL_KSQL_CONNECT_WORKER_CONFIG: /connect/connect.properties
      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
    volumes:
    - ./primitives.json:/schema/primitives.json
    command:
    - bash
    - -c
    - "echo \"Installing connector plugins\"\nmkdir -p /home/appuser/confluent-hub-components/\nconfluent-hub
      install --no-prompt --component-dir /home/appuser/confluent-hub-components/
      --worker-configs /dev/null confluentinc/kafka-connect-datagen:0.6.0\n#\necho
      \"Launching ksqlDB\"\n/etc/confluent/docker/run &\n\n echo \"Waiting for Kafka
      Connect to start listening on localhost ⏳\"\nwhile : ; do\n  curl_status=$$(curl
      -s -o /dev/null -w %{http_code} http://localhost:8083/connectors)\n  echo -e
      $$(date) \" Kafka Connect listener HTTP state: \" $$curl_status \" (waiting
      for 200)\"\n  if [ $$curl_status -eq 200 ] ; then\n    break\n  fi\n  sleep
      5 \ndone\n\necho -e \"\\n--\\n+> Creating Data Generator source\"\ncurl -X PUT
      http://localhost:8083/connectors/example/config \\\n -i -H \"Content-Type: application/json\"
      -d'{\n    \"connector.class\": \"io.confluent.kafka.connect.datagen.DatagenConnector\",\n
      \   \"schema.filename\": \"/schema/primitives.json\",\n    \"schema.keyfield\":
      \"key_field\",\n    \"kafka.topic\" : \"example\",\n    \"iterations\" : 10,\n
      \   \"transforms\": \"extractValue\",\n    \"transforms.extractValue.type\":
      \"org.apache.kafka.connect.transforms.ExtractField$$Value\",\n    \"transforms.extractValue.field\":
      \"value_field\",\n    \"key.converter\": \"org.apache.kafka.connect.converters.LongConverter\",\n
      \   \"key.converter.schemas.enable\" : \"false\",\n    \"value.converter\":
      \"org.apache.kafka.connect.converters.DoubleConverter\",\n    \"value.converter.schemas.enable\"
      : \"false\",\n    \"tasks.max\": 1\n}'\n\nsleep infinity\n"

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 Ctrl-C, 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=" : " \
 --max-messages 10 \
 --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:

7000546592895906471 : 0.6322582597613734
-2815276123070627191 : 0.3391308127269156
-6910239382365155685 : 0.8764180508020841
-1108990429618029888 : 0.9034900159780727
-6051646600310729054 : 0.6484490061426863
-297855922771180036 : 0.6636920219701741
-2161765026709818701 : 0.6839825636436166
4577207098168959186 : 0.9256904333195043
7666900789317051211 : 0.26556161712153203
2627408892797753421 : 0.9715509777797698
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 Ctrl-C then close the container shells with Ctrl-D. Then you can shut down the docker container by running:

docker compose down

Deploy on Confluent Cloud

Run your app with 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 Environments in the lefthand navigation, 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). To avoid having to enter a credit card, add an additional promo code CONFLUENTDEV1. With this promo code, you will not have to enter a credit card for 30 days or until your credits run out.

  4. Click on LEARN and follow the instructions to launch a Kafka cluster and 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.