org.apache.flink.api.common.functions.InvalidTypesException Java Examples
The following examples show how to use
org.apache.flink.api.common.functions.InvalidTypesException.
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: TypeExtractor.java From flink with Apache License 2.0 | 6 votes |
/** * Returns the type information factory for a type using the factory registry or annotations. */ @Internal public static <OUT> TypeInfoFactory<OUT> getTypeInfoFactory(Type t) { final Class<?> factoryClass; if (registeredTypeInfoFactories.containsKey(t)) { factoryClass = registeredTypeInfoFactories.get(t); } else { if (!isClassType(t) || !typeToClass(t).isAnnotationPresent(TypeInfo.class)) { return null; } final TypeInfo typeInfoAnnotation = typeToClass(t).getAnnotation(TypeInfo.class); factoryClass = typeInfoAnnotation.value(); // check for valid factory class if (!TypeInfoFactory.class.isAssignableFrom(factoryClass)) { throw new InvalidTypesException("TypeInfo annotation does not specify a valid TypeInfoFactory."); } } // instantiate return (TypeInfoFactory<OUT>) InstantiationUtil.instantiate(factoryClass); }
Example #2
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleSupertype() { RichMapFunction<?, ?> function = new RichMapFunction<String, Tuple>() { private static final long serialVersionUID = 1L; @Override public Tuple map(String value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) Types.STRING, "name", true); Assert.assertTrue(ti instanceof MissingTypeInfo); try { TypeExtractor.getMapReturnTypes(function, (TypeInformation) Types.STRING); Assert.fail("Expected an exception"); } catch (InvalidTypesException e) { // expected } }
Example #3
Source File: KeySelectorUtil.java From flink with Apache License 2.0 | 6 votes |
public static <X> KeySelector<X, Tuple> getSelectorForKeys(Keys<X> keys, TypeInformation<X> typeInfo, ExecutionConfig executionConfig) { if (!(typeInfo instanceof CompositeType)) { throw new InvalidTypesException( "This key operation requires a composite type such as Tuples, POJOs, or Case Classes."); } CompositeType<X> compositeType = (CompositeType<X>) typeInfo; int[] logicalKeyPositions = keys.computeLogicalKeyPositions(); int numKeyFields = logicalKeyPositions.length; TypeInformation<?>[] typeInfos = keys.getKeyFieldTypes(); // use ascending order here, the code paths for that are usually a slight bit faster boolean[] orders = new boolean[numKeyFields]; for (int i = 0; i < numKeyFields; i++) { orders[i] = true; } TypeComparator<X> comparator = compositeType.createComparator(logicalKeyPositions, orders, 0, executionConfig); return new ComparableKeySelector<>(comparator, numKeyFields, new TupleTypeInfo<>(typeInfos)); }
Example #4
Source File: TypeExtractionUtils.java From flink with Apache License 2.0 | 6 votes |
/** * This method extracts the n-th type argument from the given type. An InvalidTypesException * is thrown if the type does not have any type arguments or if the index exceeds the number * of type arguments. * * @param t Type to extract the type arguments from * @param index Index of the type argument to extract * @return The extracted type argument * @throws InvalidTypesException if the given type does not have any type arguments or if the * index exceeds the number of type arguments. */ public static Type extractTypeArgument(Type t, int index) throws InvalidTypesException { if (t instanceof ParameterizedType) { Type[] actualTypeArguments = ((ParameterizedType) t).getActualTypeArguments(); if (index < 0 || index >= actualTypeArguments.length) { throw new InvalidTypesException("Cannot extract the type argument with index " + index + " because the type has only " + actualTypeArguments.length + " type arguments."); } else { return actualTypeArguments[index]; } } else { throw new InvalidTypesException("The given type " + t + " is not a parameterized type."); } }
Example #5
Source File: KeySelectorUtil.java From flink with Apache License 2.0 | 6 votes |
public static <X> KeySelector<X, Tuple> getSelectorForKeys(Keys<X> keys, TypeInformation<X> typeInfo, ExecutionConfig executionConfig) { if (!(typeInfo instanceof CompositeType)) { throw new InvalidTypesException( "This key operation requires a composite type such as Tuples, POJOs, or Case Classes."); } CompositeType<X> compositeType = (CompositeType<X>) typeInfo; int[] logicalKeyPositions = keys.computeLogicalKeyPositions(); int numKeyFields = logicalKeyPositions.length; TypeInformation<?>[] typeInfos = keys.getKeyFieldTypes(); // use ascending order here, the code paths for that are usually a slight bit faster boolean[] orders = new boolean[numKeyFields]; for (int i = 0; i < numKeyFields; i++) { orders[i] = true; } TypeComparator<X> comparator = compositeType.createComparator(logicalKeyPositions, orders, 0, executionConfig); return new ComparableKeySelector<>(comparator, numKeyFields, new TupleTypeInfo<>(typeInfos)); }
Example #6
Source File: TypeExtractor.java From flink with Apache License 2.0 | 6 votes |
/** * Returns the type information factory for a type using the factory registry or annotations. */ @Internal public static <OUT> TypeInfoFactory<OUT> getTypeInfoFactory(Type t) { final Class<?> factoryClass; if (registeredTypeInfoFactories.containsKey(t)) { factoryClass = registeredTypeInfoFactories.get(t); } else { if (!isClassType(t) || !typeToClass(t).isAnnotationPresent(TypeInfo.class)) { return null; } final TypeInfo typeInfoAnnotation = typeToClass(t).getAnnotation(TypeInfo.class); factoryClass = typeInfoAnnotation.value(); // check for valid factory class if (!TypeInfoFactory.class.isAssignableFrom(factoryClass)) { throw new InvalidTypesException("TypeInfo annotation does not specify a valid TypeInfoFactory."); } } // instantiate return (TypeInfoFactory<OUT>) InstantiationUtil.instantiate(factoryClass); }
Example #7
Source File: TypeExtractor.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
/** * Returns the type information factory for a type using the factory registry or annotations. */ @Internal public static <OUT> TypeInfoFactory<OUT> getTypeInfoFactory(Type t) { final Class<?> factoryClass; if (registeredTypeInfoFactories.containsKey(t)) { factoryClass = registeredTypeInfoFactories.get(t); } else { if (!isClassType(t) || !typeToClass(t).isAnnotationPresent(TypeInfo.class)) { return null; } final TypeInfo typeInfoAnnotation = typeToClass(t).getAnnotation(TypeInfo.class); factoryClass = typeInfoAnnotation.value(); // check for valid factory class if (!TypeInfoFactory.class.isAssignableFrom(factoryClass)) { throw new InvalidTypesException("TypeInfo annotation does not specify a valid TypeInfoFactory."); } } // instantiate return (TypeInfoFactory<OUT>) InstantiationUtil.instantiate(factoryClass); }
Example #8
Source File: EitherTypeInfoFactory.java From flink with Apache License 2.0 | 6 votes |
@Override public TypeInformation<Either<L, R>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParameters) { TypeInformation<?> leftType = genericParameters.get("L"); TypeInformation<?> rightType = genericParameters.get("R"); if (leftType == null) { throw new InvalidTypesException("Type extraction is not possible on Either" + " type as it does not contain information about the 'left' type."); } if (rightType == null) { throw new InvalidTypesException("Type extraction is not possible on Either" + " type as it does not contain information about the 'right' type."); } return new EitherTypeInfo(leftType, rightType); }
Example #9
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testValueSupertypeException() { RichMapFunction<?, ?> function = new RichMapFunction<StringValue, Value>() { private static final long serialVersionUID = 1L; @Override public Value map(StringValue value) throws Exception { return null; } }; TypeInformation<?> ti =TypeExtractor.getMapReturnTypes(function, (TypeInformation)TypeInformation.of(new TypeHint<StringValue>(){}), "name", true); Assert.assertTrue(ti instanceof MissingTypeInfo); try { TypeExtractor.getMapReturnTypes(function, (TypeInformation)TypeInformation.of(new TypeHint<StringValue>(){})); Assert.fail("Expected an exception"); } catch (InvalidTypesException e) { // expected } }
Example #10
Source File: EitherTypeInfoFactory.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Override public TypeInformation<Either<L, R>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParameters) { TypeInformation<?> leftType = genericParameters.get("L"); TypeInformation<?> rightType = genericParameters.get("R"); if (leftType == null) { throw new InvalidTypesException("Type extraction is not possible on Either" + " type as it does not contain information about the 'left' type."); } if (rightType == null) { throw new InvalidTypesException("Type extraction is not possible on Either" + " type as it does not contain information about the 'right' type."); } return new EitherTypeInfo(leftType, rightType); }
Example #11
Source File: TypeExtractionUtils.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
/** * Extracts a Single Abstract Method (SAM) as defined in Java Specification (4.3.2. The Class Object, * 9.8 Functional Interfaces, 9.4.3 Interface Method Body) from given class. * * @param baseClass a class that is a FunctionalInterface to retrieve a SAM from * @throws InvalidTypesException if the given class does not implement FunctionalInterface * @return single abstract method of the given class */ public static Method getSingleAbstractMethod(Class<?> baseClass) { if (!baseClass.isInterface()) { throw new InvalidTypesException("Given class: " + baseClass + "is not a FunctionalInterface."); } Method sam = null; for (Method method : baseClass.getMethods()) { if (Modifier.isAbstract(method.getModifiers())) { if (sam == null) { sam = method; } else { throw new InvalidTypesException("Given class: " + baseClass + " is not a FunctionalInterface. It has more than one abstract method."); } } } if (sam == null) { throw new InvalidTypesException( "Given class: " + baseClass + " is not a FunctionalInterface. It does not have any abstract methods."); } return sam; }
Example #12
Source File: TypeExtractorTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testMissingTupleGenerics() { RichMapFunction<?, ?> function = new RichMapFunction<String, Tuple2>() { private static final long serialVersionUID = 1L; @Override public Tuple2 map(String value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) Types.STRING, "name", true); Assert.assertTrue(ti instanceof MissingTypeInfo); try { TypeExtractor.getMapReturnTypes(function, (TypeInformation) Types.STRING); Assert.fail("Expected an exception"); } catch (InvalidTypesException e) { // expected } }
Example #13
Source File: WritableExtractionTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Test public void testInputValidationError() { RichMapFunction<Writable, String> function = new RichMapFunction<Writable, String>() { @Override public String map(Writable value) throws Exception { return null; } }; @SuppressWarnings("unchecked") TypeInformation<Writable> inType = (TypeInformation<Writable>) (TypeInformation<?>) new WritableTypeInfo<>(DirectWritable.class); try { TypeExtractor.getMapReturnTypes(function, inType); fail("exception expected"); } catch (InvalidTypesException e) { // right } }
Example #14
Source File: SiddhiCEPITCase.java From flink-siddhi with Apache License 2.0 | 6 votes |
@Test(expected = InvalidTypesException.class) public void testUnboundedPojoSourceButReturnInvalidTupleType() throws Exception { StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); DataStream<Event> input = env.addSource(new RandomEventSource(5).closeDelay(1500)); DataStream<Tuple5<Long, Integer, String, Double, Long>> output = SiddhiCEP .define("inputStream", input, "id", "name", "price", "timestamp") .cql("from inputStream select timestamp, id, name, price insert into outputStream") .returns("outputStream"); DataStream<Long> following = output.map(new MapFunction<Tuple5<Long, Integer, String, Double, Long>, Long>() { @Override public Long map(Tuple5<Long, Integer, String, Double, Long> value) throws Exception { return value.f0; } }); String resultPath = tempFolder.newFile().toURI().toString(); following.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE); env.execute(); assertEquals(5, getLineCount(resultPath)); env.execute(); }
Example #15
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testFunctionWithMissingGenerics() { RichMapFunction function = new RichMapFunction() { private static final long serialVersionUID = 1L; @Override public String map(Object value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, Types.STRING, "name", true); Assert.assertTrue(ti instanceof MissingTypeInfo); try { TypeExtractor.getMapReturnTypes(function, Types.STRING); Assert.fail("Expected an exception"); } catch (InvalidTypesException e) { // expected } }
Example #16
Source File: WritableExtractionTest.java From flink with Apache License 2.0 | 6 votes |
@Test public void testInputValidationError() { RichMapFunction<Writable, String> function = new RichMapFunction<Writable, String>() { @Override public String map(Writable value) throws Exception { return null; } }; @SuppressWarnings("unchecked") TypeInformation<Writable> inType = (TypeInformation<Writable>) (TypeInformation<?>) new WritableTypeInfo<>(DirectWritable.class); try { TypeExtractor.getMapReturnTypes(function, inType); fail("exception expected"); } catch (InvalidTypesException e) { // right } }
Example #17
Source File: StreamExecutionEnvironment.java From flink with Apache License 2.0 | 6 votes |
/** * Ads a data source with a custom type information thus opening a * {@link DataStream}. Only in very special cases does the user need to * support type information. Otherwise use * {@link #addSource(org.apache.flink.streaming.api.functions.source.SourceFunction)} * * @param function * the user defined function * @param sourceName * Name of the data source * @param <OUT> * type of the returned stream * @param typeInfo * the user defined type information for the stream * @return the data stream constructed */ @SuppressWarnings("unchecked") public <OUT> DataStreamSource<OUT> addSource(SourceFunction<OUT> function, String sourceName, TypeInformation<OUT> typeInfo) { if (function instanceof ResultTypeQueryable) { typeInfo = ((ResultTypeQueryable<OUT>) function).getProducedType(); } if (typeInfo == null) { try { typeInfo = TypeExtractor.createTypeInfo( SourceFunction.class, function.getClass(), 0, null, null); } catch (final InvalidTypesException e) { typeInfo = (TypeInformation<OUT>) new MissingTypeInfo(sourceName, e); } } boolean isParallel = function instanceof ParallelSourceFunction; clean(function); final StreamSource<OUT, ?> sourceOperator = new StreamSource<>(function); return new DataStreamSource<>(this, typeInfo, sourceOperator, isParallel, sourceName); }
Example #18
Source File: KeySelectorUtil.java From flink with Apache License 2.0 | 6 votes |
public static <X, K> KeySelector<X, K> getSelectorForOneKey( Keys<X> keys, Partitioner<K> partitioner, TypeInformation<X> typeInfo, ExecutionConfig executionConfig) { if (!(typeInfo instanceof CompositeType)) { throw new InvalidTypesException( "This key operation requires a composite type such as Tuples, POJOs, case classes, etc"); } if (partitioner != null) { keys.validateCustomPartitioner(partitioner, null); } CompositeType<X> compositeType = (CompositeType<X>) typeInfo; int[] logicalKeyPositions = keys.computeLogicalKeyPositions(); if (logicalKeyPositions.length != 1) { throw new IllegalArgumentException("There must be exactly 1 key specified"); } TypeComparator<X> comparator = compositeType.createComparator( logicalKeyPositions, new boolean[] { true }, 0, executionConfig); return new OneKeySelector<>(comparator); }
Example #19
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testValueSupertypeException() { RichMapFunction<?, ?> function = new RichMapFunction<StringValue, Value>() { private static final long serialVersionUID = 1L; @Override public Value map(StringValue value) throws Exception { return null; } }; TypeInformation<?> ti =TypeExtractor.getMapReturnTypes(function, (TypeInformation)TypeInformation.of(new TypeHint<StringValue>(){}), "name", true); Assert.assertTrue(ti instanceof MissingTypeInfo); try { TypeExtractor.getMapReturnTypes(function, (TypeInformation)TypeInformation.of(new TypeHint<StringValue>(){})); Assert.fail("Expected an exception"); } catch (InvalidTypesException e) { // expected } }
Example #20
Source File: TypeExtractorTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Test public void testFunctionDependingOnInputWithTupleInputWithTypeMismatch() { IdentityMapper2<Boolean> function = new IdentityMapper2<Boolean>(); TypeInformation<Tuple2<Boolean, String>> inputType = new TupleTypeInfo<Tuple2<Boolean, String>>(BasicTypeInfo.BOOLEAN_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO); // input is: Tuple2<Boolean, Integer> // allowed: Tuple2<?, String> try { TypeExtractor.getMapReturnTypes(function, inputType); Assert.fail("exception expected"); } catch (InvalidTypesException e) { // right } }
Example #21
Source File: KeySelectorUtil.java From flink with Apache License 2.0 | 6 votes |
public static <X, K> KeySelector<X, K> getSelectorForOneKey( Keys<X> keys, Partitioner<K> partitioner, TypeInformation<X> typeInfo, ExecutionConfig executionConfig) { if (!(typeInfo instanceof CompositeType)) { throw new InvalidTypesException( "This key operation requires a composite type such as Tuples, POJOs, case classes, etc"); } if (partitioner != null) { keys.validateCustomPartitioner(partitioner, null); } CompositeType<X> compositeType = (CompositeType<X>) typeInfo; int[] logicalKeyPositions = keys.computeLogicalKeyPositions(); if (logicalKeyPositions.length != 1) { throw new IllegalArgumentException("There must be exactly 1 key specified"); } TypeComparator<X> comparator = compositeType.createComparator( logicalKeyPositions, new boolean[] { true }, 0, executionConfig); return new OneKeySelector<>(comparator); }
Example #22
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleSupertype() { RichMapFunction<?, ?> function = new RichMapFunction<String, Tuple>() { private static final long serialVersionUID = 1L; @Override public Tuple map(String value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) Types.STRING, "name", true); Assert.assertTrue(ti instanceof MissingTypeInfo); try { TypeExtractor.getMapReturnTypes(function, (TypeInformation) Types.STRING); Assert.fail("Expected an exception"); } catch (InvalidTypesException e) { // expected } }
Example #23
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testFunctionDependingOnUnknownInput() { IdentityMapper3<Boolean, String> function = new IdentityMapper3<Boolean, String>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.BOOLEAN_TYPE_INFO, "name", true); Assert.assertTrue(ti instanceof MissingTypeInfo); try { TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.BOOLEAN_TYPE_INFO); Assert.fail("Expected an exception"); } catch (InvalidTypesException e) { // expected } }
Example #24
Source File: OutputTag.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * Creates a new named {@code OutputTag} with the given id. * * @param id The id of the created {@code OutputTag}. */ public OutputTag(String id) { Preconditions.checkNotNull(id, "OutputTag id cannot be null."); Preconditions.checkArgument(!id.isEmpty(), "OutputTag id must not be empty."); this.id = id; try { this.typeInfo = TypeExtractor.createTypeInfo(this, OutputTag.class, getClass(), 0); } catch (InvalidTypesException e) { throw new InvalidTypesException("Could not determine TypeInformation for the OutputTag type. " + "The most common reason is forgetting to make the OutputTag an anonymous inner class. " + "It is also not possible to use generic type variables with OutputTags, such as 'Tuple2<A, B>'.", e); } }
Example #25
Source File: PrimitiveArrayTypeInfo.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * Tries to get the PrimitiveArrayTypeInfo for an array. Returns null, if the type is an array, * but the component type is not a primitive type. * * @param type The class of the array. * @return The corresponding PrimitiveArrayTypeInfo, or null, if the array is not an array of primitives. * @throws InvalidTypesException Thrown, if the given class does not represent an array. */ @SuppressWarnings("unchecked") @PublicEvolving public static <X> PrimitiveArrayTypeInfo<X> getInfoFor(Class<X> type) { if (!type.isArray()) { throw new InvalidTypesException("The given class is no array."); } // basic type arrays return (PrimitiveArrayTypeInfo<X>) TYPES.get(type); }
Example #26
Source File: OutputTag.java From flink with Apache License 2.0 | 5 votes |
/** * Creates a new named {@code OutputTag} with the given id. * * @param id The id of the created {@code OutputTag}. */ public OutputTag(String id) { Preconditions.checkNotNull(id, "OutputTag id cannot be null."); Preconditions.checkArgument(!id.isEmpty(), "OutputTag id must not be empty."); this.id = id; try { this.typeInfo = TypeExtractor.createTypeInfo(this, OutputTag.class, getClass(), 0); } catch (InvalidTypesException e) { throw new InvalidTypesException("Could not determine TypeInformation for the OutputTag type. " + "The most common reason is forgetting to make the OutputTag an anonymous inner class. " + "It is also not possible to use generic type variables with OutputTags, such as 'Tuple2<A, B>'.", e); } }
Example #27
Source File: PrimitiveArrayTypeInfo.java From flink with Apache License 2.0 | 5 votes |
/** * Tries to get the PrimitiveArrayTypeInfo for an array. Returns null, if the type is an array, * but the component type is not a primitive type. * * @param type The class of the array. * @return The corresponding PrimitiveArrayTypeInfo, or null, if the array is not an array of primitives. * @throws InvalidTypesException Thrown, if the given class does not represent an array. */ @SuppressWarnings("unchecked") @PublicEvolving public static <X> PrimitiveArrayTypeInfo<X> getInfoFor(Class<X> type) { if (!type.isArray()) { throw new InvalidTypesException("The given class is no array."); } // basic type arrays return (PrimitiveArrayTypeInfo<X>) TYPES.get(type); }
Example #28
Source File: TableFunction.java From flink with Apache License 2.0 | 5 votes |
/** * Returns {@link TypeInformation} about the operands of the evaluation method with a given * signature. * * @deprecated This method uses the old type system and is based on the old reflective extraction * logic. The method will be removed in future versions and is only called when using * the deprecated {@code TableEnvironment.registerFunction(...)} method. The new reflective * extraction logic (possibly enriched with {@link DataTypeHint} and {@link FunctionHint}) * should be powerful enough to cover most use cases. For advanced users, it is possible * to override {@link UserDefinedFunction#getTypeInference(DataTypeFactory)}. */ @Deprecated public TypeInformation<?>[] getParameterTypes(Class<?>[] signature) { final TypeInformation<?>[] types = new TypeInformation<?>[signature.length]; for (int i = 0; i < signature.length; i++) { try { types[i] = TypeExtractor.getForClass(signature[i]); } catch (InvalidTypesException e) { throw new ValidationException( "Parameter types of table function " + this.getClass().getCanonicalName() + " cannot be automatically determined. Please provide type information manually."); } } return types; }
Example #29
Source File: TypeInformation.java From flink with Apache License 2.0 | 5 votes |
/** * Creates a TypeInformation for the type described by the given class. * * <p>This method only works for non-generic types. For generic types, use the * {@link #of(TypeHint)} method. * * @param typeClass The class of the type. * @param <T> The generic type. * * @return The TypeInformation object for the type described by the hint. */ public static <T> TypeInformation<T> of(Class<T> typeClass) { try { return TypeExtractor.createTypeInfo(typeClass); } catch (InvalidTypesException e) { throw new FlinkRuntimeException( "Cannot extract TypeInformation from Class alone, because generic parameters are missing. " + "Please use TypeInformation.of(TypeHint) instead, or another equivalent method in the API that " + "accepts a TypeHint instead of a Class. " + "For example for a Tuple2<Long, String> pass a 'new TypeHint<Tuple2<Long, String>>(){}'."); } }
Example #30
Source File: TypeExtractor.java From flink with Apache License 2.0 | 5 votes |
static void validateIfWritable(TypeInformation<?> typeInfo, Type type) { try { // try to load the writable type info Class<?> writableTypeInfoClass = Class .forName(HADOOP_WRITABLE_TYPEINFO_CLASS, false, typeInfo.getClass().getClassLoader()); if (writableTypeInfoClass.isAssignableFrom(typeInfo.getClass())) { // this is actually a writable type info // check if the type is a writable if (!(type instanceof Class && isHadoopWritable((Class<?>) type))) { throw new InvalidTypesException(HADOOP_WRITABLE_CLASS + " type expected."); } // check writable type contents Class<?> clazz = (Class<?>) type; if (typeInfo.getTypeClass() != clazz) { throw new InvalidTypesException("Writable type '" + typeInfo.getTypeClass().getCanonicalName() + "' expected but was '" + clazz.getCanonicalName() + "'."); } } } catch (ClassNotFoundException e) { // class not present at all, so cannot be that type info // ignore } }