app-backup.py 805 B

1234567891011121314151617181920212223242526
  1. """Produce fake transactions into a Kafka topic."""
  2. import os
  3. from time import sleep
  4. import json
  5. from kafka import KafkaProducer
  6. from transactions import create_random_transaction
  7. TRANSACTIONS_TOPIC = os.environ.get('TRANSACTIONS_TOPIC')
  8. KAFKA_BROKER_URL = os.environ.get('KAFKA_BROKER_URL')
  9. TRANSACTIONS_PER_SECOND = float(os.environ.get('TRANSACTIONS_PER_SECOND'))
  10. SLEEP_TIME = 1 / TRANSACTIONS_PER_SECOND
  11. if __name__ == '__main__':
  12. producer = KafkaProducer(
  13. bootstrap_servers=KAFKA_BROKER_URL,
  14. # Encode all values as JSON
  15. value_serializer= json.dumps(value).encode(),
  16. )
  17. while True:
  18. transaction: dict = create_random_transaction()
  19. producer.send(TRANSACTIONS_TOPIC, value=transaction)
  20. print(transaction) # DEBUG
  21. sleep(SLEEP_TIME)