Open Kilda Java Documentation
KafkaConsumer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright 2017 Telstra Open Source
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 
17 import logging
18 import argparse
19 import uuid
20 from kafka import KafkaConsumer
21 
22 KAFKA_BOOTSTRAP_SERVERS = 'hadoop01.staging.pen:6667'
23 
24 
26  parser = argparse.ArgumentParser()
27  parser.add_argument('server', action='store', help='Kafka server:port.')
28  parser.add_argument('topic', action='store', help='Kafka topic to listen on.')
29  return parser.parse_args()
30 
31 
32 def main(args):
33  group_uuid = uuid.uuid4()
34  consumer = KafkaConsumer(bootstrap_servers=args.server,
35  auto_offset_reset='latest',
36  group_id=group_uuid,
37  api_version=(0, 9))
38  topics = [args.topic]
39  consumer.subscribe(topics)
40 
41  for message in consumer:
42  print (message)
43 
44 
45 if __name__ == "__main__":
46  logging.basicConfig(
47  format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
48  level=logging.INFO
49  )
50  logger = logging.getLogger(__name__)
52  main(args)
def main(args)
def parse_cmdline()