WindowWordCountSplitter.java 1.7 KB
Newer Older
Y
Yingjun Wu 已提交
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.window.wordcount;
Y
Yingjun Wu 已提交
17

18 19 20
import eu.stratosphere.api.java.tuple.Tuple2;
import eu.stratosphere.util.Collector;
import eu.stratosphere.api.java.functions.FlatMapFunction;
Y
Yingjun Wu 已提交
21

22
public class WindowWordCountSplitter extends FlatMapFunction<Tuple2<String, Long>, Tuple2<String, Long>> {
23 24
	private static final long serialVersionUID = 1L;
	
25
	private String[] words = new String[] {};
M
Márton Balassi 已提交
26
	private Long timestamp = 0L;
27
	private Tuple2<String, Long> outTuple = new Tuple2<String, Long>();
M
Márton Balassi 已提交
28

J
jfeher 已提交
29
	// Splits the lines according to the spaces. And adds the line's timestamp to them.
Y
Yingjun Wu 已提交
30
	@Override
31 32 33 34 35 36 37 38
	public void flatMap(Tuple2<String, Long> inTuple, Collector<Tuple2<String, Long>> out) throws Exception {

		words=inTuple.f0.split(" ");
		timestamp=inTuple.f1;
		for(String word : words){
			outTuple.f0 = word;
			outTuple.f1 = timestamp;
			out.collect(outTuple);
Y
Yingjun Wu 已提交
39 40 41
		}
	}
}