MapTest.java 10.8 KB
Newer Older
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.
 *
 **********************************************************************************************************************/

G
ghermann 已提交
16 17
package eu.stratosphere.streaming.api;

G
gyfora 已提交
18 19
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
G
gyfora 已提交
20

J
jfeher 已提交
21
import java.util.ArrayList;
G
gyfora 已提交
22
import java.util.HashSet;
J
jfeher 已提交
23
import java.util.List;
G
gyfora 已提交
24
import java.util.Set;
G
ghermann 已提交
25

G
ghermann 已提交
26 27
import org.junit.Test;

28
import eu.stratosphere.api.java.functions.MapFunction;
G
ghermann 已提交
29 30 31
import eu.stratosphere.api.java.tuple.Tuple1;
import eu.stratosphere.util.Collector;

32
public class MapTest {
G
ghermann 已提交
33

J
jfeher 已提交
34
	public static final class MySource extends SourceFunction<Tuple1<Integer>> {
G
gyfora 已提交
35
		private static final long serialVersionUID = 1L;
J
jfeher 已提交
36

G
ghermann 已提交
37
		@Override
J
jfeher 已提交
38 39 40 41 42 43
		public void invoke(Collector<Tuple1<Integer>> collector) throws Exception {
			for (int i = 0; i < 10; i++) {
				collector.collect(new Tuple1<Integer>(i));
			}
		}
	}
G
gyfora 已提交
44 45
	
	public static final class MySource1 extends SourceFunction<Tuple1<Integer>> {
G
gyfora 已提交
46
		private static final long serialVersionUID = 1L;
G
gyfora 已提交
47 48 49

		@Override
		public void invoke(Collector<Tuple1<Integer>> collector) throws Exception {
J
jfeher 已提交
50
			System.out.println("src1");
G
gyfora 已提交
51 52 53 54 55 56 57
			for (int i = 0; i < 5; i++) {
				collector.collect(new Tuple1<Integer>(i));
			}
		}
	}
	
	public static final class MySource2 extends SourceFunction<Tuple1<Integer>> {
G
gyfora 已提交
58
		private static final long serialVersionUID = 1L;
G
gyfora 已提交
59 60 61

		@Override
		public void invoke(Collector<Tuple1<Integer>> collector) throws Exception {
J
jfeher 已提交
62
			System.out.println("src2");
G
gyfora 已提交
63 64 65 66 67 68 69
			for (int i = 5; i < 10; i++) {
				collector.collect(new Tuple1<Integer>(i));
			}
		}
	}
	
	public static final class MySource3 extends SourceFunction<Tuple1<Integer>> {
G
gyfora 已提交
70
		private static final long serialVersionUID = 1L;
G
gyfora 已提交
71 72 73

		@Override
		public void invoke(Collector<Tuple1<Integer>> collector) throws Exception {
J
jfeher 已提交
74
			System.out.println("src3");
G
gyfora 已提交
75 76 77 78 79
			for (int i = 10; i < 15; i++) {
				collector.collect(new Tuple1<Integer>(i));
			}
		}
	}
M
Márton Balassi 已提交
80

J
jfeher 已提交
81
	public static final class MyMap extends MapFunction<Tuple1<Integer>, Tuple1<Integer>> {
G
gyfora 已提交
82
		private static final long serialVersionUID = 1L;
M
Márton Balassi 已提交
83

J
jfeher 已提交
84 85
		@Override
		public Tuple1<Integer> map(Tuple1<Integer> value) throws Exception {
J
jfeher 已提交
86
			map++;
J
jfeher 已提交
87 88 89
			return new Tuple1<Integer>(value.f0 * value.f0);
		}
	}
G
gyfora 已提交
90
	
J
jfeher 已提交
91
	public static final class MySingleJoinMap extends MapFunction<Tuple1<Integer>, Tuple1<Integer>> {
G
gyfora 已提交
92
		private static final long serialVersionUID = 1L;
G
gyfora 已提交
93 94 95

		@Override
		public Tuple1<Integer> map(Tuple1<Integer> value) throws Exception {
J
jfeher 已提交
96 97 98 99 100 101
			singleJoinSetResult.add(value.f0);
			return new Tuple1<Integer>(value.f0);
		}
	}
	
	public static final class MyMultipleJoinMap extends MapFunction<Tuple1<Integer>, Tuple1<Integer>> {
G
gyfora 已提交
102
		private static final long serialVersionUID = 1L;
J
jfeher 已提交
103 104 105 106

		@Override
		public Tuple1<Integer> map(Tuple1<Integer> value) throws Exception {
			multipleJoinSetResult.add(value.f0);
G
gyfora 已提交
107 108 109
			return new Tuple1<Integer>(value.f0);
		}
	}
M
Márton Balassi 已提交
110

J
jfeher 已提交
111
	public static final class MyFieldsMap extends MapFunction<Tuple1<Integer>, Tuple1<Integer>> {
G
gyfora 已提交
112
		private static final long serialVersionUID = 1L;
M
Márton Balassi 已提交
113

J
jfeher 已提交
114
		private int counter = 0;
M
Márton Balassi 已提交
115

J
jfeher 已提交
116 117 118 119 120 121 122 123 124 125
		@Override
		public Tuple1<Integer> map(Tuple1<Integer> value) throws Exception {
			counter++;
			if (counter == MAXSOURCE)
				allInOne = true;
			return new Tuple1<Integer>(value.f0 * value.f0);
		}
	}
	
	public static final class MyDiffFieldsMap extends MapFunction<Tuple1<Integer>, Tuple1<Integer>> {
G
gyfora 已提交
126
		private static final long serialVersionUID = 1L;
M
Márton Balassi 已提交
127

J
jfeher 已提交
128
		private int counter = 0;
M
Márton Balassi 已提交
129

J
jfeher 已提交
130 131 132 133 134 135 136 137
		@Override
		public Tuple1<Integer> map(Tuple1<Integer> value) throws Exception {
			counter++;
			if (counter > 3)
				threeInAll = false;
			return new Tuple1<Integer>(value.f0 * value.f0);
		}
	}
M
Márton Balassi 已提交
138

J
jfeher 已提交
139
	public static final class MySink extends SinkFunction<Tuple1<Integer>> {
G
gyfora 已提交
140
		private static final long serialVersionUID = 1L;
M
Márton Balassi 已提交
141

J
jfeher 已提交
142 143 144 145 146
		@Override
		public void invoke(Tuple1<Integer> tuple) {
			result.add(tuple.f0);
		}
	}
M
Márton Balassi 已提交
147

J
jfeher 已提交
148
	public static final class MyBroadcastSink extends SinkFunction<Tuple1<Integer>> {
G
gyfora 已提交
149
		private static final long serialVersionUID = 1L;
150

J
jfeher 已提交
151 152 153 154 155
		@Override
		public void invoke(Tuple1<Integer> tuple) {
			broadcastResult++;
		}
	}
156

J
jfeher 已提交
157
	public static final class MyShufflesSink extends SinkFunction<Tuple1<Integer>> {
G
gyfora 已提交
158
		private static final long serialVersionUID = 1L;
159

J
jfeher 已提交
160 161 162 163 164
		@Override
		public void invoke(Tuple1<Integer> tuple) {
			shuffleResult++;
		}
	}
165

J
jfeher 已提交
166
	public static final class MyFieldsSink extends SinkFunction<Tuple1<Integer>> {
G
gyfora 已提交
167
		private static final long serialVersionUID = 1L;
168

J
jfeher 已提交
169 170 171 172 173 174 175
		@Override
		public void invoke(Tuple1<Integer> tuple) {
			fieldsResult++;
		}
	}
	
	public static final class MyDiffFieldsSink extends SinkFunction<Tuple1<Integer>> {
G
gyfora 已提交
176
		private static final long serialVersionUID = 1L;
177

J
jfeher 已提交
178 179 180 181 182 183 184
		@Override
		public void invoke(Tuple1<Integer> tuple) {
			diffFieldsResult++;
		}
	}
	
	public static final class MyGraphSink extends SinkFunction<Tuple1<Integer>> {
G
gyfora 已提交
185
		private static final long serialVersionUID = 1L;
186

J
jfeher 已提交
187 188 189 190 191
		@Override
		public void invoke(Tuple1<Integer> tuple) {
			graphResult++;
		}
	}
G
gyfora 已提交
192 193
	
	public static final class JoinSink extends SinkFunction<Tuple1<Integer>> {
G
gyfora 已提交
194
		private static final long serialVersionUID = 1L;
G
gyfora 已提交
195 196 197 198 199

		@Override
		public void invoke(Tuple1<Integer> tuple) {
		}
	}
J
jfeher 已提交
200

G
gyfora 已提交
201 202
	private static Set<Integer> expected = new HashSet<Integer>();
	private static Set<Integer> result = new HashSet<Integer>();
J
jfeher 已提交
203 204 205 206 207
	private static int broadcastResult = 0;
	private static int shuffleResult = 0;
	private static int fieldsResult = 0;
	private static int diffFieldsResult = 0;
	private static int graphResult = 0;
J
jfeher 已提交
208
	private static int map = 0;
J
jfeher 已提交
209 210 211 212
	private static final int PARALELISM = 1;
	private static final int MAXSOURCE = 10;
	private static boolean allInOne = false;
	private static boolean threeInAll = true;
G
gyfora 已提交
213 214 215
	private static Set<Integer> fromCollectionSet = new HashSet<Integer>();
	private static List<Integer> fromCollectionFields = new ArrayList<Integer>();
	private static Set<Integer> fromCollectionDiffFieldsSet = new HashSet<Integer>();
G
gyfora 已提交
216 217
	private static Set<Integer> singleJoinSetExpected = new HashSet<Integer>();
	private static Set<Integer> multipleJoinSetExpected = new HashSet<Integer>();
J
jfeher 已提交
218 219
	private static Set<Integer> singleJoinSetResult = new HashSet<Integer>();
	private static Set<Integer> multipleJoinSetResult = new HashSet<Integer>();
J
jfeher 已提交
220 221 222 223 224 225

	private static void fillExpectedList() {
		for (int i = 0; i < 10; i++) {
			expected.add(i * i);
		}
	}
G
gyfora 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	
	private static void fillFromCollectionSet() {
		if(fromCollectionSet.isEmpty()){
			for (int i = 0; i < 10; i++) {
				fromCollectionSet.add(i);
			}
		}
	}
	
	private static void fillFromCollectionFieldsSet() {
		if(fromCollectionFields.isEmpty()){
			for (int i = 0; i < MAXSOURCE; i++) {
				
				fromCollectionFields.add(5);
			}
		}
	}
	
	private static void fillFromCollectionDiffFieldsSet() {
		if(fromCollectionDiffFieldsSet.isEmpty()){
			for (int i = 0; i < 9; i++) {
				fromCollectionDiffFieldsSet.add(i);
			}
		}
	}
G
gyfora 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263
	
	private static void fillSingleJoinSet() {
		for (int i = 0; i < 10; i++) {
			singleJoinSetExpected.add(i);
		}
	}
	
	private static void fillMultipleJoinSet() {
		for (int i = 0; i < 15; i++) {
			multipleJoinSetExpected.add(i);
		}
	}

264

J
jfeher 已提交
265 266
	@Test
	public void mapTest() throws Exception {
J
jfeher 已提交
267 268
		
		//mapTest
269
		StreamExecutionEnvironment env = new StreamExecutionEnvironment();
270

G
gyfora 已提交
271 272 273
		fillFromCollectionSet();
		
		DataStream<Tuple1<Integer>> dataStream = env.fromCollection(fromCollectionSet)
J
jfeher 已提交
274
				.map(new MyMap(), PARALELISM).addSink(new MySink());
275

J
jfeher 已提交
276 277

		fillExpectedList();
G
gyfora 已提交
278
		
J
jfeher 已提交
279 280
	
		//broadcastSinkTest
G
gyfora 已提交
281 282
		fillFromCollectionSet();
		
J
jfeher 已提交
283
		DataStream<Tuple1<Integer>> dataStream1 = env
G
gyfora 已提交
284
				.fromCollection(fromCollectionSet)
J
jfeher 已提交
285 286 287
				.broadcast()
				.map(new MyMap(), 3)
				.addSink(new MyBroadcastSink());
G
gyfora 已提交
288
		
J
jfeher 已提交
289 290

		//shuffleSinkTest
G
gyfora 已提交
291 292
		fillFromCollectionSet();
		
J
jfeher 已提交
293
		DataStream<Tuple1<Integer>> dataStream2 = env
G
gyfora 已提交
294
				.fromCollection(fromCollectionSet)
J
jfeher 已提交
295 296
				.map(new MyMap(), 3)
				.addSink(new MyShufflesSink());
G
gyfora 已提交
297

G
gyfora 已提交
298
		
J
jfeher 已提交
299
		//fieldsMapTest
G
gyfora 已提交
300 301
		fillFromCollectionFieldsSet();
		
J
jfeher 已提交
302
		DataStream<Tuple1<Integer>> dataStream3 = env
G
gyfora 已提交
303
				.fromCollection(fromCollectionFields)
J
jfeher 已提交
304 305 306 307
				.partitionBy(0)
				.map(new MyFieldsMap(), 3)
				.addSink(new MyFieldsSink());

G
gyfora 已提交
308
		
J
jfeher 已提交
309
		//diffFieldsMapTest
G
gyfora 已提交
310 311
		fillFromCollectionDiffFieldsSet();
		
J
jfeher 已提交
312
		DataStream<Tuple1<Integer>> dataStream4 = env
G
gyfora 已提交
313
				.fromCollection(fromCollectionDiffFieldsSet)
J
jfeher 已提交
314 315 316
				.partitionBy(0)
				.map(new MyDiffFieldsMap(), 3)
				.addSink(new MyDiffFieldsSink());
G
gyfora 已提交
317 318
	
		
J
jfeher 已提交
319
		//singleConnectWithTest
G
gyfora 已提交
320 321 322 323 324 325 326
		DataStream<Tuple1<Integer>> source1 = env.addSource(new MySource1(),
				1);
		
		DataStream<Tuple1<Integer>> source2 = env
				.addSource(new MySource2(), 1)
				.connectWith(source1)
				.partitionBy(0)
J
jfeher 已提交
327
				.map(new MySingleJoinMap(), 1)
G
gyfora 已提交
328 329 330 331 332 333
				.addSink(new JoinSink());

		
		fillSingleJoinSet();
		
		
J
jfeher 已提交
334 335
		//multipleConnectWithTest
		DataStream<Tuple1<Integer>> source3 = env.addSource(new MySource1(),
G
gyfora 已提交
336 337
				1);
		
J
jfeher 已提交
338
		DataStream<Tuple1<Integer>> source4 = env.addSource(new MySource2(),
G
gyfora 已提交
339
				1);
J
jfeher 已提交
340
		DataStream<Tuple1<Integer>> source5 = env
G
gyfora 已提交
341
				.addSource(new MySource3(), 1)
J
jfeher 已提交
342
				.connectWith(source3, source4)
G
gyfora 已提交
343
				.partitionBy(0)
J
jfeher 已提交
344
				.map(new MyMultipleJoinMap(), 1)
G
gyfora 已提交
345 346
				.addSink(new JoinSink());

347
		env.execute();
G
gyfora 已提交
348 349 350
		
		fillMultipleJoinSet();
		
J
jfeher 已提交
351 352 353 354 355 356 357 358 359
		assertTrue(expected.equals(result));
		assertEquals(30, broadcastResult);
		assertEquals(10, shuffleResult);
		assertTrue(allInOne);
		assertTrue(threeInAll);
		assertEquals(9, diffFieldsResult);
		assertEquals(singleJoinSetExpected, singleJoinSetResult);
		assertEquals(multipleJoinSetExpected, multipleJoinSetResult);
		
G
gyfora 已提交
360
	}
J
jfeher 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	
//	@Test
//	public void fieldsSinkTest() throws Exception {
//		StreamExecutionEnvironment env = new StreamExecutionEnvironment();
//		DataStream<Tuple1<Integer>> dataStream = env
//				.addSource(new MySource(), 1)
//				.partitionBy(0)
//				.map(new MyMap(), 3)
//				.addSink(new MyFieldsSink());
//
//		env.execute();
//		assertEquals(10, fieldsResult);
//
//	}
	
//	@Test
//	public void graphTest() throws Exception {
//		for(int i=0; i<1000; i++){
//			System.out.println(i);
//			StreamExecutionEnvironment env = new StreamExecutionEnvironment();
//			DataStream<Tuple1<Integer>> dataStream = env
//					.addSource(new MySource(), 2)
//					.partitionBy(0)
//					.map(new MyMap(), 3)
//					.broadcast()
//					.addSink(new MyGraphSink(),2);
//	
//			env.execute();
//			assertEquals(40, graphResult);
//			graphResult=0;
//			map=0;
//		}
//		
//	}
J
jfeher 已提交
395

G
ghermann 已提交
396
}