'How to write a bash command to open up a kafka producer and immediately follow with topic to create?

What's the best way to run the following sequence of commands

kafka-console-producer --topic discounts --broker-list localhost:9092 --property parse.key=true --property key.separator=,
profile1,{"profile":"profile1","amount":0.5 }
profile2,{"profile":"profile2","amount":0.25 }
profile3,{"profile":"profile3","amount":0.15 }
exit
kafka-console-producer --topic discount-profiles-by-user --broker-list localhost:9092 --property parse.key=true --property key.separator=,
Daniel,profile1
Riccardo,profile2
exit
...

I'm trying to mass create kafka messages for topics without typing line-by-line



Solution 1:[1]

without typing line-by-line

Create files with the data you want to upload

Then use bash redirection

kafka-console-producer \
 --topic discounts \
 --broker-list localhost:9092 \
 --property parse.key=true --property key.separator=, < data.csv

If you name the files by the topic name, then you could write a for loop over each file and use that file name as the topic name

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OneCricketeer