Java Code Examples for org.apache.flink.api.java.typeutils.TypeExtractor#getForClass()

The following examples show how to use org.apache.flink.api.java.typeutils.TypeExtractor#getForClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionKeysTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testPojoKeys() {
	TypeInformation<PojoWithMultiplePojos> ti = TypeExtractor.getForClass(PojoWithMultiplePojos.class);
	ExpressionKeys<PojoWithMultiplePojos> ek;
	ek = new ExpressionKeys<>("*", ti);
	Assert.assertArrayEquals(new int[] {0,1,2,3,4}, ek.computeLogicalKeyPositions());

	ek = new ExpressionKeys<>("p1.*", ti);
	Assert.assertArrayEquals(new int[] {1,2}, ek.computeLogicalKeyPositions());

	ek = new ExpressionKeys<>("p2.*", ti);
	Assert.assertArrayEquals(new int[] {3,4}, ek.computeLogicalKeyPositions());

	ek = new ExpressionKeys<>("p1", ti);
	Assert.assertArrayEquals(new int[] {1,2}, ek.computeLogicalKeyPositions());

	ek = new ExpressionKeys<>("p2", ti);
	Assert.assertArrayEquals(new int[] {3,4}, ek.computeLogicalKeyPositions());

	ek = new ExpressionKeys<>("i0", ti);
	Assert.assertArrayEquals(new int[] {0}, ek.computeLogicalKeyPositions());
}
 
Example 2
Source File: SelectorFunctionKeysTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testAreCompatible1() throws Keys.IncompatibleKeysException {
	TypeInformation<Pojo2> t1 = TypeExtractor.getForClass(Pojo2.class);
	TypeInformation<Tuple2<Integer, String>> t2 =
		new TupleTypeInfo<>(BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO);

	Keys<Pojo2> k1 = new Keys.SelectorFunctionKeys<>(
		new KeySelector1(),
		t1,
		BasicTypeInfo.STRING_TYPE_INFO
	);
	Keys<Tuple2<Integer, String>> k2 = new Keys.SelectorFunctionKeys<>(
		new KeySelector2(),
		t2,
		BasicTypeInfo.STRING_TYPE_INFO
	);

	Assert.assertTrue(k1.areCompatible(k2));
	Assert.assertTrue(k2.areCompatible(k1));
}
 
Example 3
Source File: SelectorFunctionKeysTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testOriginalTypes2() throws Exception {
	final TupleTypeInfo<Tuple2<Integer, String>> t1 = new TupleTypeInfo<>(
			BasicTypeInfo.INT_TYPE_INFO,
			BasicTypeInfo.STRING_TYPE_INFO
	);
	TypeInformation<PojoWithMultiplePojos> t2 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);

	Keys<PojoWithMultiplePojos> sk = new Keys.SelectorFunctionKeys<>(
			new KeySelector3(),
			t2,
			t1
	);

	Assert.assertArrayEquals(
			new TypeInformation<?>[] { t1 },
			sk.getOriginalKeyFieldTypes()
	);
}
 
Example 4
Source File: ExpressionKeysTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPojo() throws Throwable {
	TypeInformation<ComplexNestedClass> ti = TypeExtractor.getForClass(ComplexNestedClass.class);

	String[][] tests = new String[][] {
		new String[] {"nonexistent"},
		new String[] {"date.abc"} // nesting into unnested
	};
	for (String[] test : tests) {
		Throwable e = null;
		try {
			new ExpressionKeys<>(test, ti);
		} catch (Throwable t) {
			e = t;
		}
		Assert.assertNotNull(e);
	}
}
 
Example 5
Source File: TypeTransformationsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testLegacyRawToTypeInfoRaw() {
	DataType dataType = DataTypes.ROW(
		DataTypes.FIELD("a", DataTypes.STRING()),
		DataTypes.FIELD("b", DataTypes.DECIMAL(10, 3)),
		DataTypes.FIELD("c", createLegacyRaw()),
		DataTypes.FIELD("d", DataTypes.ARRAY(createLegacyRaw()))
	);

	TypeInformation<TypeTransformationsTest> typeInformation = TypeExtractor.getForClass(TypeTransformationsTest.class);
	DataType expected = DataTypes.ROW(
		DataTypes.FIELD("a", DataTypes.STRING()),
		DataTypes.FIELD("b", DataTypes.DECIMAL(10, 3)),
		DataTypes.FIELD("c", DataTypes.RAW(typeInformation)),
		DataTypes.FIELD("d", DataTypes.ARRAY(DataTypes.RAW(typeInformation)))
	);

	assertEquals(expected, DataTypeUtils.transform(dataType, legacyRawToTypeInfoRaw()));
}
 
Example 6
Source File: StreamExecutionEnvironment.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new data set that contains the given elements. The framework will determine the type according to the
 * based type user supplied. The elements should be the same or be the subclass to the based type.
 * The sequence of elements must not be empty.
 * Note that this operation will result in a non-parallel data stream source, i.e. a data stream source with a
 * degree of parallelism one.
 *
 * @param type
 * 		The based class type in the collection.
 * @param data
 * 		The array of elements to create the data stream from.
 * @param <OUT>
 * 		The type of the returned data stream
 * @return The data stream representing the given array of elements
 */
@SafeVarargs
public final <OUT> DataStreamSource<OUT> fromElements(Class<OUT> type, OUT... data) {
	if (data.length == 0) {
		throw new IllegalArgumentException("fromElements needs at least one element as argument");
	}

	TypeInformation<OUT> typeInfo;
	try {
		typeInfo = TypeExtractor.getForClass(type);
	}
	catch (Exception e) {
		throw new RuntimeException("Could not create TypeInformation for type " + type.getName()
				+ "; please specify the TypeInformation manually via "
				+ "StreamExecutionEnvironment#fromElements(Collection, TypeInformation)", e);
	}
	return fromCollection(Arrays.asList(data), typeInfo);
}
 
Example 7
Source File: SelectorFunctionKeysTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAreCompatible2() throws Keys.IncompatibleKeysException {
	TypeInformation<PojoWithMultiplePojos> t1 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);
	TypeInformation<Tuple3<Long, Pojo1, Integer>> t2 = new TupleTypeInfo<>(
		BasicTypeInfo.LONG_TYPE_INFO,
		TypeExtractor.getForClass(Pojo1.class),
		BasicTypeInfo.INT_TYPE_INFO);
	TypeInformation<Tuple2<Integer, String>> kt = new TupleTypeInfo<>(
		BasicTypeInfo.INT_TYPE_INFO,
		BasicTypeInfo.STRING_TYPE_INFO
	);

	Keys<PojoWithMultiplePojos> k1 = new Keys.SelectorFunctionKeys<>(
		new KeySelector3(),
		t1,
		kt
	);
	Keys<Tuple3<Long, Pojo1, Integer>> k2 = new Keys.SelectorFunctionKeys<>(
		new KeySelector4(),
		t2,
		kt
	);

	Assert.assertTrue(k1.areCompatible(k2));
	Assert.assertTrue(k2.areCompatible(k1));
}
 
Example 8
Source File: ExpressionKeysTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testAreCompatible4() throws Keys.IncompatibleKeysException {
	TypeInformation<PojoWithMultiplePojos> t1 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);
	TypeInformation<Tuple3<String, Long, Integer>> t2 = new TupleTypeInfo<>(
		BasicTypeInfo.STRING_TYPE_INFO,
		BasicTypeInfo.LONG_TYPE_INFO,
		BasicTypeInfo.INT_TYPE_INFO
	);

	ExpressionKeys<PojoWithMultiplePojos> ek1 = new ExpressionKeys<>(new String[]{"p1", "i0"}, t1);
	ExpressionKeys<Tuple3<String, Long, Integer>> ek2 = new ExpressionKeys<>(new int[]{0, 0, 2}, t2);

	Assert.assertTrue(ek1.areCompatible(ek2));
	Assert.assertTrue(ek2.areCompatible(ek1));
}
 
Example 9
Source File: MultidimensionalArraySerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrimitiveArray() {
	int[][] array = new int[][]{{12,1},{48,42},{23,80},{484,849},{987,4}};
	TypeInformation<int[][]> ti = TypeExtractor.getForClass(int[][].class);

	SerializerTestInstance<int[][]> testInstance = new SerializerTestInstance<int[][]>(ti.createSerializer(new ExecutionConfig()), int[][].class, -1, array);
	testInstance.testAll();
}
 
Example 10
Source File: ExpressionKeysTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test(expected = Keys.IncompatibleKeysException.class)
public void testAreCompatible7() throws Keys.IncompatibleKeysException {
	TypeInformation<Pojo1> t1 = TypeExtractor.getForClass(Pojo1.class);
	TypeInformation<Tuple2<String, Long>> t2 = new TupleTypeInfo<>(
		BasicTypeInfo.STRING_TYPE_INFO,
		BasicTypeInfo.LONG_TYPE_INFO
	);

	ExpressionKeys<Pojo1> ek1 = new ExpressionKeys<>(new String[]{"a", "b"}, t1);
	ExpressionKeys<Tuple2<String, Long>> ek2 = new ExpressionKeys<>(0, t2);

	ek1.areCompatible(ek2);
}
 
Example 11
Source File: HadoopMapFunction.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public TypeInformation<Tuple2<KEYOUT, VALUEOUT>> getProducedType() {
	Class<KEYOUT> outKeyClass = (Class<KEYOUT>) TypeExtractor.getParameterType(Mapper.class, mapper.getClass(), 2);
	Class<VALUEOUT> outValClass = (Class<VALUEOUT>) TypeExtractor.getParameterType(Mapper.class, mapper.getClass(), 3);

	final TypeInformation<KEYOUT> keyTypeInfo = TypeExtractor.getForClass((Class<KEYOUT>) outKeyClass);
	final TypeInformation<VALUEOUT> valueTypleInfo = TypeExtractor.getForClass((Class<VALUEOUT>) outValClass);
	return new TupleTypeInfo<Tuple2<KEYOUT, VALUEOUT>>(keyTypeInfo, valueTypleInfo);
}
 
Example 12
Source File: SelectorFunctionKeysTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testAreCompatible3() throws Keys.IncompatibleKeysException {
	TypeInformation<String> t1 = BasicTypeInfo.STRING_TYPE_INFO;
	TypeInformation<Pojo2> t2 = TypeExtractor.getForClass(Pojo2.class);

	Keys.ExpressionKeys<String> ek1 = new Keys.ExpressionKeys<>("*", t1);
	Keys<Pojo2> sk2 = new Keys.SelectorFunctionKeys<>(
		new KeySelector1(),
		t2,
		BasicTypeInfo.STRING_TYPE_INFO
	);

	Assert.assertTrue(sk2.areCompatible(ek1));
}
 
Example 13
Source File: PojoTypeInformationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecursivePojoTypeExtraction() {
	// This one tests whether a recursive pojo is detected using the set of visited
	// types in the type extractor. The recursive field will be handled using the generic serializer.
	TypeInformation<Recursive1Pojo> type = TypeExtractor.getForClass(Recursive1Pojo.class);
	assertTrue("Extracted type is not a Pojo type but should be.", type instanceof CompositeType);
}
 
Example 14
Source File: ExpressionKeysTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAreCompatible4() throws Keys.IncompatibleKeysException {
	TypeInformation<PojoWithMultiplePojos> t1 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);
	TypeInformation<Tuple3<String, Long, Integer>> t2 = new TupleTypeInfo<>(
		BasicTypeInfo.STRING_TYPE_INFO,
		BasicTypeInfo.LONG_TYPE_INFO,
		BasicTypeInfo.INT_TYPE_INFO
	);

	ExpressionKeys<PojoWithMultiplePojos> ek1 = new ExpressionKeys<>(new String[]{"p1", "i0"}, t1);
	ExpressionKeys<Tuple3<String, Long, Integer>> ek2 = new ExpressionKeys<>(new int[]{0, 0, 2}, t2);

	Assert.assertTrue(ek1.areCompatible(ek2));
	Assert.assertTrue(ek2.areCompatible(ek1));
}
 
Example 15
Source File: TypeInformationSerializationSchemaTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializability() {
	try {
		TypeInformation<MyPOJO> info = TypeExtractor.getForClass(MyPOJO.class);
		TypeInformationSerializationSchema<MyPOJO> schema =
				new TypeInformationSerializationSchema<MyPOJO>(info, new ExecutionConfig());

		// this needs to succeed
		CommonTestUtils.createCopySerializable(schema);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example 16
Source File: HadoopReduceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public TypeInformation<Tuple2<KEYOUT, VALUEOUT>> getProducedType() {
	Class<KEYOUT> outKeyClass = (Class<KEYOUT>) TypeExtractor.getParameterType(Reducer.class, reducer.getClass(), 2);
	Class<VALUEOUT> outValClass = (Class<VALUEOUT>) TypeExtractor.getParameterType(Reducer.class, reducer.getClass(), 3);

	final TypeInformation<KEYOUT> keyTypeInfo = TypeExtractor.getForClass((Class<KEYOUT>) outKeyClass);
	final TypeInformation<VALUEOUT> valueTypleInfo = TypeExtractor.getForClass((Class<VALUEOUT>) outValClass);
	return new TupleTypeInfo<Tuple2<KEYOUT, VALUEOUT>>(keyTypeInfo, valueTypleInfo);
}
 
Example 17
Source File: PojoGenericTypeSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> TypeSerializer<T> createSerializer(Class<T> type) {
	TypeInformation<T> typeInfo = TypeExtractor.getForClass(type);
	return typeInfo.createSerializer(new ExecutionConfig());
}
 
Example 18
Source File: MessageAcknowledgingSourceBase.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new MessageAcknowledgingSourceBase for IDs of the given type.
 *
 * @param idClass The class of the message ID type, used to create a serializer for the message IDs.
 */
protected MessageAcknowledgingSourceBase(Class<UId> idClass) {
	this(TypeExtractor.getForClass(idClass));
}
 
Example 19
Source File: ExecutionEnvironment.java    From Flink-CEPplus with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link DataSet} that represents the primitive type produced by reading the given file in delimited way.
 * This method is similar to {@link #readCsvFile(String)} with single field, but it produces a DataSet not through
 * {@link org.apache.flink.api.java.tuple.Tuple1}.
 *
 * @param filePath The path of the file, as a URI (e.g., "file:///some/local/file" or "hdfs://host:port/file/path").
 * @param delimiter The delimiter of the given file.
 * @param typeClass The primitive type class to be read.
 * @return A {@link DataSet} that represents the data read from the given file as primitive type.
 */
public <X> DataSource<X> readFileOfPrimitives(String filePath, String delimiter, Class<X> typeClass) {
	Preconditions.checkNotNull(filePath, "The file path may not be null.");

	return new DataSource<>(this, new PrimitiveInputFormat<>(new Path(filePath), delimiter, typeClass), TypeExtractor.getForClass(typeClass), Utils.getCallLocationName());
}
 
Example 20
Source File: ExecutionEnvironment.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link DataSet} that represents the primitive type produced by reading the given file line wise.
 * This method is similar to {@link #readCsvFile(String)} with single field, but it produces a DataSet not through
 * {@link org.apache.flink.api.java.tuple.Tuple1}.
 *
 * @param filePath The path of the file, as a URI (e.g., "file:///some/local/file" or "hdfs://host:port/file/path").
 * @param typeClass The primitive type class to be read.
 * @return A {@link DataSet} that represents the data read from the given file as primitive type.
 */
public <X> DataSource<X> readFileOfPrimitives(String filePath, Class<X> typeClass) {
	Preconditions.checkNotNull(filePath, "The file path may not be null.");

	return new DataSource<>(this, new PrimitiveInputFormat<>(new Path(filePath), typeClass), TypeExtractor.getForClass(typeClass), Utils.getCallLocationName());
}