Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
fix out of range issue (#6)
Browse files Browse the repository at this point in the history
* cleanup producer a bit

* change screenshots
  • Loading branch information
lucj authored Jan 30, 2024
1 parent e3d708b commit 141b873
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
Binary file modified examples/images/consumer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/images/producer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 7 additions & 16 deletions examples/producer/producer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from kafka import KafkaProducer
from faker import Faker
import pycountry
import random
import time
Expand All @@ -9,26 +8,18 @@
topic = os.environ['KAFKA_TOPIC']
producer = KafkaProducer(bootstrap_servers=bootstrap_servers)

# Set up Faker to generate country-related data
fake = Faker()

# Get total number of countries
total_countries = len(pycountry.countries)

# Generate a dummy messsage every second
while True:
# Get random country code and related name
country_code = fake.country_code()
country_description = fake.country()

# Convert to bytes
key = country_code.encode('utf-8')
value = country_description.encode('utf-8')

random_index = random.randint(0, total_countries)
while True:
# Get a random country
random_index = random.randint(0, total_countries-1)
country = list(pycountry.countries)[random_index]
print(country)

value = "alpha_2: {}, alpha_3: {}, name: {}, numeric: {}".format(country.alpha_2, country.alpha_3, country.name, country.numeric).encode('utf-8')
# Define key and value
key = country.alpha_2.encode('utf-8')
value = "alpha_3: {}, name: {}, numeric: {}".format(country.alpha_3, country.name, country.numeric).encode('utf-8')

# Sent message
try:
Expand Down

0 comments on commit 141b873

Please sign in to comment.