This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 594
Add Window Bolt support #1215
Open
windie
wants to merge
3
commits into
apache:master
Choose a base branch
from
windie:window
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Window Bolt support #1215
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
heron/examples/src/java/com/twitter/heron/examples/PrinterBolt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 org.apache.storm.topology.BasicOutputCollector; | ||
import org.apache.storm.topology.OutputFieldsDeclarer; | ||
import org.apache.storm.topology.base.BaseBasicBolt; | ||
import org.apache.storm.tuple.Tuple; | ||
|
||
@SuppressWarnings("serial") | ||
public class PrinterBolt extends BaseBasicBolt { | ||
|
||
@Override | ||
public void execute(Tuple tuple, BasicOutputCollector collector) { | ||
System.out.println(tuple); | ||
} | ||
|
||
@Override | ||
public void declareOutputFields(OutputFieldsDeclarer ofd) { | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
heron/examples/src/java/com/twitter/heron/examples/RandomIntegerSpout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.storm.spout.SpoutOutputCollector; | ||
import org.apache.storm.task.TopologyContext; | ||
import org.apache.storm.topology.OutputFieldsDeclarer; | ||
import org.apache.storm.topology.base.BaseRichSpout; | ||
import org.apache.storm.tuple.Fields; | ||
import org.apache.storm.tuple.Values; | ||
import org.apache.storm.utils.Utils; | ||
|
||
/** | ||
* Emits a random integer and a timestamp value (offset by one day), | ||
* every 100 ms. The ts field can be used in tuple time based windowing. | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class RandomIntegerSpout extends BaseRichSpout { | ||
private static final Logger LOG = Logger.getLogger(RandomIntegerSpout.class.getName()); | ||
private SpoutOutputCollector collector; | ||
private Random rand; | ||
private long msgId = 0; | ||
|
||
@Override | ||
public void declareOutputFields(OutputFieldsDeclarer declarer) { | ||
declarer.declare(new Fields("value", "ts", "msgid")); | ||
} | ||
|
||
@Override | ||
public void open( | ||
Map<String, Object> conf, TopologyContext context, SpoutOutputCollector newCollector) { | ||
collector = newCollector; | ||
rand = new Random(); | ||
} | ||
|
||
@Override | ||
public void nextTuple() { | ||
Utils.sleep(100); | ||
collector.emit(new Values(rand.nextInt(1000), | ||
System.currentTimeMillis() - (24 * 60 * 60 * 1000), ++msgId), msgId); | ||
} | ||
|
||
@Override | ||
public void ack(Object newMsgId) { | ||
LOG.log(Level.FINE, "Got ACK for msgId : " + newMsgId); | ||
} | ||
|
||
@Override | ||
public void fail(Object newMsgId) { | ||
LOG.log(Level.FINE, "Got FAIL for msgId : " + newMsgId); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
heron/examples/src/java/com/twitter/heron/examples/SlidingTupleTsTopology.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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.concurrent.TimeUnit; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.storm.Config; | ||
import org.apache.storm.LocalCluster; | ||
import org.apache.storm.StormSubmitter; | ||
import org.apache.storm.topology.TopologyBuilder; | ||
import org.apache.storm.topology.base.BaseWindowedBolt; | ||
import org.apache.storm.utils.Utils; | ||
|
||
import static org.apache.storm.topology.base.BaseWindowedBolt.Duration; | ||
|
||
/** | ||
* Windowing based on tuple timestamp (e.g. the time when tuple is generated | ||
* rather than when its processed). | ||
*/ | ||
// SUPPRESS CHECKSTYLE HideUtilityClassConstructor | ||
public class SlidingTupleTsTopology { | ||
public static void main(String[] args) throws Exception { | ||
Logger.getGlobal().setLevel(Level.FINE); | ||
TopologyBuilder builder = new TopologyBuilder(); | ||
BaseWindowedBolt bolt = new SlidingWindowSumBolt() | ||
.withWindow(new Duration(5, TimeUnit.SECONDS), new Duration(3, TimeUnit.SECONDS)) | ||
.withTimestampField("ts") | ||
.withLag(new Duration(5, TimeUnit.SECONDS)); | ||
builder.setSpout("integer", new RandomIntegerSpout(), 1); | ||
builder.setBolt("slidingsum", bolt, 1).shuffleGrouping("integer"); | ||
builder.setBolt("printer", new PrinterBolt(), 1).shuffleGrouping("slidingsum"); | ||
Config conf = new Config(); | ||
conf.setDebug(true); | ||
|
||
if (args != null && args.length > 0) { | ||
conf.setNumWorkers(1); | ||
StormSubmitter.submitTopology(args[0], conf, builder.createTopology()); | ||
} else { | ||
LocalCluster cluster = new LocalCluster(); | ||
cluster.submitTopology("test", conf, builder.createTopology()); | ||
Utils.sleep(40000); | ||
cluster.killTopology("test"); | ||
cluster.shutdown(); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
heron/examples/src/java/com/twitter/heron/examples/SlidingWindowSumBolt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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.List; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.storm.task.OutputCollector; | ||
import org.apache.storm.task.TopologyContext; | ||
import org.apache.storm.topology.OutputFieldsDeclarer; | ||
import org.apache.storm.topology.base.BaseWindowedBolt; | ||
import org.apache.storm.tuple.Fields; | ||
import org.apache.storm.tuple.Tuple; | ||
import org.apache.storm.tuple.Values; | ||
import org.apache.storm.windowing.TupleWindow; | ||
|
||
/** | ||
* Computes sliding window sum | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class SlidingWindowSumBolt extends BaseWindowedBolt { | ||
private static final Logger LOG = Logger.getLogger(SlidingWindowSumBolt.class.getName()); | ||
|
||
private int sum = 0; | ||
private OutputCollector collector; | ||
|
||
@Override | ||
@SuppressWarnings("rawtypes") | ||
public void prepare(Map stormConf, TopologyContext context, OutputCollector newCollector) { | ||
collector = newCollector; | ||
} | ||
|
||
@Override | ||
public void execute(TupleWindow inputWindow) { | ||
/* | ||
* The inputWindow gives a view of | ||
* (a) all the events in the window | ||
* (b) events that expired since last activation of the window | ||
* (c) events that newly arrived since last activation of the window | ||
*/ | ||
List<Tuple> tuplesInWindow = inputWindow.get(); | ||
List<Tuple> newTuples = inputWindow.getNew(); | ||
List<Tuple> expiredTuples = inputWindow.getExpired(); | ||
|
||
LOG.log(Level.FINE, "Events in current window: " + tuplesInWindow.size()); | ||
/* | ||
* Instead of iterating over all the tuples in the window to compute | ||
* the sum, the values for the new events are added and old events are | ||
* subtracted. Similar optimizations might be possible in other | ||
* windowing computations. | ||
*/ | ||
for (Tuple tuple : newTuples) { | ||
sum += (int) tuple.getValue(0); | ||
} | ||
for (Tuple tuple : expiredTuples) { | ||
sum -= (int) tuple.getValue(0); | ||
} | ||
collector.emit(new Values(sum)); | ||
} | ||
|
||
@Override | ||
public void declareOutputFields(OutputFieldsDeclarer declarer) { | ||
declarer.declare(new Fields("sum")); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
heron/examples/src/java/com/twitter/heron/examples/SlidingWindowTopology.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// 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.List; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.storm.Config; | ||
import org.apache.storm.LocalCluster; | ||
import org.apache.storm.StormSubmitter; | ||
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.BaseWindowedBolt; | ||
import org.apache.storm.tuple.Fields; | ||
import org.apache.storm.tuple.Tuple; | ||
import org.apache.storm.tuple.Values; | ||
import org.apache.storm.utils.Utils; | ||
import org.apache.storm.windowing.TupleWindow; | ||
|
||
import static org.apache.storm.topology.base.BaseWindowedBolt.Count; | ||
|
||
/** | ||
* A sample topology that demonstrates the usage of {@link org.apache.storm.topology.IWindowedBolt} | ||
* to calculate sliding window sum. | ||
*/ | ||
// SUPPRESS CHECKSTYLE HideUtilityClassConstructor | ||
public class SlidingWindowTopology { | ||
|
||
private static final Logger LOG = Logger.getLogger(SlidingWindowTopology.class.getName()); | ||
|
||
/** | ||
* Computes tumbling window average | ||
*/ | ||
@SuppressWarnings("serial") | ||
private static class TumblingWindowAvgBolt extends BaseWindowedBolt { | ||
private OutputCollector collector; | ||
|
||
@Override | ||
@SuppressWarnings("rawtypes") | ||
public void prepare(Map stormConf, TopologyContext context, OutputCollector newCollector) { | ||
collector = newCollector; | ||
} | ||
|
||
@Override | ||
public void execute(TupleWindow inputWindow) { | ||
int sum = 0; | ||
List<Tuple> tuplesInWindow = inputWindow.get(); | ||
LOG.log(Level.FINE, "Events in current window: " + tuplesInWindow.size()); | ||
if (tuplesInWindow.size() > 0) { | ||
/* | ||
* Since this is a tumbling window calculation, | ||
* we use all the tuples in the window to compute the avg. | ||
*/ | ||
for (Tuple tuple : tuplesInWindow) { | ||
sum += (int) tuple.getValue(0); | ||
} | ||
collector.emit(new Values(sum / tuplesInWindow.size())); | ||
} | ||
} | ||
|
||
@Override | ||
public void declareOutputFields(OutputFieldsDeclarer declarer) { | ||
declarer.declare(new Fields("avg")); | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) throws Exception { | ||
TopologyBuilder builder = new TopologyBuilder(); | ||
builder.setSpout("integer", new RandomIntegerSpout(), 1); | ||
builder.setBolt("slidingsum", | ||
new SlidingWindowSumBolt().withWindow(new Count(30), new Count(10)), 1) | ||
.shuffleGrouping("integer"); | ||
builder.setBolt("tumblingavg", new TumblingWindowAvgBolt().withTumblingWindow(new Count(3)), 1) | ||
.shuffleGrouping("slidingsum"); | ||
builder.setBolt("printer", new PrinterBolt(), 1).shuffleGrouping("tumblingavg"); | ||
Config conf = new Config(); | ||
conf.setDebug(true); | ||
if (args != null && args.length > 0) { | ||
conf.setNumWorkers(1); | ||
StormSubmitter.submitTopology(args[0], conf, builder.createTopology()); | ||
} else { | ||
LocalCluster cluster = new LocalCluster(); | ||
cluster.submitTopology("test", conf, builder.createTopology()); | ||
Utils.sleep(40000); | ||
cluster.killTopology("test"); | ||
cluster.shutdown(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of suppressing this here and elsewhere, can you add a serialVersionId?