|
2 | 2 | from pyspark.streaming import StreamingContext |
3 | 3 | from pyspark.streaming.kafka import KafkaUtils |
4 | 4 |
|
5 | | -#def main(): |
6 | 5 |
|
| 6 | +class kafka_monitor(object): |
7 | 7 |
|
| 8 | + def __init__(self): |
| 9 | + # Define Spark configuration |
| 10 | + conf = SparkConf() |
| 11 | + conf.setMaster("local[4]") |
| 12 | + conf.setAppName("Kafka Monitor") |
| 13 | + # Initialize a SparkContext |
| 14 | + sc = SparkContext(conf=conf) |
| 15 | + self.ssc = StreamingContext(sc, 10) |
8 | 16 |
|
9 | | -if __name__=="__main__": |
10 | | - # Define Spark configuration |
11 | | - conf = SparkConf() |
12 | | - conf.setMaster("local[4]") |
13 | | - conf.setAppName("Stream Analysis") |
14 | | - # Initialize a SparkContext |
15 | | - sc = SparkContext(conf=conf) |
16 | | - |
17 | | - #batch_interval = 10 |
18 | | - #window_time = 10 |
19 | | - #process_times = 1 |
20 | | - |
21 | | - # Compute the whole time for displaying |
22 | | - #total_time = batch_interval * process_times |
23 | | - |
24 | | - #main(sc) |
25 | | - |
26 | | - ssc = StreamingContext(sc, 10) |
27 | | - |
28 | | - #zkQuorum, topic = sys.argv[1:] |
29 | | - zkQuorum = 'localhost:9092' |
30 | | - topic = 'TutorialTopic' |
31 | | - kvs = KafkaUtils.createDirectStream(ssc, [topic], {"metadata.broker.list": 'localhost:9092'}) |
32 | | - kvs.foreachRDD(lambda x: print(x.collect())) |
33 | | - #lines = kvs.map(lambda x: x[1]) |
34 | | - #counts = lines.flatMap(lambda line: line.split(" ")).map(lambda word: (word, 1)).reduceByKey(lambda a, b: a+b) |
35 | | - #counts.pprint() |
36 | | - #kvs.pprint() |
37 | | - |
38 | | - ssc.start() |
39 | | - ssc.awaitTermination() |
| 17 | + self.addr = 'localhost:9092' |
| 18 | + self.topic = 'TutorialTopic' |
40 | 19 |
|
| 20 | + def run(self): |
| 21 | + lines = KafkaUtils.createDirectStream(self.ssc, [self.topic], {"metadata.broker.list": self.addr}) |
| 22 | + lines.foreachRDD(lambda x: print(x.collect())) |
41 | 23 |
|
| 24 | + self.ssc.start() |
| 25 | + self.ssc.awaitTermination() |
42 | 26 |
|
| 27 | +if __name__=="__main__": |
| 28 | + monitor = kafka_monitor() |
| 29 | + monitor.run() |
43 | 30 |
|
0 commit comments