I am following this tutorial: https://florimond.dev/blog/articles/2018/09/building-a-streaming-fraud-detection-system-with-kafka-and-python/
The script to create the Producer App is failing, exiting with code 1, due to a NameError: name 'json' is not defined.
Here is the full output:
xxxx@xxxx:~/xxxx/fraud_detection$ docker-compose up WARNING: Found orphan containers (fraud_detection_zookeeper_1, fraud_detection_broker_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. Starting fraud_detection_generator_1 ... done Attaching to fraud_detection_generator_1 generator_1 | Traceback (most recent call last): generator_1 | File "app.py", line 28, in generator_1 | producer.send(TRANSACTIONS_TOPIC, value=transaction) generator_1 | File "/usr/local/lib/python3.6/site-packages/kafka/producer/kafka.py", line 583, in send generator_1 | topic, value) generator_1 | File "/usr/local/lib/python3.6/site-packages/kafka/producer/kafka.py", line 714, in _serialize generator_1 | return f(data) generator_1 |
File "app.py", line 24, in generator_1 |
value_serializer=lambda value: json.dumps(value).encode(), generator_1 | NameError: name 'json' is not defined fraud_detection_generator_1 exited with code 1
Here is the full code for the Producer App:
import os
from time import sleep
import json
from kafka import KafkaProducer
from transactions import create_random_transaction
TRANSACTIONS_TOPIC = os.environ.get('TRANSACTIONS_TOPIC')
KAFKA_BROKER_URL = os.environ.get('KAFKA_BROKER_URL')
TRANSACTIONS_PER_SECOND = float(os.environ.get('TRANSACTIONS_PER_SECOND'))
SLEEP_TIME = 1 / TRANSACTIONS_PER_SECOND
if __name__ == '__main__':
producer = KafkaProducer(
bootstrap_servers=KAFKA_BROKER_URL,
# Encode all values as JSON
value_serializer=lambda value: json.dumps(value).encode(),
)
while True:
transaction: dict = create_random_transaction()
producer.send(TRANSACTIONS_TOPIC, value=transaction)
print(transaction) # DEBUG
sleep(SLEEP_TIME)
If I run a command json in the terminal it runs fine.
Any help would be greatly appreciated