FlatMapTest.java 6.0 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
import static org.junit.Assert.assertTrue;
M
Márton Balassi 已提交
19
import static org.junit.Assert.fail;
G
gyfora 已提交
20

G
ghermann 已提交
21 22 23
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

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

import eu.stratosphere.api.java.functions.FlatMapFunction;
G
ghermann 已提交
27
import eu.stratosphere.api.java.tuple.Tuple;
G
ghermann 已提交
28
import eu.stratosphere.api.java.tuple.Tuple1;
G
gyfora 已提交
29
import eu.stratosphere.api.java.typeutils.TupleTypeInfo;
G
ghermann 已提交
30 31 32
import eu.stratosphere.api.java.typeutils.TypeExtractor;
import eu.stratosphere.configuration.Configuration;
import eu.stratosphere.nephele.jobgraph.AbstractJobVertex;
G
gyfora 已提交
33 34
import eu.stratosphere.nephele.jobgraph.JobInputVertex;
import eu.stratosphere.nephele.jobgraph.JobOutputVertex;
G
ghermann 已提交
35
import eu.stratosphere.nephele.jobgraph.JobTaskVertex;
G
gyfora 已提交
36 37
import eu.stratosphere.streaming.api.invokable.UserSinkInvokable;
import eu.stratosphere.streaming.api.invokable.UserSourceInvokable;
G
gyfora 已提交
38
import eu.stratosphere.streaming.api.invokable.UserTaskInvokable;
G
ghermann 已提交
39 40
import eu.stratosphere.util.Collector;

41
public class FlatMapTest {
G
ghermann 已提交
42

M
Márton Balassi 已提交
43
	public static final class MyFlatMap extends FlatMapFunction<Tuple1<String>, Tuple1<String>> {
G
ghermann 已提交
44
		@Override
M
Márton Balassi 已提交
45 46 47
		public void flatMap(Tuple1<String> value, Collector<Tuple1<String>> out) throws Exception {
			out.collect(value);
			System.out.println("flatMap");
G
ghermann 已提交
48 49
		}
	}
G
ghermann 已提交
50

M
Márton Balassi 已提交
51 52
	public static final class MySink extends SinkFunction<Tuple1<String>> {
		int c=0;
G
gyfora 已提交
53
		@Override
M
Márton Balassi 已提交
54 55 56 57
		public void invoke(Tuple1<String> tuple) {
			System.out.println(tuple);
			c++;
			System.out.println(c);
G
gyfora 已提交
58 59 60 61
		}

	}

M
Márton Balassi 已提交
62
	public static final class MySource extends SourceFunction<Tuple1<String>> {
63 64

		@Override
M
Márton Balassi 已提交
65 66 67
		public void invoke(Collector<Tuple1<String>> collector) {
			for (int i = 0; i < 5; i++) {
				collector.collect(new Tuple1<String>("hi"));
68 69 70 71
			}
		}
	}

M
Márton Balassi 已提交
72
	private static final int PARALELISM = 2;
73

G
ghermann 已提交
74
	@Test
G
gyfora 已提交
75
	public void test() throws Exception {
M
Márton Balassi 已提交
76 77 78 79 80 81 82 83 84 85 86

		try {
			StreamExecutionEnvironment env2 = new StreamExecutionEnvironment(0, 1000);
			fail();
		} catch (IllegalArgumentException e) {
			try {
				StreamExecutionEnvironment env2 = new StreamExecutionEnvironment(1, 0);
				fail();
			} catch (IllegalArgumentException e2) {	
			}
		}
G
gyfora 已提交
87
		
M
Márton Balassi 已提交
88 89
		StreamExecutionEnvironment env = new StreamExecutionEnvironment(2, 1000);
		DataStream<Tuple1<String>> dataStream0 = env.addSource(new MySource(),1);
G
ghermann 已提交
90

M
Márton Balassi 已提交
91 92
		DataStream<Tuple1<String>> dataStream1 = env.addDummySource().connectWith(dataStream0)
				.partitionBy(0).flatMap(new MyFlatMap(), PARALELISM).broadcast().addSink(new MySink());
G
ghermann 已提交
93

M
Márton Balassi 已提交
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
		env.execute();

		JobGraphBuilder jgb = env.jobGB();

		for (AbstractJobVertex c : jgb.components.values()) {
			if (c instanceof JobTaskVertex) {
				Configuration config = c.getConfiguration();
				System.out.println(config.getString("componentName", "default"));
				byte[] bytes = config.getBytes("operator", null);

				ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));

				FlatMapFunction<Tuple, Tuple> f = (FlatMapFunction<Tuple, Tuple>) in.readObject();

				StreamCollector<Tuple> s = new StreamCollector<Tuple>(1, 1000, 1, null);
				Tuple t = new Tuple1<String>("asd");

				f.flatMap(t, s);

				System.out.println(f.getClass().getGenericSuperclass());
				TupleTypeInfo<Tuple> ts = (TupleTypeInfo) TypeExtractor.createTypeInfo(
						FlatMapFunction.class, f.getClass(), 0, null, null);

				System.out.println(ts);

				byte[] userFunctionSerialized = config.getBytes("serializedudf", null);
				in = new ObjectInputStream(new ByteArrayInputStream(userFunctionSerialized));
				UserTaskInvokable userFunction = (UserTaskInvokable) in.readObject();
				System.out.println(userFunction.getClass());
				assertTrue(true);
				System.out.println("----------------");
			}

			if (c instanceof JobOutputVertex) {
				Configuration config = c.getConfiguration();
				System.out.println(config.getString("componentName", "default"));
				byte[] bytes = config.getBytes("operator", null);

				ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));

				SinkFunction<Tuple> f = (SinkFunction<Tuple>) in.readObject();

				System.out.println(f.getClass().getGenericSuperclass());
				TupleTypeInfo<Tuple> ts = (TupleTypeInfo) TypeExtractor.createTypeInfo(
						SinkFunction.class, f.getClass(), 0, null, null);

				System.out.println(ts);

				byte[] userFunctionSerialized = config.getBytes("serializedudf", null);
				in = new ObjectInputStream(new ByteArrayInputStream(userFunctionSerialized));
				UserSinkInvokable userFunction = (UserSinkInvokable) in.readObject();
				System.out.println(userFunction.getClass());
				assertTrue(true);
				System.out.println("----------------");
			}

			if (c instanceof JobInputVertex) {
				Configuration config = c.getConfiguration();
				System.out.println(config.getString("componentName", "default"));
				byte[] bytes = config.getBytes("operator", null);

				ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));

				UserSourceInvokable<Tuple> f = (UserSourceInvokable<Tuple>) in.readObject();

				System.out.println(f.getClass().getGenericSuperclass());
				TupleTypeInfo<Tuple> ts = (TupleTypeInfo) TypeExtractor.createTypeInfo(
						UserSourceInvokable.class, f.getClass(), 0, null, null);

				System.out.println(ts);
				System.out.println("----------------");
			}
		}
G
ghermann 已提交
167 168
	}
}