DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Scripting Tmux for Kafka

Scripting Tmux for Kafka

Learn more about tmux, and the Processor API in Kafka.

John Vester user avatar by
John Vester
CORE ·
Dec. 18, 15 · Analysis
Like (3)
Save
Tweet
Share
3.61K Views

Join the DZone community and get the full member experience.

Join For Free

I’ve known about tmux for some time now, but I kept putting off working with it. Lately I’ve started looking at the Processor API available in Kafka. After opening up 4-5 terminal windows (Zookeeper, Kafka, sending messgaes, recieving messages) and toggling in between them, I quickly realized I needed a way to open up these terminal sessions at one time and have them centralized. I had the perfect use case to use tmux! This post is about scripting tmux to save time during devlopment, in this case working with kafka.

In this post we are going to assume the reader is alreay familiar with zookeeper and kafka.

Getting Tmux

Before we start it’s helpful to know how we can install tmux. Tmux can be downloaded from the main site. In my case I’m using a mac so I used homebrew – brew install tmux.

Scripting Tmux

I wanted a script to create the following workspace:

  1. Zookeeper running in it’s own pane.
  2. Kafka server running in it’s own pane.
  3. A separate pane to send messages.
  4. A separate pane to receive messages.
  5. Finally a pane to run my kafka-processor code or perform ad-hoc tasks.

I wanted to have zookeeper and kafka start automatically, the message producer and consumer I would be stopping and starting as needed. But I would like each respective pane to cd to the base kafka install directory. Once I found the relevant tmux commands it was simple to set up. Here’s the script:

#!/bin/sh

KAFKA_DIR=/usr/local/kafka_2.11-0.9.0.0-SNAPSHOT

START_ZK="./bin/zookeeper-server-start.sh"
ZK_PROPS="config/zookeeper.properties"

START_KAFKA="./bin/kafka-server-start.sh"
KAFKA_PROPS="config/server.properties"

tmux new-session -s kafka-work -d

#split screen in have horizontally
tmux split-window -v
#split the second half in half again
tmux split-window -v

#split the top window in half vertically
tmux split-window -h -t 0
#split the new half top window in half horizontally
tmux split-window -v

#cd into kafka directory
tmux send-keys -t 1 "cd $KAFKA_DIR"  C-m
#start zookeeper
tmux send-keys -t 1 "$START_ZK  $ZK_PROPS" C-m

tmux send-keys -t 2 "cd $KAFKA_DIR"  C-m
tmux send-keys -t 2 "$START_KAFKA $KAFKA_PROPS" C-m

tmux send-keys -t 3 "cd $KAFKA_DIR"  C-m
tmux send-keys -t 4 "cd $KAFKA_DIR"  C-m

tmux attach -t kafka-work

While in depth coverage of tmux is beyond the scope of this post, it will be helpful to give a brief description of the tmux commands in the script.

Splitting Panes

There are serveral split-window commands in the script and they do what you’d expect, split the window either horizontally or vertically. What’s not so obvious is that split-window -v splits the window horizontally and split-window -h splits vertically. Tmux sees the resulting panes differently than we humans. When the window is divided horizontally, two panes stacked on top of each other vertically and for vertical splits, panes are horizontally adjacent to each other. There is one final note about splitting a window into panes that needs mentioning. When the split-window command is issued the cursor ends up in the most recently created pane. This is important to keep in mind if you are going to issue additional commands but don’t want them executed in the pane just created.

Pane Numbers

By default, tmux numbers the panes in a given window starting at 0. If the initial window is split in two panes horizontally, the top pane is still 0, and the bottom half is now number 1. While this seems very natural, when additional panes are split in different areas, the numbering may seem confusing. Just keep in mind panes are numbered in order of creation.

Sending Commands to Panes

Finally we have the send-keys -t N <some command> commands in the script. This is so we can target a particular pane with the given command. In our case we want 4 of our panes to be in the base kakfa install directory so we issue the tmux send-keys -t N "cd $KAFKA_DIR" C-m command. There are 4 such commands, each one with a specific target pane 1-4. We want to start zookeeper in pane 1 so the tmux send-keys -t 1 "$START_ZK $ZK_PROPS" C-m command is used. To start the kafka server we issue a very similar command, except pane 2 is the target.

Final Tmux – Kafka Results

Here’s a screen shot of the final results of running the the kafka-tmux script. The numbers in the panes are for illustration purposes only.

Conclusion

This has been a quick tour of tmux. It is woefully incomplete and only scratches the surface on what we can do with tmux and how it can be configured. I just wanted to share my script in case someone might find it helpful. Thanks for your time.

kafka Command (computing)

Published at DZone with permission of John Vester, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Integrate AWS Secrets Manager in Spring Boot Application
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Detecting Network Anomalies Using Apache Spark
  • Asynchronous Messaging Service

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: