| // Copyright 2016 Twitter. All rights reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package com.twitter.heron.examples; |
| |
| import java.util.Map; |
| import java.util.Random; |
| |
| import com.twitter.heron.api.Config; |
| import com.twitter.heron.api.HeronSubmitter; |
| import com.twitter.heron.api.bolt.BaseRichBolt; |
| import com.twitter.heron.api.bolt.OutputCollector; |
| import com.twitter.heron.api.metric.GlobalMetrics; |
| import com.twitter.heron.api.spout.BaseRichSpout; |
| import com.twitter.heron.api.spout.SpoutOutputCollector; |
| import com.twitter.heron.api.topology.OutputFieldsDeclarer; |
| import com.twitter.heron.api.topology.TopologyBuilder; |
| import com.twitter.heron.api.topology.TopologyContext; |
| import com.twitter.heron.api.tuple.Fields; |
| import com.twitter.heron.api.tuple.Tuple; |
| import com.twitter.heron.api.tuple.Values; |
| import com.twitter.heron.common.basics.ByteAmount; |
| |
| /** |
| * This is a basic example of a Storm topology. |
| */ |
| public final class ExclamationTopology { |
| |
| private ExclamationTopology() { |
| } |
| |
| public static void main(String[] args) throws Exception { |
| TopologyBuilder builder = new TopologyBuilder(); |
| int parallelism = 2; |
| |
| builder.setSpout("word", new TestWordSpout(), parallelism); |
| builder.setBolt("exclaim1", new ExclamationBolt(), 2 * parallelism) |
| .shuffleGrouping("word"); |
| |
| Config conf = new Config(); |
| conf.setDebug(true); |
| conf.setMaxSpoutPending(10); |
| conf.put(Config.TOPOLOGY_WORKER_CHILDOPTS, "-XX:+HeapDumpOnOutOfMemoryError"); |
| conf.setComponentRam("word", ByteAmount.fromGigabytes(3)); |
| conf.setComponentRam("exclaim1", ByteAmount.fromGigabytes(3)); |
| conf.setContainerDiskRequested(ByteAmount.fromGigabytes(5)); |
| conf.setContainerCpuRequested(5); |
| |
| conf.setNumStmgrs(parallelism); |
| HeronSubmitter.submitTopology(args[0], conf, builder.createTopology()); |
| } |
| |
| public static class TestWordSpout extends BaseRichSpout { |
| |
| private static final long serialVersionUID = -3217886193225455451L; |
| private SpoutOutputCollector collector; |
| private String[] words; |
| private Random rand; |
| |
| @SuppressWarnings("rawtypes") |
| public void open( |
| Map conf, |
| TopologyContext context, |
| SpoutOutputCollector acollector) { |
| collector = acollector; |
| words = new String[]{"nathan", "mike", "jackson", "golda", "bertels"}; |
| rand = new Random(); |
| } |
| |
| public void close() { |
| } |
| |
| public void nextTuple() { |
| final String word = words[rand.nextInt(words.length)]; |
| collector.emit(new Values(word)); |
| } |
| |
| public void ack(Object msgId) { |
| } |
| |
| public void fail(Object msgId) { |
| } |
| |
| public void declareOutputFields(OutputFieldsDeclarer declarer) { |
| declarer.declare(new Fields("word")); |
| } |
| } |
| |
| public static class ExclamationBolt extends BaseRichBolt { |
| |
| private static final long serialVersionUID = 1184860508880121352L; |
| private long nItems; |
| private long startTime; |
| |
| @Override |
| @SuppressWarnings("rawtypes") |
| public void prepare( |
| Map conf, |
| TopologyContext context, |
| OutputCollector collector) { |
| nItems = 0; |
| startTime = System.currentTimeMillis(); |
| } |
| |
| @Override |
| public void execute(Tuple tuple) { |
| if (++nItems % 100000 == 0) { |
| long latency = System.currentTimeMillis() - startTime; |
| System.out.println(tuple.getString(0) + "!!!"); |
| System.out.println("Bolt processed " + nItems + " tuples in " + latency + " ms"); |
| GlobalMetrics.incr("selected_items"); |
| } |
| } |
| |
| @Override |
| public void declareOutputFields(OutputFieldsDeclarer declarer) { |
| // declarer.declare(new Fields("word")); |
| } |
| } |
| } |