forked from apache/incubator-heron
/
WordCountTopology.java
200 lines (166 loc) · 6.29 KB
/
WordCountTopology.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.heron.examples.api;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.heron.api.Config;
import org.apache.heron.api.HeronSubmitter;
import org.apache.heron.api.bolt.BaseRichBolt;
import org.apache.heron.api.bolt.OutputCollector;
import org.apache.heron.api.exception.AlreadyAliveException;
import org.apache.heron.api.exception.InvalidTopologyException;
import org.apache.heron.api.spout.BaseRichSpout;
import org.apache.heron.api.spout.SpoutOutputCollector;
import org.apache.heron.api.topology.OutputFieldsDeclarer;
import org.apache.heron.api.topology.TopologyBuilder;
import org.apache.heron.api.topology.TopologyContext;
import org.apache.heron.api.tuple.Fields;
import org.apache.heron.api.tuple.Tuple;
import org.apache.heron.api.tuple.Values;
import org.apache.heron.common.basics.ByteAmount;
/**
* This is a topology that does simple word counts.
* <p>
* In this topology,
* 1. the spout task generate a set of random words during initial "open" method.
* (~128k words, 20 chars per word)
* 2. During every "nextTuple" call, each spout simply picks a word at random and emits it
* 3. Spouts use a fields grouping for their output, and each spout could send tuples to
* every other bolt in the topology
* 4. Bolts maintain an in-memory map, which is keyed by the word emitted by the spouts,
* and updates the count when it receives a tuple.
*/
public final class WordCountTopology {
private WordCountTopology() {
}
// Utils class to generate random String at given length
public static class RandomString {
private final char[] symbols;
private final Random random = new Random();
private final char[] buf;
public RandomString(int length) {
// Construct the symbol set
StringBuilder tmp = new StringBuilder();
for (char ch = '0'; ch <= '9'; ++ch) {
tmp.append(ch);
}
for (char ch = 'a'; ch <= 'z'; ++ch) {
tmp.append(ch);
}
symbols = tmp.toString().toCharArray();
if (length < 1) {
throw new IllegalArgumentException("length < 1: " + length);
}
buf = new char[length];
}
public String nextString() {
for (int idx = 0; idx < buf.length; ++idx) {
buf[idx] = symbols[random.nextInt(symbols.length)];
}
return new String(buf);
}
}
/**
* A spout that emits a random word
*/
public static class WordSpout extends BaseRichSpout {
private static final long serialVersionUID = 4322775001819135036L;
private static final int ARRAY_LENGTH = 128 * 1024;
private static final int WORD_LENGTH = 20;
private final String[] words = new String[ARRAY_LENGTH];
private final Random rnd = new Random(31);
private SpoutOutputCollector collector;
@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
outputFieldsDeclarer.declare(new Fields("word"));
}
@Override
@SuppressWarnings("rawtypes")
public void open(Map map, TopologyContext topologyContext,
SpoutOutputCollector spoutOutputCollector) {
RandomString randomString = new RandomString(WORD_LENGTH);
for (int i = 0; i < ARRAY_LENGTH; i++) {
words[i] = randomString.nextString();
}
collector = spoutOutputCollector;
}
@Override
public void nextTuple() {
int nextInt = rnd.nextInt(ARRAY_LENGTH);
collector.emit(new Values(words[nextInt]));
}
}
/**
* A bolt that counts the words that it receives
*/
public static class ConsumerBolt extends BaseRichBolt {
private static final long serialVersionUID = -5470591933906954522L;
private OutputCollector collector;
private Map<String, Integer> countMap;
@SuppressWarnings("rawtypes")
public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
collector = outputCollector;
countMap = new HashMap<String, Integer>();
}
@Override
public void execute(Tuple tuple) {
String key = tuple.getString(0);
if (countMap.get(key) == null) {
countMap.put(key, 1);
} else {
Integer val = countMap.get(key);
countMap.put(key, ++val);
}
}
@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
}
}
/**
* Main method
*/
public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException {
if (args.length < 1) {
throw new RuntimeException("Specify topology name");
}
int parallelism = 1;
if (args.length > 1) {
parallelism = Integer.parseInt(args[1]);
}
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("word", new WordSpout(), parallelism);
builder.setBolt("consumer", new ConsumerBolt(), parallelism)
.fieldsGrouping("word", new Fields("word"));
Config conf = new Config();
conf.setNumStmgrs(parallelism);
// configure component resources
conf.setComponentRam("word",
ByteAmount.fromMegabytes(ExampleResources.COMPONENT_RAM_MB));
conf.setComponentRam("consumer",
ByteAmount.fromMegabytes(ExampleResources.COMPONENT_RAM_MB));
// configure container resources
conf.setContainerDiskRequested(
ExampleResources.getContainerDisk(2 * parallelism, parallelism));
conf.setContainerRamRequested(
ExampleResources.getContainerRam(2 * parallelism, parallelism));
conf.setContainerCpuRequested(2);
HeronSubmitter.submitTopology(args[0], conf, builder.createTopology());
}
}