try: KAFKA_APIKEY = os.environ['KAFKA_APIKEY'] except KeyError: print( "The KAFKA_APIKEY environment variable not set... assume local deployment" ) try: KAFKA_ENV = os.environ['KAFKA_ENV'] except KeyError: KAFKA_ENV = 'LOCAL' TOPIC_NAME = 'orders' OID = '' def parseArguments(): if len(sys.argv) != 2: print("Need to have at least one argument order ID") exit(1) OID = sys.argv[1] print("The arguments are: ", str(sys.argv)) if __name__ == '__main__': parseArguments() orderConsumer = KafkaConsumer(KAFKA_ENV, KAFKA_BROKERS, KAFKA_APIKEY, TOPIC_NAME) orderConsumer.prepareConsumer() orderConsumer.pollNextEvent(OID, 'orderID') orderConsumer.close()
print( "The KAFKA_APIKEY environment variable not set... assume local deployment" ) try: KAFKA_ENV = os.environ['KAFKA_ENV'] except KeyError: KAFKA_ENV = 'LOCAL' CID = "c_1" TOPIC_NAME = "containers" def parseArguments(): if len(sys.argv) != 2: print("Need to have at least one argument container ID") exit(1) CID = sys.argv[1] print("The arguments are: ", str(sys.argv)) print(KAFKA_ENV) print(KAFKA_BROKERS) print(KAFKA_APIKEY) if __name__ == '__main__': parseArguments() consumer = KafkaConsumer(KAFKA_ENV, KAFKA_BROKERS, KAFKA_APIKEY, TOPIC_NAME) consumer.prepareConsumer() consumer.pollNextEvent(CID, 'containerID') consumer.close()
print("[ERROR] - The SCRAM_PASSWORD environment variable needs to be set") exit(1) ####################### FUNCTIONS ####################### # Parse arguments to get the Kafka topic def parseArguments(): global TOPIC_NAME print("The arguments for this script are: ", str(sys.argv)) if len(sys.argv) == 2: TOPIC_NAME = sys.argv[1] else: print( "[ERROR] - The ConsumePlainMessage.py script expects one argument: The Kafka topic to consume messages from" ) exit(1) ####################### MAIN ####################### if __name__ == '__main__': # Parse arguments to get the topic to read from parseArguments() # Create a Kafka Consumer kafka_consumer = KafkaConsumer(KAFKA_BROKERS, SCRAM_USERNAME, SCRAM_PASSWORD, TOPIC_NAME) # Prespare the consumer kafka_consumer.prepareConsumer() # Poll for next message kafka_consumer.pollNextEvent() # Close the consumer kafka_consumer.close()