How copy some message from one kafka topic to another from bash?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is an open-source stream-processing software platform developed by LinkedIn and donated to the Apache Software Foundation, written in Scala and Java. Kafka is designed to handle real-time data feeds with high throughput and low latency. One common task when working with Kafka is copying messages from one topic to another, which can be useful for numerous purposes like data migration, testing, or creating backup copies of data.
Using Kafka Console Tools
The simplest method to copy messages from one Kafka topic to another is by using the Kafka console tools that come bundled with Kafka. Specifically, you can use the combination of kafka-console-consumer and kafka-console-producer.
Here are the steps and a basic example:
- Start the Consumer: Begin by consuming messages from the source topic. Use the
kafka-console-consumertool to read the messages. - Pipe to Producer: Directly pipe the output of the consumer to
kafka-console-producer, which sends the messages to the destination topic.
Basic Command Structure:
This command will:
- Start consuming messages from
source_topicon a Kafka server running on localhost:9092. - Copy all messages (from the beginning of the topic) to
destination_topicon the same Kafka instance.
Using Kafka Connect
For more robust and scalable solutions, especially in production, Kafka Connect can be used. Kafka Connect is a tool for scalably and reliably streaming data between Apache Kafka and other systems.
- Configure a Source Connector: This read data from the source topic.
- Configure a Sink Connector: This writes data to the destination topic.
Example Configurations:
source-connector.properties:
sink-connector.properties:
Using kafkacat
kafkacat is a command-line utility that provides a fast and flexible way to interact with Kafka from the bash shell. It can produce and consume messages, making it suited for operations like copying topics.
Command to Copy Topics:
Practical Considerations
- Data Integrity: Ensure that messages are not altered during the process unless intended.
- Performance: Test the impact on the network and Kafka cluster performance, particularly with large volumes of data.
- Security: Use SSL/TLS and authentication as needed to protect the data during transfer.
Summary Table:
| Key Component | Tool | Purpose |
| Consuming Messages | kafka-console-consumer | Consumes messages from a topic |
| Producing Messages | kafka-console-producer | Publishes messages to a topic |
| Data Pipeline | Kafka Connect | Connects Kafka with external systems for robust data pipelines |
| Command Line Utility | kafkacat | Flexible, quick Kafka interaction |
This summary should provide a clear view of how different tools and methods can be used to copy messages from one Kafka topic to another through a bash interface. Depending on the use case — whether it’s for development, testing, or production — the choice of tool and the configuration might vary.
Related reading
- How create Kafka ZKStringSerializer in Java?
- How customer offsets are maintained in mirrored cluster in Kafka?
- how do i add a topic to a running kafka container using docker commands?
- How do I configure spring-kafka to ignore messages in the wrong format?
- How do I connect to a Kerberos-secured Kafka cluster with Spark Structured Streaming?
- How do I create a memory bound message queue in Erlang?
- How do I create a topic in a running container instance of spotif/kafka from bash
- How do I create or add a user to rabbitmq?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.