|
| 1 | +package com.msj.storm.kafka; |
| 2 | + |
| 3 | +import backtype.storm.Config; |
| 4 | +import backtype.storm.LocalCluster; |
| 5 | +import backtype.storm.generated.AlreadyAliveException; |
| 6 | +import backtype.storm.generated.InvalidTopologyException; |
| 7 | +import backtype.storm.topology.TopologyBuilder; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author Vincent.M mengshaojie@188.com |
| 11 | + * @date 2017/5/11 下午2:57 |
| 12 | + * @copyright ©2017 孟少杰 All Rights Reserved |
| 13 | + * @desc kafka与storm,缺少jar包,所以需要jar包地方屏蔽掉 |
| 14 | + */ |
| 15 | +public class KafkaTopology { |
| 16 | + public static void main(String[] args) throws |
| 17 | + AlreadyAliveException, InvalidTopologyException { |
| 18 | + // zookeeper hosts for the Kafka cluster |
| 19 | + //缺jar包 |
| 20 | +// ZkHosts zkHosts = new ZkHosts("192.168.1.114:2181"); |
| 21 | + |
| 22 | + // Create the KafkaSpout configuartion |
| 23 | + // Second argument is the topic name |
| 24 | + // Third argument is the zookeeper root for Kafka |
| 25 | + // Fourth argument is consumer group id |
| 26 | + //缺jar包 |
| 27 | +// SpoutConfig kafkaConfig = new SpoutConfig(zkHosts, |
| 28 | +// "words_topic", "", "id7"); |
| 29 | + |
| 30 | + // Specify that the kafka messages are String |
| 31 | + //缺jar包 |
| 32 | +// kafkaConfig.scheme = new SchemeAsMultiScheme(new StringScheme()); |
| 33 | + |
| 34 | + // We want to consume all the first messages in the topic everytime |
| 35 | + // we run the topology to help in debugging. In production, this |
| 36 | + // property should be false |
| 37 | + //缺jar包 |
| 38 | +// kafkaConfig.forceFromStart = true; |
| 39 | + |
| 40 | + // Now we create the topology |
| 41 | + TopologyBuilder builder = new TopologyBuilder(); |
| 42 | + |
| 43 | + // set the kafka spout class |
| 44 | + //缺jar包 |
| 45 | +// builder.setSpout("KafkaSpout", new KafkaSpout(kafkaConfig), 1); |
| 46 | + |
| 47 | + // configure the bolts |
| 48 | + builder.setBolt("SentenceBolt", new com.msj.storm.kafka.SentenceBolt(), 1).globalGrouping("KafkaSpout"); |
| 49 | + builder.setBolt("PrinterBolt", new com.msj.storm.kafka.PrinterBolt(), 1).globalGrouping("SentenceBolt"); |
| 50 | + |
| 51 | + |
| 52 | + // create an instance of LocalCluster class for executing topology in local mode. |
| 53 | + LocalCluster cluster = new LocalCluster(); |
| 54 | + Config conf = new Config(); |
| 55 | + |
| 56 | + // Submit topology for execution |
| 57 | + cluster.submitTopology("KafkaToplogy", conf, builder.createTopology()); |
| 58 | + |
| 59 | + |
| 60 | + try { |
| 61 | + // Wait for some time before exiting |
| 62 | + System.out.println("Waiting to consume from kafka"); |
| 63 | + Thread.sleep(10000); |
| 64 | + } catch (Exception exception) { |
| 65 | + System.out.println("Thread interrupted exception : " + exception); |
| 66 | + } |
| 67 | + |
| 68 | + // kill the KafkaTopology |
| 69 | + cluster.killTopology("KafkaToplogy"); |
| 70 | + |
| 71 | + // shut down the storm test cluster |
| 72 | + cluster.shutdown(); |
| 73 | + } |
| 74 | +} |
0 commit comments