eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

In this tutorial, we’ll learn how to connect Apache Kafka to ElasticSearch using the Kafka Connector Sink.

The Kafka project provides Kafka Connect, a powerful tool that allows seamless integration between Kafka and external data store sources without the need for additional code or applications.

2. Why Use Kafka Connect?

Kafka Connect provides an easy way to stream data between Kafka and various data stores, including ElasticSearch. Instead of writing a custom application to consume data from Kafka and dump it into ElasticSearch, we can use it because it’s designed for scalability, fault tolerance, and manageability. Some benefits of Kafka Connect are:

  • Scalability: Kafka Connect can run in distributed mode, allowing multiple workers to share the load
  • Fault Tolerance: Automatic handling of failures so that it’s possible to preserve data correctness and integrity. This also makes our pipelines more resilient
  • Self-serviced Connectors: No need to write custom integration components or services
  • Highly Configurable: Easy to set up and manage through simple configuration and APIs

3. Docker Setup

Let’s use Docker to deploy and manage our installation. This will simplify the setup and reduce issues with platform dependencies. The respective teams maintain official images for all required services.

We’ll define a Docker Compose file to spin up the services: Kafka, Zookeeper, ElasticSearch, and Kafka Connect. In this article, we won’t discuss the Kafka setup in-depth, but here we find out more about it.

The first step is to create the Docker Compose file:

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
    ports:
      - "2181:2181"
  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.6.0
    environment:
      discovery.type: single-node
      xpack.security.enabled: "false"
    ports:
      - "9200:9200"
  kafka-connect:
    image: confluentinc/cp-kafka-connect:latest
    depends_on:
      - kafka
    ports:
      - "8083:8083"
    environment:
      CONNECT_BOOTSTRAP_SERVERS: kafka:9092
      CONNECT_REST_ADVERTISED_HOST_NAME: kafka-connect
      CONNECT_GROUP_ID: kafka-connect-group
      CONNECT_CONFIG_STORAGE_TOPIC: connect-configs
      CONNECT_OFFSET_STORAGE_TOPIC: connect-offsets
      CONNECT_STATUS_STORAGE_TOPIC: connect-status
      CONNECT_KEY_CONVERTER: org.apache.kafka.connect.storage.StringConverter
      CONNECT_VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter
      CONNECT_VALUE_CONVERTER_SCHEMAS_ENABLE: "false"
      CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: 1
      CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: 1
      CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: 1

Basically, we’ve created Zookeeper to hold our Kafka cluster setting, a Kafka broker to handle our topic data, and pointed it to Zookeeper service. Then, we also created an ElasticSearch instance, and for simplicity, we disabled authentication.

Our Kafka Connect basic properties require minimal setup to run our Kafka Connectors locally. They set things like the replication factor, default converters, and Kafka cluster address. To understand all configurations, please check the official documentation page.

It’s important to highlight that the configuration above is not recommended for production. Instead, it is a quick-start guide for playing around with Kafka Connectors. Resilience and fault tolerance are not concerns for this article.

Once we understand the content of our Docker Compose file, we can run our services:

# use -d to run in background 
docker compose up

Once the containers are running, we need to manually install the Elasticsearch Sink Connector since it isn’t built into the Kafka Connector. For that, let’s run the following command:

docker exec -it kafka-elastic-search-kafka-connect-1 bash -c
  "confluent-hub install --no-prompt
  confluentinc/kafka-connect-elasticsearch:latest"

Then, next, we need to restart the Kafka Connect service so that we can start using the new Sink:

docker restart kafka-elastic-search-kafka-connect-1

Finally, to check that everything worked as expected, we can call the Kafka Connect API to check the available Sinks:

curl -s http://localhost:8083/connector-plugins | jq .

We should see io.confluent.connect.elasticsearch.ElasticsearchSinkConnector within the response.

4. Hello World

Now, let’s try to send our first message, which flows from Kafka to ElasticSearch. In order to do so, we first need to create our topic, which is as follows:

docker exec -it $(
  docker ps --filter "name=kafka-elastic-search-kafka-1" --format "{{.ID}}"
) bash -c
  "kafka-topics --create --topic logs
  --bootstrap-server kafka:9092
  --partitions 1
  --replication-factor 1"

This will create our Kafka topic in the Kafka Broker. Next, let’s create a file called test-connector.json:

{
    "name": "elasticsearch-sink-connector-test",
    "config": {
        "connector.class": "io.confluent.connect.elasticsearch.ElasticsearchSinkConnector",
        "type.name": "_doc",
        "connection.url": "http://elasticsearch:9200",
        "tasks.max": "1",
        "topics": "logs",
        "key.ignore": "true",
        "schema.ignore": "true"
    }
}

The file contains our Kafka Connector Sink and its configuration. We’ll better understand these configurations later, but we need to know that this is the payload required to create a connector via the API. The four first properties of this file will be the same for all other examples in this article, so they’ll be omitted for simplicity.

Let’s now create our Kafka connector:

curl -X POST -H 'Content-Type: application/json' --data @test-connector.json http://localhost:8083/connectors

By doing this, our connector is created, and it should be running to confirm that we can use the name of the connector defined in the JSON file and query it using another Kafka Connect API:

curl http://localhost:8083/connectors/elasticsearch-sink-connector-test/status

This should confirm that our connector is up and running.

Now that we know our connector is running, let’s send our first message. To simulate a Kafka producer, we can run the following line:

docker exec -it $(docker ps --filter "name=kafka-elastic-search-kafka-1" --format "{{.ID}}")
  kafka-console-producer --broker-list kafka:9092 --topic logs

The command above creates an interactive prompt that allows us to send messages to our logs Kafka topic. We can create any valid JSON and press enter to send the message:

{"message": "Hello word", "timestamp": "2025-02-05T12:00:00Z"}
{"message": "Test Kafka Connector", "timestamp": "2025-02-05T13:00:00Z"}

To verify if the data arrives at ElasticSearch, we can open another terminal and call:

 curl -X GET "http://localhost:9200/logs/_search?pretty"

As we can observe, the data flowed automatically from our Kafka topic to ElasticSearch; binding our topic to our ElasticSearch index was the only required step. However, this connector offers much more.

5. Advanced Scenarios for Kafka Connect Elasticsearch Sink

As mentioned previously, Kafka connectors are powerful tools that offer robust mechanisms for integrating data stores and Kafka. Kafka Connect provides an extensive range of configuration options, allowing users to define their data pipelines to fulfill their use cases.

Processing distributed messages or data streams can be a very complex problem. This tool aims to simplify it. Let’s consider some common scenarios.

5.1. Kafka Avro Messages Sent to Elasticsearch

Many projects use the Avro format because it is efficient in serialization and schema evolution. When using Avro, Elasticsearch should automatically detect field types based on the schema. Let’s look into leveraging Avro schemas when integrating with Elasticsearch.

First, we will need an Avro schema registry:

schema-registry:
    image: confluentinc/cp-schema-registry:latest
    depends_on:
      - kafka
    ports:
      - "8081:8081"
    environment:
      SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: "kafka:9092"
      SCHEMA_REGISTRY_HOST_NAME: "schema-registry"

The first step is to add this new service to our Docker Compose file and run:

docker compose up -d

Once we have our schema registry, let’s create a new topic to hold our Avro messages:

docker exec -it $(
  docker ps --filter "name=kafka-elastic-search-kafka-1" --format "{{.ID}}"
) bash -c
"kafka-topics --create
  --topic avro_logs
  --bootstrap-server kafka:9092
  --partitions 1
  --replication-factor 1"

The next step is creating a new connector configuration file named avro-sink-config.json:

{
  "name": "avro-elasticsearch-sink",
  "config": {
    ...
    "key.ignore": "true",
    "schema.ignore": "false",
    "value.converter": "io.confluent.connect.avro.AvroConverter",
    "value.converter.schema.registry.url": "http://schema-registry:8081"
  }
}

Let’s take a moment to understand this file:

  • schema.ignore: this tells the connector to use the message schema to create the ElasticSearch documents. In this case, the schema registry definition will be used to define the index mapping
  • value.converter: tell the connector the messages are following Avro format (io.confluent.connect.avro.AvroConverter)
  • value.converter.schema.registry.url: specifies the schema registry location

Having understood the configurations, we can proceed and create our connector:

curl -X POST -H "Content-Type: application/json" --data @avro-sink-config.json http://localhost:8083/connectors

We can confirm the connector is running by checking the status as we did before. After confirming it, we can move ahead to create our Avro messages:

docker exec -it $(
  docker ps --filter "name=kafka-elastic-search-schema-registry-1" --format "{{.ID}}"
) kafka-avro-console-producer
  --broker-list kafka:9092
  --topic avro_logs
  --property value.schema='{
   "type": "record",
   "name": "LogEntry",
   "fields": [
     {"name": "message", "type": "string"},
     {"name": "timestamp", "type": "long"}
   ]
 }'

With the prompt ready, let’s send a testing message like:

{"message": "My Avro message", "timestamp": 1700000000}

Finally, let’s look at ElasticSearch and see our message and mappings:

curl -X GET "http://localhost:9200/avro_logs/_search?pretty"

And then:

curl -X GET "http://localhost:9200/avro_logs/_mapping"

As we can see, the mapping was created using the schema.

Before moving to our next test, let’s clean up:

curl -X DELETE "http://localhost:9200/avro_logs"

And then:

curl -X DELETE "http://localhost:8083/connectors/avro-elasticsearch-sink"

This will delete the Kafka connector and the ElasticSearch index.

5.2. Timestamp Transformation

Let’s use a new connector configuration file, timestamp-transform-sink.json, to automatically convert epoch timestamps into ISO-8601 format. The configuration goes as follows:

{
    "name": "timestamp-transform-sink",
    "config": {
        ...
        "topics": "epoch_logs",
        "key.ignore": "true",
        "schema.ignore": "true",
        "transforms": "TimestampConverter",
        "transforms.TimestampConverter.type":"org.apache.kafka.connect.transforms.TimestampConverter$Value",
        "transforms.TimestampConverter.field": "timestamp",
        "transforms.TimestampConverter.target.type": "string",
        "transforms.TimestampConverter.format": "yyyy-MM-dd'T'HH:mm:ssZ"
    }
}

Let’s look at the following highlights:

  • transforms: defines the transforms name in order to be applied in our data processing pipeline
  • TimestampConverter: defines a transformation that extracts a field from the message and converts it using a particular format

Then, we create the connector:

curl -X POST -H "Content-Type: application/json" --data @timestamp-transform-sink.json http://localhost:8083/connectors 

Let’s test it:

docker exec -it $(
  docker ps --filter "name=kafka-elastic-search-kafka-1" --format "{{.ID}}"
) kafka-console-producer
  --broker-list kafka:9092
  --topic epoch_logs

Sending message:

{"message": "Timestamp transformation", "timestamp": 1700000000000}

To confirm it, let’s run:

curl -X GET "http://localhost:9200/epoch_logs/_search?pretty"

And then:

curl -X GET "http://localhost:9200/epoch_logs/_mapping"

Here, we saw how the timestamp was transformed, and ElasticSearch correctly mapped the field to the data type.

5.3. Ignoring and Logging Errors

By default, the connector has a property called errors.tolerance, which is defined as none. This means the connectors will stop processing when an error occurs. However, sometimes, when processing in real-time, that might not be a good idea. For this reason, now let’s see how to make a connector ignore an error and move forward.

Again, we start by creating a topic:

docker exec -it $(
  docker ps --filter "name=kafka-elastic-search-kafka-1" --format "{{.ID}}"
) bash -c
"kafka-topics --create
  --topic test-error-handling
  --bootstrap-server kafka:9092
  --partitions 1
  --replication-factor 1"

Then, we’ll configure the connector error-handling-sink-config.json:

{
    "name": "error-handling-elasticsearch-sink",
    "config": {
        ...
        "topics": "test-error-handling",
        "key.ignore": "true",
        "schema.ignore": "true",
        "behavior.on.malformed.documents": "warn",
        "behavior.on.error": "LOG",
        "errors.tolerance": "all",
        "errors.log.enable": "true",
        "errors.log.include.messages": "true"
    }
}

Key Properties:

  • behavior.on.malformed.documents: logs invalid documents instead of stopping the connector
  • errors.tolerance: allows Kafka Connect to continue processing valid messages despite errors
  • errors.log.enable: logs errors to Kafka Connect logs
  • errors.log.include.messages: includes the actual problematic message in logs

Now we register the connector:

curl -X POST -H "Content-Type: application/json" --data @error-handling-sink-config.json http://localhost:8083/connectors

Then, let’s open a console to test it:

docker exec -it $(
  docker ps --filter "name=kafka-elastic-search-kafka-1" --format "{{.ID}}"
) kafka-console-producer
  --broker-list kafka:9092
  --topic test-error-handling

Next, we send the following messages:

{"message": "Ok", "timestamp": "2025-02-08T12:00:00Z"}
{"message": "NOK", "timestamp": "invalid_timestamp"}
{"message": "Ok Again", "timestamp": "2025-02-08T13:00:00Z"}

Finally, when let’s check ElasticSearch:

curl -X GET "http://localhost:9200/test-error-handling/_search?pretty"

We can confirm only the first and the last messages were indexed. Now, let’s check the connector logs:

docker logs kafka-elastic-search-kafka-connect-1 | grep "ERROR"

The logs show the error when processing offset 1 of the topic. However, the connector status is running, which is what we would like to happen.

5.4. Fine-Tuning Bulk Processing and Flushing in Elasticsearch

When it comes to efficiently processing data stream at scale, many variables come into play. For this reason, we’ll not test a particular scenario this time. Instead, let’s take some time to look at the different parameters ElastickSearch Connector Sink makes available for us to fine-tune our use case.

The combination of such configurations will directly impact our efficiency and scalability. Therefore, it is essential to properly design some capacity plan and execute it against different combinations of configurations in order to understand how they affect our workload. Let’s now check the most relevant configurations related to ingestion and data flushing:

Parameter Name Default value
batch.size 2000 (can go from 1 to 1000000)
bulk.size.bytes 5 megabytes (can go to GBs)
max.in.flight.requests 5 (can go from 1 to 1000)
max.buffered.records 20000 (can go from 1 to 2147483647)
linger.ms 1 (can go from 0 to 604800000)
flush.timeout.ms 3 minutes (can go to hours)
flush.synchronously false
max.retries 5
retry.backoff.ms 100
connection.compression false
write.method INSERT (can also be UPSERT)
read.timeout.ms 3 minutes (can go to hours)

For the exhaustive list, we can check the official documentation page.

6. Conclusion

Following this guide, we’ve successfully established a near real-time data pipeline from Kafka to Elasticsearch using the Kafka Connect Sink. The additional test scenarios ensure flexibility in handling various real-world data transformations and ingestion strategies. We also get to know all the controls and mechanisms this connector provides us to fine-tune our stream pipeline.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)