kafka storm 整合

kafka版本:kafka_2.11-1.1.1

storm版本:apache-storm-1.2.2

pom:

 
        
            org.apache.storm
            storm-kafka-client
            1.2.2
        
        
            org.apache.kafka
            kafka-clients
            1.0.0
        
        
            org.apache.storm
            storm-core
            1.2.2
             
              provided
        
    
     
         
             
             
                 org.apache.maven.plugins
                 maven-resources-plugin
                 2.7
                 
                     UTF-8
                 
             
             
             
                 org.apache.maven.plugins
                 maven-compiler-plugin
                 3.2
                 
                     1.8
                     1.8
                     UTF-8
                 
             
             
                 org.apache.maven.plugins
                 maven-jar-plugin
                 2.4
             
             
                 org.apache.maven.plugins
                 maven-assembly-plugin
                 2.4
                 
                     
                         jar-with-dependencies
                     
                     
                         
                             cn.itcast.kafka.Kafka2Storm
                         
                     
                 
                 
                     
                         make-assembly
                         package
                         
                             single
                       
                     
                 
             
         
     
package test.stormKafka;

import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.generated.AlreadyAliveException;
import org.apache.storm.generated.AuthorizationException;
import org.apache.storm.generated.InvalidTopologyException;
import org.apache.storm.kafka.spout.KafkaSpout;
import org.apache.storm.kafka.spout.KafkaSpoutConfig;
import org.apache.storm.topology.TopologyBuilder;

public class TopologyClass {

	public static void main(String[] args)
			throws AlreadyAliveException, InvalidTopologyException, AuthorizationException {

		TopologyBuilder topologyBuilder = new TopologyBuilder();

		KafkaSpoutConfig.Builder kafkaBuilder = KafkaSpoutConfig.builder("geng2:9092,geng3:9092",
				"mytest");
		// 设置kafka属于哪个组
		kafkaBuilder.setProp("group.id", "mytest");
		// 创建kafkaspoutConfig
		KafkaSpoutConfig build = kafkaBuilder.build();
		// 通过kafkaspoutConfig获得kafkaspout
		KafkaSpout kafkaSpout = new KafkaSpout(build);

		topologyBuilder.setSpout("kafkaSpout", kafkaSpout);
		
		topologyBuilder.setBolt("valueBolt", new KafkaBolt(), 1).shuffleGrouping("kafkaSpout");
	//topologyBuilder.setBolt("mybolt2", new MyBolt(), 1).shuffleGrouping("kafkaSpout");
		// 启动topology的配置信息
		Config config = new Config();
		// 定义集群分配多少个工作进程来执行这个topology
		config.setNumWorkers(3);
		config.setNumAckers(1);

		LocalCluster localCluster = new LocalCluster();
		localCluster.submitTopology("kafkaStomrTopology", config, topologyBuilder.createTopology());
		// 集群模式提交topology
		// StormSubmitter.submitTopologyWithProgressBar("mytest", config,
		// topologyBuilder.createTopology());
	}
}
package test.stormKafka;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Random;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

public class KafkaBolt extends BaseRichBolt {

	private OutputCollector collector;

	public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {

		this.collector = collector;
	}

	public void execute(Tuple input) {
		System.out.println(input.toString());
		/*
		 * int nextInt = new Random().nextInt(100); File f = new
		 * File("/home/geng/mytest-"+nextInt); OutputStream out; try { out = new
		 * FileOutputStream(f); out.write(input.getString(4).getBytes());
		 * out.flush(); out.close(); } catch (FileNotFoundException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } catch (IOException
		 * e) { // TODO Auto-generated catch block e.printStackTrace(); }
		 */

		collector.ack(input);

	}

	public void declareOutputFields(OutputFieldsDeclarer declarer) {
		// TODO Auto-generated method stub

	}

}

 

你可能感兴趣的:(大数据)