WordCountCounter.java 1.8 KB
Newer Older
M
Márton Balassi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/***********************************************************************************************************************
 *
 * Copyright (C) 2010-2014 by the Stratosphere project (http://stratosphere.eu)
 *
 * 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.
 *
 **********************************************************************************************************************/

16
package eu.stratosphere.streaming.examples.wordcount;
M
Márton Balassi 已提交
17

G
ghermann 已提交
18 19 20
import java.util.HashMap;
import java.util.Map;

M
Márton Balassi 已提交
21 22
import eu.stratosphere.api.java.functions.MapFunction;
import eu.stratosphere.api.java.tuple.Tuple1;
23
import eu.stratosphere.api.java.tuple.Tuple2;
M
Márton Balassi 已提交
24

M
Márton Balassi 已提交
25
public class WordCountCounter extends MapFunction<Tuple1<String>, Tuple2<String, Integer>> {
26
	private static final long serialVersionUID = 1L;
G
Gyula Fora 已提交
27

G
ghermann 已提交
28
	private Map<String, Integer> wordCounts = new HashMap<String, Integer>();
29 30 31
	private String word = "";
	private Integer count = 0;

M
Márton Balassi 已提交
32 33
	private Tuple2<String, Integer> outTuple = new Tuple2<String, Integer>();
	
34
	// Increments the counter of the occurrence of the input word
M
Márton Balassi 已提交
35
	@Override
M
Márton Balassi 已提交
36 37
	public Tuple2<String, Integer> map(Tuple1<String> inTuple) throws Exception {
		word = inTuple.f0;
38

G
Gyula Fora 已提交
39 40 41 42
		if (wordCounts.containsKey(word)) {
			count = wordCounts.get(word) + 1;
			wordCounts.put(word, count);
		} else {
43
			count = 1;
G
Gyula Fora 已提交
44
			wordCounts.put(word, 1);
M
Márton Balassi 已提交
45
		}
46

M
Márton Balassi 已提交
47 48
		outTuple.f0 = word;
		outTuple.f1 = count;
49

M
Márton Balassi 已提交
50
		return outTuple;
G
gyfora 已提交
51 52
	}

G
Gyula Fora 已提交
53
}