Skip to content

Commit a25352e

Browse files
authored
Re adding Run example
1 parent 4ad7273 commit a25352e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

articles/hdinsight/kafka/apache-kafka-producer-consumer-api.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,49 @@ If you would like to skip this step, prebuilt jars can be downloaded from the `P
139139
scp ./target/kafka-producer-consumer*.jar [email protected]:kafka-producer-consumer.jar
140140
```
141141
142+
## <a id="run"></a> Run the example
143+
This conversation was marked as resolved by anusricorp
144+
145+
1. Replace `sshuser` with the SSH user for your cluster, and replace `CLUSTERNAME` with the name of your cluster. Open an SSH connection to the cluster, by entering the following command. If prompted, enter the password for the SSH user account.
146+
147+
```cmd
148+
149+
```
150+
151+
1. To get the Kafka broker hosts, substitute the values for `<clustername>` and `<password>` in the following command and execute it. Use the same casing for `<clustername>` as shown in the Azure portal. Replace `<password>` with the cluster login password, then execute:
152+
153+
```bash
154+
sudo apt -y install jq
155+
export clusterName='<clustername>'
156+
export password='<password>'
157+
export KAFKABROKERS=$(curl -sS -u admin:$password -G https://$clusterName.azurehdinsight.net/api/v1/clusters/$clusterName/services/KAFKA/components/KAFKA_BROKER | jq -r '["\(.host_components[].HostRoles.host_name):9092"] | join(",")' | cut -d',' -f1,2);
158+
```
159+
160+
> [!Note]
161+
> This command requires Ambari access. If your cluster is behind an NSG, run this command from a machine that can access Ambari.
162+
1. Create Kafka topic, `myTest`, by entering the following command:
163+
164+
```bash
165+
java -jar kafka-producer-consumer.jar create myTest $KAFKABROKERS
166+
```
167+
168+
1. To run the producer and write data to the topic, use the following command:
169+
170+
```bash
171+
java -jar kafka-producer-consumer.jar producer myTest $KAFKABROKERS
172+
```
173+
174+
1. Once the producer has finished, use the following command to read from the topic:
175+
176+
```bash
177+
java -jar kafka-producer-consumer.jar consumer myTest $KAFKABROKERS
178+
scp ./target/kafka-producer-consumer*.jar [email protected]:kafka-producer-consumer.jar
179+
```
180+
181+
The records read, along with a count of records, is displayed.
182+
183+
1. Use __Ctrl + C__ to exit the consumer.
184+
142185
### Multiple consumers
143186
144187
Kafka consumers use a consumer group when reading records. Using the same group with multiple consumers results in load balanced reads from a topic. Each consumer in the group receives a portion of the records.

0 commit comments

Comments
 (0)