-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordCountFieldGrouping.java
185 lines (155 loc) · 4.94 KB
/
WordCountFieldGrouping.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
package storm.starter;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import storm.starter.spout.ZipfGeneratorSpout;
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.topology.base.BaseBasicBolt;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;
import backtype.storm.utils.Utils;
import com.codahale.metrics.CsvReporter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
/**
* This is a basic example of a Storm topology.
*/
public class WordCountFieldGrouping {
public static class CounterBolt extends BaseRichBolt {
private static final String GRAPHITE_METRICS_NAMESPACE_PREFIX = "zipf_4_4_FG";
private static final Pattern hostnamePattern =
Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9-]*(\\.([a-zA-Z0-9][a-zA-Z0-9-]*))*$");
int boltId = -1;
private transient Meter tuplesReceived;
private transient Meter memoryUsed;
private transient Histogram histogram;
private static final long serialVersionUID = 1L;
OutputCollector collector;
private void initializeMetricReporting()
{
final MetricRegistry registry = new MetricRegistry();
final CsvReporter reporter = CsvReporter.forRegistry(registry)
.formatFor(Locale.US)
.convertRatesTo(TimeUnit.MINUTES)
.build(new File("/mnt/anis-logs/KG/"));
reporter.start(1,TimeUnit.MINUTES);
tuplesReceived = registry.meter(MetricRegistry.name("tuples", "received",metricsPath()));
memoryUsed = registry.meter(MetricRegistry.name("memory","received",metricsPath()));
histogram = registry.histogram(MetricRegistry.name("histogram","time",metricsPath()));
}
Map<String, Integer> counts = new HashMap<String, Integer>();
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word","count"));
}
private String metricsPath() {
if (boltId == -1) {
Random rnd = new Random();
boltId = rnd.nextInt(1000);
}
final String myHostname = extractHostnameFromFQHN(detectHostname());
return GRAPHITE_METRICS_NAMESPACE_PREFIX + "." + myHostname+"."+boltId;
}
private static String detectHostname()
{
String hostname = "hostname-could-not-be-detected";
try {
hostname = InetAddress.getLocalHost().getHostName();
}catch (UnknownHostException e) {
}
return hostname;
}
private static String extractHostnameFromFQHN(String fqhn)
{
if (hostnamePattern.matcher(fqhn).matches())
{
if (fqhn.contains("."))
{
return fqhn.split("\\.")[0];
}
else
{
return fqhn;
}
}
else {
return fqhn;
}
}
public void testWait(long INTERVAL){
long start = System.nanoTime();
long end=0;
do{
end = System.nanoTime();
}while(start + INTERVAL >= end);
System.out.println(end - start);
}
@Override
public void execute(Tuple tuple) {
String word = tuple.getStringByField("word");
Long startTime = tuple.getLongByField("count");
int count = 0;
if(!word.isEmpty())
{
testWait(400000);
if(counts.containsKey(word)) {
count = counts.get(word);
count++;
counts.put(word, count);
}else {
counts.put(word, 1);
count = 1;
memoryUsed.mark();
}
tuplesReceived.mark();
collector.emit(new Values(word, count));
}
collector.ack(tuple);
Long endTime = System.currentTimeMillis();
Long executionLatency = endTime-startTime;
histogram.update(executionLatency);
}
@Override
public void prepare(Map arg0, TopologyContext arg1, OutputCollector arg2) {
initializeMetricReporting();
collector = arg2;
}
}
public static void main(String[] args) throws Exception {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("stream", new ZipfGeneratorSpout(), 24);
builder.setBolt("counter", new CounterBolt(),8).fieldsGrouping("stream",new Fields("word"));;
Config conf = new Config();
conf.setDebug(false);
conf.setMaxSpoutPending(10);
if (args != null && args.length > 0) {
conf.setNumWorkers(32);
StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
}
else
{
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("test", conf, builder.createTopology());
Utils.sleep(14400000);
cluster.killTopology("test");
cluster.shutdown();
}
}
}