Java Code Examples for org.apache.storm.LocalCluster#shutdown()
The following examples show how to use
org.apache.storm.LocalCluster#shutdown() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParserBoltTest.java From logparser with Apache License 2.0 | 6 votes |
@Test public void runRest() throws InterruptedException, NoSuchMethodException { TopologyBuilder builder = new TopologyBuilder(); // ---------- builder.setSpout("Spout", new TestApacheLogsSpout()); // ---------- HttpdLoglineParserBolt parserBolt = new HttpdLoglineParserBolt(TestCase.getLogFormat(), INPUT_FIELD_NAME, OUTPUT_FIELD_NAME); builder.setBolt("Parser", parserBolt, 1).shuffleGrouping("Spout"); // ---------- builder.setBolt("Printer", new ValidateOutput(), 1).shuffleGrouping("Parser"); // ---------- StormTopology topology = builder.createTopology(); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("Unit test", new HashMap<String, String>(), topology); Thread.sleep(10000L); // Run for 10 seconds cluster.killTopology("Unit test"); cluster.shutdown(); }
Example 2
Source File: ConfigurableTopology.java From storm-crawler with Apache License 2.0 | 6 votes |
/** Submits the topology under a specific name **/ protected int submit(String name, Config conf, TopologyBuilder builder) { // register Metadata for serialization with FieldsSerializer Config.registerSerialization(conf, Metadata.class); if (isLocal) { LocalCluster cluster = new LocalCluster(); cluster.submitTopology(name, conf, builder.createTopology()); if (ttl != -1) { Utils.sleep(ttl * 1000); cluster.shutdown(); } } else { try { StormSubmitter.submitTopology(name, conf, builder.createTopology()); } catch (Exception e) { e.printStackTrace(); return -1; } } return 0; }
Example 3
Source File: StormMain.java From chuidiang-ejemplos with GNU Lesser General Public License v3.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, AlreadyAliveException, InvalidTopologyException, AuthorizationException { TopologyBuilder builder = new TopologyBuilder(); builder.setSpout(DATA_GENERATOR, new ASpout()); builder.setBolt(DATA_CALCULATOR, new ABolt()).shuffleGrouping(DATA_GENERATOR); builder.setBolt(DATA_PRINTER, new DataPrinter()).shuffleGrouping(DATA_CALCULATOR).shuffleGrouping(DATA_GENERATOR); Config config = new Config(); LocalCluster cluster = new LocalCluster(); cluster.submitTopology(TOPOLOGY_NAME, config, builder.createTopology()); Thread.sleep(100000); cluster.killTopology(TOPOLOGY_NAME); cluster.shutdown(); }
Example 4
Source File: TopologyStarter.java From breeze with Apache License 2.0 | 6 votes |
@Override public void run() { properties.put(Config.TOPOLOGY_NAME, ID); Config config = stormConfig(properties); ApplicationContext spring = SingletonApplicationContext.loadXml(config, MAIN_CONTEXT); try { StormTopology topology = spring.getBean(ID, StormTopology.class); Properties systemProperties = System.getProperties(); if (systemProperties.containsKey(LOCAL_RUN_PARAM)) { String timeout = systemProperties.getProperty(LOCAL_RUN_PARAM); if (! hasText(timeout)) timeout = LOCAL_RUN_DEFAULT_TIMEOUT; long ms = 1000L * Integer.parseInt(timeout); LocalCluster cluster = new LocalCluster(); cluster.submitTopology(ID, config, topology); sleep(ms); cluster.shutdown(); } else StormSubmitter.submitTopology(ID, config, topology); } catch (Exception e) { e.printStackTrace(); exit(255); } }
Example 5
Source File: MultiSpoutExclamationTopology.java From incubator-heron with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("word0", new TestWordSpout(), 2); builder.setSpout("word1", new TestWordSpout(), 2); builder.setSpout("word2", new TestWordSpout(), 2); builder.setBolt("exclaim1", new ExclamationBolt(), 2) .shuffleGrouping("word0") .shuffleGrouping("word1") .shuffleGrouping("word2"); Config conf = new Config(); conf.setDebug(true); conf.setMaxSpoutPending(10); conf.put(Config.TOPOLOGY_WORKER_CHILDOPTS, "-XX:+HeapDumpOnOutOfMemoryError"); if (args != null && args.length > 0) { conf.setNumWorkers(3); StormSubmitter.submitTopology(args[0], conf, builder.createTopology()); } else { LocalCluster cluster = new LocalCluster(); cluster.submitTopology("test", conf, builder.createTopology()); Utils.sleep(10000); cluster.killTopology("test"); cluster.shutdown(); } }
Example 6
Source File: App.java From springBoot-study with Apache License 2.0 | 5 votes |
public static void main(String[] args) { //定义一个拓扑 TopologyBuilder builder=new TopologyBuilder(); //设置1个Executeor(线程),默认一个 builder.setSpout(test_spout, new TestSpout(),1); //shuffleGrouping:表示是随机分组 //设置1个Executeor(线程),和两个task builder.setBolt(test_bolt, new TestBolt(),1).setNumTasks(1).shuffleGrouping(test_spout); //fieldsGrouping:表示是按字段分组 //设置1个Executeor(线程),和1个task builder.setBolt(test2_bolt, new Test2Bolt(),1).setNumTasks(1).fieldsGrouping(test_bolt, new Fields("count")); Config conf = new Config(); conf.put("test", "test"); try{ //运行拓扑 if(args !=null&&args.length>0){ //有参数时,表示向集群提交作业,并把第一个参数当做topology名称 System.out.println("运行远程模式"); StormSubmitter.submitTopology(args[0], conf, builder.createTopology()); } else{//没有参数时,本地提交 //启动本地模式 System.out.println("运行本地模式"); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("Word-counts" ,conf, builder.createTopology() ); Thread.sleep(20000); // //关闭本地集群 cluster.shutdown(); } }catch (Exception e){ e.printStackTrace(); } }
Example 7
Source File: NiFiStormTopology.java From nifi with Apache License 2.0 | 5 votes |
public static void main( String[] args ) { // Build a Site-To-Site client config for pulling data final SiteToSiteClientConfig inputConfig = new SiteToSiteClient.Builder() .url("http://localhost:8080/nifi") .portName("Data for Storm") .buildConfig(); // Build a Site-To-Site client config for pushing data final SiteToSiteClientConfig outputConfig = new SiteToSiteClient.Builder() .url("http://localhost:8080/nifi") .portName("Data from Storm") .buildConfig(); final int tickFrequencySeconds = 5; final NiFiDataPacketBuilder niFiDataPacketBuilder = new SimpleNiFiDataPacketBuilder(); final NiFiBolt niFiBolt = new NiFiBolt(outputConfig, niFiDataPacketBuilder, tickFrequencySeconds) //.withBatchSize(1) ; TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("nifiInput", new NiFiSpout(inputConfig)); builder.setBolt("nifiOutput", niFiBolt).shuffleGrouping("nifiInput"); // Submit the topology running in local mode Config conf = new Config(); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("test", conf, builder.createTopology()); Utils.sleep(90000); cluster.shutdown(); }
Example 8
Source File: ParserTopologyCLI.java From metron with Apache License 2.0 | 5 votes |
public static void main(String[] args) { try { Options options = new Options(); final CommandLine cmd = parse(options, args); if (cmd.hasOption("h")) { final HelpFormatter usageFormatter = new HelpFormatter(); usageFormatter.printHelp("ParserTopologyCLI", null, options, null, true); System.exit(0); } ParserTopologyCLI cli = new ParserTopologyCLI(); ParserTopologyBuilder.ParserTopology topology = cli.createParserTopology(cmd); String sensorTypes = ParserOptions.SENSOR_TYPES.get(cmd); String topologyName = sensorTypes.replaceAll(TOPOLOGY_OPTION_SEPARATOR, STORM_JOB_SEPARATOR); if (ParserOptions.TEST.has(cmd)) { topology.getTopologyConfig().put(Config.TOPOLOGY_DEBUG, true); LocalCluster cluster = new LocalCluster(); cluster.submitTopology(topologyName, topology.getTopologyConfig(), topology.getBuilder().createTopology()); Utils.sleep(300000); cluster.shutdown(); } else { StormSubmitter.submitTopology(topologyName, topology.getTopologyConfig(), topology.getBuilder().createTopology()); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
Example 9
Source File: ExclamationTopology.java From incubator-heron with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { TopologyBuilder builder = new TopologyBuilder(); int parallelism = 2; int spouts = parallelism; builder.setSpout("word", new TestWordSpout(Duration.ofMillis(50)), spouts); int bolts = 2 * parallelism; builder.setBolt("exclaim1", new ExclamationBolt(), bolts) .shuffleGrouping("word"); Config conf = new Config(); conf.setDebug(true); conf.setMaxSpoutPending(10); conf.setMessageTimeoutSecs(600); conf.put(Config.TOPOLOGY_WORKER_CHILDOPTS, "-XX:+HeapDumpOnOutOfMemoryError"); if (args != null && args.length > 0) { conf.setNumWorkers(parallelism); StormSubmitter.submitTopology(args[0], conf, builder.createTopology()); } else { System.out.println("Topology name not provided as an argument, running in simulator mode."); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("test", conf, builder.createTopology()); Utils.sleep(10000); cluster.killTopology("test"); cluster.shutdown(); } }
Example 10
Source File: StormKafkaProcess.java From BigData with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) throws InterruptedException, InvalidTopologyException, AuthorizationException, AlreadyAliveException { String topologyName = "TSAS";// 元组名 // Zookeeper主机地址,会自动选取其中一个 ZkHosts zkHosts = new ZkHosts("192.168.230.128:2181,192.168.230.129:2181,192.168.230.131:2181"); String topic = "trademx"; String zkRoot = "/storm";// storm在Zookeeper上的根路径 String id = "tsaPro"; // 创建SpoutConfig对象 SpoutConfig spontConfig = new SpoutConfig(zkHosts, topic, zkRoot, id); TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("kafka", new KafkaSpout(spontConfig), 2); builder.setBolt("AccBolt", new AccBolt()).shuffleGrouping("kafka"); builder.setBolt("ToDbBolt", new ToDbBolt()).shuffleGrouping("AccBolt"); Config config = new Config(); config.setDebug(false); if (args.length == 0) { // 本地运行,用于测试 LocalCluster localCluster = new LocalCluster(); localCluster.submitTopology(topologyName, config, builder.createTopology()); Thread.sleep(1000 * 3600); localCluster.killTopology(topologyName); localCluster.shutdown(); } else { // 提交至集群运行 StormSubmitter.submitTopology(topologyName, config, builder.createTopology()); } }
Example 11
Source File: AppMain.java From storm_spring_boot_demo with MIT License | 5 votes |
/** * 用于debug * @param name * @param builder * @throws InterruptedException */ private static void localSubmit(String name,TopologyBuilder builder, Config conf) throws InterruptedException { conf.setDebug(true); conf.setMaxTaskParallelism(3); LocalCluster cluster = new LocalCluster(); cluster.submitTopology(name, conf, builder.createTopology()); Thread.sleep(100000); cluster.shutdown(); }
Example 12
Source File: KafkaStormWordCountTopology.java From Building-Data-Streaming-Applications-with-Apache-Kafka with MIT License | 5 votes |
public static void main(String[] args) throws Exception { String zkConnString = "localhost:2181"; String topic = "words"; BrokerHosts hosts = new ZkHosts(zkConnString); SpoutConfig kafkaSpoutConfig = new SpoutConfig(hosts, topic, "/" + topic, "wordcountID"); kafkaSpoutConfig.startOffsetTime = kafka.api.OffsetRequest.EarliestTime(); kafkaSpoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme()); TopologyBuilder topologyBuilder = new TopologyBuilder(); topologyBuilder.setSpout("kafkaspout", new KafkaSpout(kafkaSpoutConfig)); topologyBuilder.setBolt("stringsplit", new StringToWordsSpliterBolt()).shuffleGrouping("kafkaspout"); topologyBuilder.setBolt("counter", new WordCountCalculatorBolt()).shuffleGrouping("stringsplit"); Config config = new Config(); config.setDebug(true); if (args != null && args.length > 1) { config.setNumWorkers(3); StormSubmitter.submitTopology(args[1], config, topologyBuilder.createTopology()); } else { // Cap the maximum number of executors that can be spawned // for a component to 3 config.setMaxTaskParallelism(3); // LocalCluster is used to run locally LocalCluster cluster = new LocalCluster(); cluster.submitTopology("KafkaLocal", config, topologyBuilder.createTopology()); // sleep try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block cluster.killTopology("KafkaToplogy"); cluster.shutdown(); } cluster.shutdown(); } }
Example 13
Source File: App.java From java-study with Apache License 2.0 | 5 votes |
public static void main(String[] args) { //定义一个拓扑 TopologyBuilder builder=new TopologyBuilder(); //设置两个Executeor(线程),默认一个 builder.setSpout(test_spout, new TestSpout(),2); //shuffleGrouping:表示是随机分组 //设置两个Executeor(线程),和两个task builder.setBolt(test_bolt, new TestBolt(),2).setNumTasks(2).shuffleGrouping(test_spout); //fieldsGrouping:表示是按字段分组 //设置两个Executeor(线程),和两个task builder.setBolt(test2_bolt, new Test2Bolt(),2).setNumTasks(2).fieldsGrouping(test_bolt, new Fields("count")); Config conf = new Config(); conf.put("test", "test"); try{ //运行拓扑 if(args !=null&&args.length>0){ //有参数时,表示向集群提交作业,并把第一个参数当做topology名称 System.out.println("运行远程模式"); StormSubmitter.submitTopology(args[0], conf, builder.createTopology()); } else{//没有参数时,本地提交 //启动本地模式 System.out.println("运行本地模式"); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("Word-counts" ,conf, builder.createTopology() ); Thread.sleep(20000); // //关闭本地集群 cluster.shutdown(); } }catch (Exception e){ e.printStackTrace(); } }
Example 14
Source File: WordCountApp.java From java-study with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws InterruptedException, AlreadyAliveException, InvalidTopologyException { //定义拓扑 TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("word-reader" , new WordReader()); builder.setBolt("word-normalizer" , new WordNormalizer()).shuffleGrouping("word-reader" ); builder.setBolt("word-counter" , new WordCounter()).fieldsGrouping("word-normalizer" , new Fields("word")); StormTopology topology = builder.createTopology(); //配置 Config conf = new Config(); String fileName ="words.txt" ; conf.put("fileName" , fileName ); conf.setDebug(false); //运行拓扑 System.out.println("开始..."); if(args !=null&&args.length>0){ //有参数时,表示向集群提交作业,并把第一个参数当做topology名称 System.out.println("远程模式"); try { StormSubmitter.submitTopology(args[0], conf, topology); } catch (AuthorizationException e) { e.printStackTrace(); } } else{//没有参数时,本地提交 //启动本地模式 System.out.println("本地模式"); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("Getting-Started-Topologie" , conf , topology ); Thread.sleep(5000); //关闭本地集群 cluster.shutdown(); } System.out.println("结束"); }
Example 15
Source File: LocalDRPCTopology.java From 163-bigdate-note with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) { LinearDRPCTopologyBuilder builder = new LinearDRPCTopologyBuilder("addUser"); builder.addBolt(new MyBolt()); LocalCluster localCluster = new LocalCluster(); LocalDRPC drpc = new LocalDRPC(); localCluster.submitTopology("local-drpc", new Config(), builder.createLocalTopology(drpc)); String result = drpc.execute("addUser", "zhangsan"); System.out.println("From client: " + result); localCluster.shutdown(); drpc.shutdown(); }
Example 16
Source File: LogLevelCountTopology.java From nifi-streaming-examples with Apache License 2.0 | 4 votes |
public static void main( String[] args ) throws Exception { String propertiesFile = DEFAULT_PROPERTIES_FILE; if (args != null && args.length == 1 && args[0] != null) { propertiesFile = args[0]; } LogLevelCountProperties props = new LogLevelCountProperties(propertiesFile); int windowMillis = props.getStormWindowMillis(); double rateThreshold = props.getStormRateThreshold(); // Build the spout for pulling data from NiFi and pull out the log level into a tuple field NiFiSpout niFiSpout = new NiFiSpout(getSourceConfig(props), Collections.singletonList(props.getLogLevelAttribute())); // Build the bolt for counting log levels over a tumbling window BaseWindowedBolt logLevelWindowBolt = new LogLevelWindowBolt(props.getLogLevelAttribute()) .withTumblingWindow(new BaseWindowedBolt.Duration(windowMillis, TimeUnit.MILLISECONDS)); // Build the bolt for pushing results back to NiFi NiFiDataPacketBuilder dictionaryBuilder = new DictionaryBuilder(windowMillis, rateThreshold); NiFiBolt niFiBolt = new NiFiBolt(getSinkConfig(props), dictionaryBuilder, 10).withBatchSize(1); // Build the topology of NiFiSpout -> LogLevelWindowBolt -> NiFiBolt TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("nifiInput", niFiSpout); builder.setBolt("logLevels", logLevelWindowBolt).shuffleGrouping("nifiInput"); builder.setBolt("nifiOutput", niFiBolt).shuffleGrouping("logLevels"); // Submit the topology Config conf = new Config(); conf.setDebug(true); // Need to set the message timeout to twice the window size in seconds conf.setMessageTimeoutSecs((props.getStormWindowMillis()/1000) * 2); if (args != null && args.length > 0) { conf.setNumWorkers(3); StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createTopology()); } else { LocalCluster cluster = new LocalCluster(); cluster.submitTopology("log-levels", conf, builder.createTopology()); Utils.sleep(130000); cluster.killTopology("log-levels"); cluster.shutdown(); } }
Example 17
Source File: IPFraudDetectionTopology.java From Building-Data-Streaming-Applications-with-Apache-Kafka with MIT License | 4 votes |
public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException, AuthorizationException { Intialize(args[0]); logger.info("Successfully loaded Configuration "); BrokerHosts hosts = new ZkHosts(zkhost); SpoutConfig spoutConfig = new SpoutConfig(hosts, inputTopic, "/" + KafkaBroker, consumerGroup); spoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme()); spoutConfig.startOffsetTime = kafka.api.OffsetRequest.EarliestTime(); KafkaSpout kafkaSpout = new KafkaSpout(spoutConfig); String[] partNames = {"status_code"}; String[] colNames = {"date", "request_url", "protocol_type", "status_code"}; DelimitedRecordHiveMapper mapper = new DelimitedRecordHiveMapper().withColumnFields(new Fields(colNames)) .withPartitionFields(new Fields(partNames)); HiveOptions hiveOptions; //make sure you change batch size and all paramtere according to requirement hiveOptions = new HiveOptions(metaStoreURI, dbName, tblName, mapper).withTxnsPerBatch(250).withBatchSize(2) .withIdleTimeout(10).withCallTimeout(10000000); logger.info("Creating Storm Topology"); TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("KafkaSpout", kafkaSpout, 1); builder.setBolt("frauddetect", new FraudDetectorBolt()).shuffleGrouping("KafkaSpout"); builder.setBolt("KafkaOutputBolt", new IPFraudKafkaBolt(zkhost, "kafka.serializer.StringEncoder", KafkaBroker, outputTopic), 1) .shuffleGrouping("frauddetect"); builder.setBolt("HiveOutputBolt", new IPFraudHiveBolt(), 1).shuffleGrouping("frauddetect"); builder.setBolt("HiveBolt", new HiveBolt(hiveOptions)).shuffleGrouping("HiveOutputBolt"); Config conf = new Config(); if (args != null && args.length > 1) { conf.setNumWorkers(3); logger.info("Submiting topology to storm cluster"); StormSubmitter.submitTopology(args[1], conf, builder.createTopology()); } else { // Cap the maximum number of executors that can be spawned // for a component to 3 conf.setMaxTaskParallelism(3); // LocalCluster is used to run locally LocalCluster cluster = new LocalCluster(); logger.info("Submitting topology to local cluster"); cluster.submitTopology("KafkaLocal", conf, builder.createTopology()); // sleep try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block logger.error("Exception ocuured" + e); cluster.killTopology("KafkaToplogy"); logger.info("Shutting down cluster"); cluster.shutdown(); } cluster.shutdown(); } }
Example 18
Source File: AdvertisingTopology.java From streaming-benchmarks with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { TopologyBuilder builder = new TopologyBuilder(); Options opts = new Options(); opts.addOption("conf", true, "Path to the config file."); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(opts, args); String configPath = cmd.getOptionValue("conf"); Map commonConfig = Utils.findAndReadConfigFile(configPath, true); String zkServerHosts = joinHosts((List<String>)commonConfig.get("zookeeper.servers"), Integer.toString((Integer)commonConfig.get("zookeeper.port"))); String redisServerHost = (String)commonConfig.get("redis.host"); String kafkaTopic = (String)commonConfig.get("kafka.topic"); int kafkaPartitions = ((Number)commonConfig.get("kafka.partitions")).intValue(); int workers = ((Number)commonConfig.get("storm.workers")).intValue(); int ackers = ((Number)commonConfig.get("storm.ackers")).intValue(); int cores = ((Number)commonConfig.get("process.cores")).intValue(); int parallel = Math.max(1, cores/7); ZkHosts hosts = new ZkHosts(zkServerHosts); SpoutConfig spoutConfig = new SpoutConfig(hosts, kafkaTopic, "/" + kafkaTopic, UUID.randomUUID().toString()); spoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme()); KafkaSpout kafkaSpout = new KafkaSpout(spoutConfig); builder.setSpout("ads", kafkaSpout, kafkaPartitions); builder.setBolt("event_deserializer", new DeserializeBolt(), parallel).shuffleGrouping("ads"); builder.setBolt("event_filter", new EventFilterBolt(), parallel).shuffleGrouping("event_deserializer"); builder.setBolt("event_projection", new EventProjectionBolt(), parallel).shuffleGrouping("event_filter"); builder.setBolt("redis_join", new RedisJoinBolt(redisServerHost), parallel).shuffleGrouping("event_projection"); builder.setBolt("campaign_processor", new CampaignProcessor(redisServerHost), parallel*2) .fieldsGrouping("redis_join", new Fields("campaign_id")); Config conf = new Config(); if (args != null && args.length > 0) { conf.setNumWorkers(workers); conf.setNumAckers(ackers); StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createTopology()); } else { LocalCluster cluster = new LocalCluster(); cluster.submitTopology("test", conf, builder.createTopology()); org.apache.storm.utils.Utils.sleep(10000); cluster.killTopology("test"); cluster.shutdown(); } }
Example 19
Source File: WordCountTopologyCsharp.java From storm-net-adapter with Apache License 2.0 | 3 votes |
public static void main(String[] args) throws Exception { TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("generator", new Generator(), 1); builder.setBolt("splitter", new Splitter(), 5).fieldsGrouping("generator", new Fields("word")); builder.setBolt("counter", new Counter(), 8).fieldsGrouping("splitter", new Fields("word", "count")); Config conf = new Config(); //conf.setDebug(true); if (args != null && args.length > 0) { conf.setNumWorkers(3); StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createTopology()); } else { conf.setMaxTaskParallelism(3); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("WordCount", conf, builder.createTopology()); Thread.sleep(10000); cluster.shutdown(); } }
Example 20
Source File: WordCountApp.java From java-study with Apache License 2.0 | 2 votes |
public static void main( String[] args ) //throws Exception { //System.out.println( "Hello World!" ); //实例化spout和bolt SentenceSpout spout = new SentenceSpout(); SplitSentenceBolt splitBolt = new SplitSentenceBolt(); WordCountBolt countBolt = new WordCountBolt(); ReportBolt reportBolt = new ReportBolt(); TopologyBuilder builder = new TopologyBuilder();//创建了一个TopologyBuilder实例 //TopologyBuilder提供流式风格的API来定义topology组件之间的数据流 //builder.setSpout(SENTENCE_SPOUT_ID, spout);//注册一个sentence spout //设置两个Executeor(线程),默认一个 builder.setSpout(SENTENCE_SPOUT_ID, spout,2); // SentenceSpout --> SplitSentenceBolt //注册一个bolt并订阅sentence发射出的数据流,shuffleGrouping方法告诉Storm要将SentenceSpout发射的tuple随机均匀的分发给SplitSentenceBolt的实例 //builder.setBolt(SPLIT_BOLT_ID, splitBolt).shuffleGrouping(SENTENCE_SPOUT_ID); //SplitSentenceBolt单词分割器设置4个Task,2个Executeor(线程) builder.setBolt(SPLIT_BOLT_ID, splitBolt,2).setNumTasks(4).shuffleGrouping(SENTENCE_SPOUT_ID); // SplitSentenceBolt --> WordCountBolt //fieldsGrouping将含有特定数据的tuple路由到特殊的bolt实例中 //这里fieldsGrouping()方法保证所有“word”字段相同的tuuple会被路由到同一个WordCountBolt实例中 //builder.setBolt(COUNT_BOLT_ID, countBolt).fieldsGrouping( SPLIT_BOLT_ID, new Fields("word")); //WordCountBolt单词计数器设置4个Executeor(线程) builder.setBolt(COUNT_BOLT_ID, countBolt,4).fieldsGrouping( SPLIT_BOLT_ID, new Fields("word")); // WordCountBolt --> ReportBolt //globalGrouping是把WordCountBolt发射的所有tuple路由到唯一的ReportBolt builder.setBolt(REPORT_BOLT_ID, reportBolt).globalGrouping(COUNT_BOLT_ID); Config config = new Config();//Config类是一个HashMap<String,Object>的子类,用来配置topology运行时的行为 //设置worker数量 //config.setNumWorkers(2); //本地提交 LocalCluster cluster = new LocalCluster(); cluster.submitTopology(TOPOLOGY_NAME, config, builder.createTopology()); Utils.sleep(10000); cluster.killTopology(TOPOLOGY_NAME); cluster.shutdown(); }