This article is based on the original two articles on the basis of expansion
Storm entry (Integrated Kafkabolt) Portal: http://blog.csdn.net/yl3395017/article/details/77452604
kafkaspout Update is mainly to build kafkaspout basic configuration operation
/** * Build Kafkaspout */private static void Builtkafkaspout (Topologybuilder builder) {try {
Brokerhosts brokerhosts = new Zkhosts ("10.2.4.12:2181,10.2.4.13:2181,10.2.4.14:2181");
Spoutconfig spoutconf = new Spoutconfig (brokerhosts, "Test_rce_yjd", "/storm/data/2016122615", "logFramework");
Spoutconf.scheme = new Schemeasmultischeme (new Stringscheme ());
Spoutconf.zkport = 2181; Spoutconf.ignorezkoffsets = false;
Each consumption starts from scratch and is used for performance testing list<string> servers = new arraylist<string> ();
String zk_servers = "10.2.4.12,10.2.4.13,10.2.4.14";
if (zk_servers! = null) {string[] arr = Zk_servers.split (","); for (int i = 0; i < arr.length; i++) {if (! ("").
Equals (Arr[i])) {Servers.add (arr[i]);
}}} spoutconf.zkservers = Servers; Builder.setspout ("Kafka-spout", New Kafkaspout (spoutconf), 1); } catch (Exception e) {}}
topology update build spout
Package com.storm.topology;
Import Com.storm.bolt.BoltA;
Import Com.storm.bolt.KafkaMsgBolt;
Import Com.storm.spout.SpoutA;
Import Org.apache.storm.Config;
Import Org.apache.storm.LocalCluster;
Import Org.apache.storm.StormSubmitter;
Import org.apache.storm.generated.AlreadyAliveException;
Import org.apache.storm.generated.AuthorizationException;
Import org.apache.storm.generated.InvalidTopologyException;
Import org.apache.storm.kafka.*;
Import Org.apache.storm.kafka.bolt.KafkaBolt;
Import Org.apache.storm.kafka.bolt.mapper.FieldNameBasedTupleToKafkaMapper;
Import Org.apache.storm.kafka.bolt.selector.DefaultTopicSelector;
Import Org.apache.storm.spout.SchemeAsMultiScheme;
Import Org.apache.storm.topology.TopologyBuilder;
Import Org.apache.storm.utils.Utils;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.Properties;
/** * Created with IntelliJ idea. * User:administrator * date:17-8-21 * Time: Morning 10:54 * To change this template use File | Settings | File TemplAtes.
*/public class Topology {public static Boolean Kafka = True;
public static void Main (String args[]) throws Authorizationexception, Alreadyaliveexception, invalidtopologyexception {
Topologybuilder builder = new Topologybuilder ();
if (Kafka) {//kafkaspout builtkafkaspout (builder);
Builder.setbolt ("Bolta", New Bolta (), 1). shufflegrouping ("Kafka-spout");
}else{//General spout Builder.setspout ("Spouta", New Spouta (), 1);
Builder.setbolt ("Bolta", New Bolta (), 1). shufflegrouping ("Spouta");
Build Kafkabolt Builtkafkabolt (builder);
} config conf = new config ();
Conf.setdebug (TRUE);
if (args! = null && args.length > 0) {conf.setnumworkers (3);
Stormsubmitter.submittopologywithprogressbar (Args[0], conf, builder.createtopology ()); } else {Localcluster cluster = new LOCALCLUSter ();
Cluster.submittopology ("Soc", Conf, Builder.createtopology ()); }
}
Overall Code
Package com.storm.topology;
Import Com.storm.bolt.BoltA;
Import Com.storm.bolt.KafkaMsgBolt;
Import Com.storm.spout.SpoutA;
Import Org.apache.storm.Config;
Import Org.apache.storm.LocalCluster;
Import Org.apache.storm.StormSubmitter;
Import org.apache.storm.generated.AlreadyAliveException;
Import org.apache.storm.generated.AuthorizationException;
Import org.apache.storm.generated.InvalidTopologyException;
Import org.apache.storm.kafka.*;
Import Org.apache.storm.kafka.bolt.KafkaBolt;
Import Org.apache.storm.kafka.bolt.mapper.FieldNameBasedTupleToKafkaMapper;
Import Org.apache.storm.kafka.bolt.selector.DefaultTopicSelector;
Import Org.apache.storm.spout.SchemeAsMultiScheme;
Import Org.apache.storm.topology.TopologyBuilder;
Import Org.apache.storm.utils.Utils;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.Properties;
/** * Created with IntelliJ idea. * User:administrator * date:17-8-21 * Time: Morning 10:54 * To change this template use File | Settings | File TemplAtes.
*/public class Topology {public static Boolean Kafka = True;
public static void Main (String args[]) throws Authorizationexception, Alreadyaliveexception, invalidtopologyexception {
Topologybuilder builder = new Topologybuilder ();
if (Kafka) {//kafkaspout builtkafkaspout (builder);
Builder.setbolt ("Bolta", New Bolta (), 1). shufflegrouping ("Kafka-spout");
}else{//General spout Builder.setspout ("Spouta", New Spouta (), 1);
Builder.setbolt ("Bolta", New Bolta (), 1). shufflegrouping ("Spouta");
Build Kafkabolt Builtkafkabolt (builder);
} config conf = new config ();
Conf.setdebug (TRUE);
if (args! = null && args.length > 0) {conf.setnumworkers (3);
Stormsubmitter.submittopologywithprogressbar (Args[0], conf, builder.createtopology ()); } else {Localcluster cluster = new LOCALCLUSter ();
Cluster.submittopology ("Soc", Conf, Builder.createtopology ());
Utils.sleep (20000);
Cluster.killtopology ("Soc");
Cluster.shutdown (); }}/** * Build Kafkabolt * metadata.broker.list=10.2.4.13:9092,10.2.4.14:9092,10.2.4.12:9092 * BOOTST rap.servers=10.2.4.13:9092,10.2.4.14:9092,10.2.4.12:9092 * Producer.type=async * request.required.ack=1 * s Erializer.class=kafka.serializer.stringencoder * key.serializer= Org.apache.kafka.common.serialization.StringSerializer * value.serializer= Org.apache.kafka.common.serialization.StringSerializer * sendtopic=test * */private static void Builtkafkabol
T (Topologybuilder builder) {//kafka producer config Properties prop = new properties ();
Prop.put ("Metadata.broker.list", "10.2.4.13:9092,10.2.4.14:9092,10.2.4.12:9092");
Prop.put ("Bootstrap.servers", "10.2.4.13:9092,10.2.4.14:9092,10.2.4.12:9092"); Prop.put ("Producer.type", "async");
Prop.put ("Request.required.acks", "1");
Prop.put ("Serializer.class", "Kafka.serializer.StringEncoder");
Prop.put ("Key.serializer", "Org.apache.kafka.common.serialization.StringSerializer");
Prop.put ("Value.serializer", "Org.apache.kafka.common.serialization.StringSerializer");
Kafkabolt Kafkabolt bolt = new Kafkabolt ();
Bolt.withtopicselector (New Defaulttopicselector ("TEST_RCE_YJD"));
Bolt.withproducerproperties (prop);
Bolt.withtupletokafkamapper (New Fieldnamebasedtupletokafkamapper ());
Build Kafkabolt Builder.setbolt ("Msgsentencebolt", New Kafkamsgbolt ()). Shufflegrouping ("Bolta");
Forwards Kafka message Builder.setbolt ("Forwardtokafka", Bolt). Shufflegrouping ("Msgsentencebolt");
}/** * Build Kafkaspout */private static void Builtkafkaspout (Topologybuilder builder) {try { Brokerhosts brokerhosts = new Zkhosts ("10.2.4.12:2181,10.2.4.13:2181,10.2.4.14:2181 ");
Spoutconfig spoutconf = new Spoutconfig (brokerhosts, "Test_rce_yjd", "/storm/data/2016122615", "logFramework");
Spoutconf.scheme = new Schemeasmultischeme (new Stringscheme ());
Spoutconf.zkport = 2181; Spoutconf.ignorezkoffsets = false;
Each consumption starts from scratch and is used for performance testing list<string> servers = new arraylist<string> ();
String zk_servers = "10.2.4.12,10.2.4.13,10.2.4.14";
if (zk_servers! = null) {string[] arr = Zk_servers.split (","); for (int i = 0; i < arr.length; i++) {if (! ("").
Equals (Arr[i])) {Servers.add (arr[i]);
}}} spoutconf.zkservers = Servers;
Builder.setspout ("Kafka-spout", New Kafkaspout (spoutconf), 1);
} catch (Exception e) {}}}
Results