storm整合kafka

maven


    4.0.0
    storm-kafka-mysql
    storm-kafka-mysql
    0.0.1-SNAPSHOT
    jar
    storm-kafka-mysql
    
    
        UTF-8
    
    
        
            javax
            javaee-api
            8.0
            provided
        
        
            org.glassfish.web
            javax.servlet.jsp.jstl
            1.2.2
        


        
            org.apache.storm
            storm-core
            0.9.5
            
        

        
            org.apache.storm
            storm-kafka
            0.9.5
            
        


        
            org.apache.kafka
            kafka_2.11
            0.8.2.0
            
                
                    org.apache.zookeeper
                    zookeeper
                
                
                    log4j
                    log4j
                
            
        
        
            mysql
            mysql-connector-java
            5.1.31
        

    
    
        
            
                maven-compiler-plugin
                2.3.2
                
                    1.7
                    1.7
                
            
            
                
                
                
                    
                    
                
            
        
    

MyKafkaTopology

package com.neusoft;
 
import java.util.Arrays;

import storm.kafka.BrokerHosts;
import storm.kafka.KafkaSpout;
import storm.kafka.SpoutConfig;
import storm.kafka.StringScheme;
import storm.kafka.ZkHosts;
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.generated.AlreadyAliveException;
import backtype.storm.generated.InvalidTopologyException;
import backtype.storm.spout.SchemeAsMultiScheme;
import backtype.storm.topology.TopologyBuilder;
public class MyKafkaTopology {
 
     public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException, InterruptedException {
          String zks = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
          String topic = "orderMq";
         // String zkRoot = "/opt/modules/app/zookeeper/zkdata"; // default zookeeper root configuration for storm
          String id = "wordtest";
          
          BrokerHosts brokerHosts = new ZkHosts(zks);
          SpoutConfig spoutConf = new SpoutConfig(brokerHosts, topic, "", id);
          spoutConf.scheme = new SchemeAsMultiScheme(new StringScheme());
          spoutConf.forceFromStart = false;
          spoutConf.zkServers = Arrays.asList(new String[] {"hadoop01", "hadoop02", "hadoop03"});
          spoutConf.zkPort = 2181;
          
          TopologyBuilder builder = new TopologyBuilder();
          builder.setSpout("kafka-reader", new KafkaSpout(spoutConf), 2); // Kafka我们创建了一个2分区的Topic,这里并行度设置为2
          builder.setBolt("print-bolt", new PrintBolt(), 2).shuffleGrouping("kafka-reader");
          
          Config conf = new Config();
          String name = MyKafkaTopology.class.getSimpleName();
          if (args != null && args.length > 0) {
               // Nimbus host name passed from command line
               conf.put(Config.NIMBUS_HOST, args[0]);
               conf.setNumWorkers(3);
               StormSubmitter.submitTopologyWithProgressBar(name, conf, builder.createTopology());
          } 
          else {
               conf.setMaxTaskParallelism(3);
               LocalCluster cluster = new LocalCluster();
               cluster.submitTopology(name, conf, builder.createTopology());
//               Thread.sleep(60000);
//               cluster.killTopology(name);
//               cluster.shutdown();
               
//               StormSubmitter.submitTopology(name, conf, builder.createTopology());
          }
     }
}

bolt

package com.neusoft;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseBasicBolt;

import backtype.storm.tuple.Tuple;


public class PrintBolt extends BaseBasicBolt {

    public static final Log log = LogFactory.getLog(PrintBolt.class);
    
    public static final long serialVersionUID = 1L;

    @Override
    public void execute(Tuple input, BasicOutputCollector collector) {
        //获取上一个组件所声明的Field
    
        String print = input.getString(0);
        log.info("message: " + print);
        System.out.println("message is : " + print);

        //进行传递给下一个bolt
        //collector.emit(new Values(print));
        
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        //declarer.declare(new Fields("write"));
    }

}

你可能感兴趣的:(storm整合kafka)