Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

时间:2024-01-18 23:19:08

前期博客

Storm编程入门API系列之Storm的Topology默认Workers、默认executors和默认tasks数目

Storm编程入门API系列之Storm的Topology多个Workers数目控制实现

继续编写

  StormTopologyMoreExecutor.java

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

package zhouls.bigdata.stormDemo;

import java.util.Map;

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.spout.SpoutOutputCollector;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import org.apache.storm.utils.Utils; public class StormTopologyMoreExecutor { public static class MySpout extends BaseRichSpout{
private Map conf;
private TopologyContext context;
private SpoutOutputCollector collector;
public void open(Map conf, TopologyContext context,
SpoutOutputCollector collector) {
this.conf = conf;
this.collector = collector;
this.context = context;
} int num = ;
public void nextTuple() {
num++;
System.out.println("spout:"+num);
this.collector.emit(new Values(num));
Utils.sleep();
} public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("num"));
} } public static class MyBolt extends BaseRichBolt{ private Map stormConf;
private TopologyContext context;
private OutputCollector collector;
public void prepare(Map stormConf, TopologyContext context,
OutputCollector collector) {
this.stormConf = stormConf;
this.context = context;
this.collector = collector;
} public void execute(Tuple input) {
Integer num = input.getIntegerByField("num");
System.out.println("线程id:"+Thread.currentThread().getId()+",接收的值为:"+num);
} public void declareOutputFields(OutputFieldsDeclarer declarer) { } } public static void main(String[] args) {
TopologyBuilder topologyBuilder = new TopologyBuilder();
String spout_id = MySpout.class.getSimpleName();
String bolt_id = MyBolt.class.getSimpleName(); topologyBuilder.setSpout(spout_id, new MySpout());
topologyBuilder.setBolt(bolt_id, new MyBolt(),).shuffleGrouping(spout_id); Config config = new Config();
String topology_name = StormTopologyMoreExecutor.class.getSimpleName();
if(args.length==){
//在本地运行
LocalCluster localCluster = new LocalCluster();
localCluster.submitTopology(topology_name, config, topologyBuilder.createTopology());
}else{
//在集群运行
try {
StormSubmitter.submitTopology(topology_name, config, topologyBuilder.createTopology());
} catch (AlreadyAliveException e) {
e.printStackTrace();
} catch (InvalidTopologyException e) {
e.printStackTrace();
} catch (AuthorizationException e) {
e.printStackTrace();
}
} } }

  打jar包

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

提交作业之前

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

[hadoop@master apache-storm-1.0.]$ pwd
/home/hadoop/app/apache-storm-1.0.
[hadoop@master apache-storm-1.0.]$ ll
total
drwxrwxr-x hadoop hadoop May : bin
-rw-r--r-- hadoop hadoop Jul CHANGELOG.md
drwxrwxr-x hadoop hadoop Jul : conf
drwxrwxr-x hadoop hadoop Jul examples
drwxrwxr-x hadoop hadoop May : external
drwxrwxr-x hadoop hadoop Jul extlib
drwxrwxr-x hadoop hadoop Jul extlib-daemon
drwxrwxr-x hadoop hadoop Jul : jar
drwxrwxr-x hadoop hadoop May : lib
-rw-r--r-- hadoop hadoop Jul LICENSE
drwxrwxr-x hadoop hadoop May : log4j2
drwxrwxr-x hadoop hadoop May : logs
-rw-r--r-- hadoop hadoop Jul NOTICE
drwxrwxr-x hadoop hadoop May : public
-rw-r--r-- hadoop hadoop Jul README.markdown
-rw-r--r-- hadoop hadoop Jul RELEASE
-rw-r--r-- hadoop hadoop Jul SECURITY.md
[hadoop@master apache-storm-1.0.]$ bin/storm jar jar/StormTopologyMoreExecutor.jar zhouls.bigdata.stormDemo.StormTopologyMoreExecutor aaa
Running: /home/hadoop/app/jdk/bin/java -client -Ddaemon.name= -Dstorm.options= -Dstorm.home=/home/hadoop/app/apache-storm-1.0. -Dstorm.log.dir=/home/hadoop/app/apache-storm-1.0./logs -Djava.library.path=/usr/local/lib:/opt/local/lib:/usr/lib -Dstorm.conf.file= -cp /home/hadoop/app/apache-storm-1.0./lib/log4j-api-2.1.jar:/home/hadoop/app/apache-storm-1.0./lib/kryo-3.0..jar:/home/hadoop/app/apache-storm-1.0./lib/storm-rename-hack-1.0..jar:/home/hadoop/app/apache-storm-1.0./lib/log4j-core-2.1.jar:/home/hadoop/app/apache-storm-1.0./lib/slf4j-api-1.7..jar:/home/hadoop/app/apache-storm-1.0./lib/minlog-1.3..jar:/home/hadoop/app/apache-storm-1.0./lib/objenesis-2.1.jar:/home/hadoop/app/apache-storm-1.0./lib/clojure-1.7..jar:/home/hadoop/app/apache-storm-1.0./lib/servlet-api-2.5.jar:/home/hadoop/app/apache-storm-1.0./lib/log4j-slf4j-impl-2.1.jar:/home/hadoop/app/apache-storm-1.0./lib/log4j-over-slf4j-1.6..jar:/home/hadoop/app/apache-storm-1.0./lib/storm-core-1.0..jar:/home/hadoop/app/apache-storm-1.0./lib/disruptor-3.3..jar:/home/hadoop/app/apache-storm-1.0./lib/asm-5.0..jar:/home/hadoop/app/apache-storm-1.0./lib/reflectasm-1.10..jar:jar/StormTopologyMoreExecutor.jar:/home/hadoop/app/apache-storm-1.0./conf:/home/hadoop/app/apache-storm-1.0./bin -Dstorm.jar=jar/StormTopologyMoreExecutor.jar zhouls.bigdata.stormDemo.StormTopologyMoreExecutor aaa
[main] INFO o.a.s.StormSubmitter - Generated ZooKeeper secret payload for MD5-digest: -:-
[main] INFO o.a.s.s.a.AuthUtils - Got AutoCreds []
[main] INFO o.a.s.StormSubmitter - Uploading topology jar jar/StormTopologyMoreExecutor.jar to assigned location: /home/hadoop/data/storm/nimbus/inbox/stormjar-83ebee61--4ab5-aff7-e9fcf4560f42.jar
[main] INFO o.a.s.StormSubmitter - Successfully uploaded topology jar to assigned location: /home/hadoop/data/storm/nimbus/inbox/stormjar-83ebee61--4ab5-aff7-e9fcf4560f42.jar
[main] INFO o.a.s.StormSubmitter - Submitting topology StormTopologyMoreExecutor in distributed mode with conf {"storm.zookeeper.topology.auth.scheme":"digest","storm.zookeeper.topology.auth.payload":"-7700164527916050772:-9124174655622273375"}
[main] INFO o.a.s.StormSubmitter - Finished submitting topology: StormTopologyMoreExecutor
[hadoop@master apache-storm-1.0.]$

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

  

  为什么,会是如上的数字呢?大家要学,就要深入去学和理解。

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

  因为,我之前运行的StormTopologyMoreWorker没有停掉

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

  为什么,会是如上的数字呢?大家要学,就要深入去学和理解。

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现

  

  即,以下就是它的3个Executor

Storm编程入门API系列之Storm的Topology多个Executors数目控制实现