Class Types


  • @PublicEvolving
    public class Types
    extends Object
    This class gives access to the type information of the most common types for which Flink has built-in serializers and comparators.

    In many cases, Flink tries to analyze generic signatures of functions to determine return types automatically. This class is intended for cases where type information has to be supplied manually or cases where automatic type inference results in an inefficient type.

    Please note that the Scala API and Table API have dedicated Types classes. (See org.apache.flink.api.scala.Types and org.apache.flink.table.api.Types)

    A more convenient alternative might be a TypeHint.

    See Also:
    specify type information based on a class that will be analyzed, specify type information based on a
    • Constructor Detail

      • Types

        public Types()
    • Method Detail

      • ROW

        public static TypeInformation<Row> ROW​(TypeInformation<?>... types)
        Returns type information for Row with fields of the given types. A row itself must not be null.

        A row is a fixed-length, null-aware composite type for storing multiple values in a deterministic field order. Every field can be null regardless of the field's type. The type of row fields cannot be automatically inferred; therefore, it is required to provide type information whenever a row is produced.

        The schema of rows can have up to Integer.MAX_VALUE fields, however, all row instances must strictly adhere to the schema defined by the type info.

        This method generates type information with fields of the given types; the fields have the default names (f0, f1, f2 ..).

        Parameters:
        types - The types of the row fields, e.g., Types.STRING, Types.INT
      • ROW_NAMED

        public static TypeInformation<Row> ROW_NAMED​(String[] fieldNames,
                                                     TypeInformation<?>... types)
        Returns type information for Row with fields of the given types and with given names. A row must not be null.

        A row is a fixed-length, null-aware composite type for storing multiple values in a deterministic field order. Every field can be null independent of the field's type. The type of row fields cannot be automatically inferred; therefore, it is required to provide type information whenever a row is used.

        The schema of rows can have up to Integer.MAX_VALUE fields, however, all row instances must strictly adhere to the schema defined by the type info.

        Example use: ROW_NAMED(new String[]{"name", "number"}, Types.STRING, Types.INT).

        Parameters:
        fieldNames - array of field names
        types - array of field types
      • TUPLE

        public static <T extends TupleTypeInformation<T> TUPLE​(TypeInformation<?>... types)
        Returns type information for subclasses of Flink's Tuple (namely Tuple0 till Tuple25) with fields of the given types. A tuple must not be null.

        A tuple is a fixed-length composite type for storing multiple values in a deterministic field order. Fields of a tuple are typed. Tuples are the most efficient composite type; a tuple does not support null-valued fields unless the type of the field supports nullability.

        Parameters:
        types - The types of the tuple fields, e.g., Types.STRING, Types.INT
      • TUPLE

        public static <T extends TupleTypeInformation<T> TUPLE​(Class<T> tupleSubclass)
        Returns type information for typed subclasses of Flink's Tuple. Typed subclassed are classes that extend Tuple0 till Tuple25 to provide types for all fields and might add additional getters and setters for better readability. Additional member fields must not be added. A tuple must not be null.

        A tuple is a fixed-length composite type for storing multiple values in a deterministic field order. Fields of a tuple are typed. Tuples are the most efficient composite type; a tuple does not support null-valued fields unless the type of the field supports nullability.

        The generic types for all fields of the tuple can be defined in a hierarchy of subclasses.

        If Flink's type analyzer is unable to extract a tuple type information with type information for all fields, an InvalidTypesException is thrown.

        Example use:

        
           class MyTuple extends Tuple2<Integer, String> {
        
             public int getId() { return f0; }
        
             public String getName() { return f1; }
           }
         
        
         Types.TUPLE(MyTuple.class)
         
        Parameters:
        tupleSubclass - A subclass of Tuple0 till Tuple25 that defines all field types and does not add any additional fields
      • POJO

        public static <T> TypeInformation<T> POJO​(Class<T> pojoClass)
        Returns type information for a POJO (Plain Old Java Object).

        A POJO class is public and standalone (no non-static inner class). It has a public no-argument constructor. All non-static, non-transient fields in the class (and all superclasses) are either public (and non-final) or have a public getter and a setter method that follows the Java beans naming conventions for getters and setters.

        A POJO is a fixed-length and null-aware composite type. Every field can be null independent of the field's type.

        The generic types for all fields of the POJO can be defined in a hierarchy of subclasses.

        Java Record classes can also be used as valid POJOs (even though they don't fulfill some of the above criteria). In this case Flink will use the record canonical constructor to create the objects.

        If Flink's type analyzer is unable to extract a valid POJO type information with type information for all fields, an InvalidTypesException is thrown. Alternatively, you can use POJO(Class, Map) to specify all fields manually.

        Parameters:
        pojoClass - POJO class to be analyzed by Flink
      • POJO

        public static <T> TypeInformation<T> POJO​(Class<T> pojoClass,
                                                  Map<String,​TypeInformation<?>> fields)
        Returns type information for a POJO (Plain Old Java Object) and allows to specify all fields manually.

        A type is considered a FLink POJO type, if it fulfills the conditions below.

        • It is a public class, and standalone (not a non-static inner class)
        • It has a public no-argument constructor.
        • All non-static, non-transient fields in the class (and all superclasses) are either public (and non-final) or have a public getter and a setter method that follows the Java beans naming conventions for getters and setters.
        • It is a fixed-length, null-aware composite type with non-deterministic field order. Every field can be null independent of the field's type.

        The generic types for all fields of the POJO can be defined in a hierarchy of subclasses.

        If Flink's type analyzer is unable to extract a POJO field, an InvalidTypesException is thrown.

        Note: In most cases the type information of fields can be determined automatically, we recommend to use POJO(Class).

        Parameters:
        pojoClass - POJO class
        fields - map of fields that map a name to type information. The map key is the name of the field and the value is its type.
      • GENERIC

        public static <T> TypeInformation<T> GENERIC​(Class<T> genericClass)
        Returns generic type information for any Java object. The serialization logic will use the general purpose serializer Kryo.

        Generic types are black-boxes for Flink, but allow any object and null values in fields.

        By default, serialization of this type is not very efficient. Please read the documentation about how to improve efficiency (namely by pre-registering classes).

        Parameters:
        genericClass - any Java class
      • PRIMITIVE_ARRAY

        public static TypeInformation<?> PRIMITIVE_ARRAY​(TypeInformation<?> elementType)
        Returns type information for Java arrays of primitive type (such as byte[]). The array must not be null.
        Parameters:
        elementType - element type of the array (e.g. Types.BOOLEAN, Types.INT, Types.DOUBLE)
      • OBJECT_ARRAY

        public static <E> TypeInformation<E[]> OBJECT_ARRAY​(TypeInformation<E> elementType)
        Returns type information for Java arrays of object types (such as String[], Integer[]). The array itself must not be null. Null values for elements are supported.
        Parameters:
        elementType - element type of the array
      • VALUE

        public static <V extends ValueTypeInformation<V> VALUE​(Class<V> valueType)
        Returns type information for Flink value types (classes that implement Value). Built-in value types do not support null values (except for StringValue).

        Value types describe their serialization and deserialization manually. Instead of going through a general purpose serialization framework. A value type is reasonable when general purpose serialization would be highly inefficient. The wrapped value can be altered, allowing programmers to reuse objects and take pressure off the garbage collector.

        Flink provides built-in value types for all Java primitive types (such as BooleanValue, IntValue) as well as StringValue, NullValue, ListValue, and MapValue.

        Parameters:
        valueType - class that implements Value
      • MAP

        public static <K,​V> TypeInformation<Map<K,​V>> MAP​(TypeInformation<K> keyType,
                                                                      TypeInformation<V> valueType)
        Returns type information for a Java Map. A map must not be null. Null values in keys are not supported. An entry's value can be null.

        By default, maps are untyped and treated as a generic type in Flink; therefore, it is useful to pass type information whenever a map is used.

        Note: Flink does not preserve the concrete Map type. It converts a map into HashMap when copying or deserializing.

        Parameters:
        keyType - type information for the map's keys
        valueType - type information for the map's values
      • LIST

        public static <E> TypeInformation<List<E>> LIST​(TypeInformation<E> elementType)
        Returns type information for a Java List. A list must not be null. Null values in elements are not supported.

        By default, lists are untyped and treated as a generic type in Flink; therefore, it is useful to pass type information whenever a list is used.

        Note: Flink does not preserve the concrete List type. It converts a list into ArrayList when copying or deserializing.

        Parameters:
        elementType - type information for the list's elements
      • ENUM

        public static <E extends Enum<E>> TypeInformation<E> ENUM​(Class<E> enumType)
        Returns type information for Java enumerations. Null values are not supported.
        Parameters:
        enumType - enumeration class extending Enum
      • EITHER

        public static <L,​R> TypeInformation<Either<L,​R>> EITHER​(TypeInformation<L> leftType,
                                                                            TypeInformation<R> rightType)
        Returns type information for Flink's Either type. Null values are not supported.

        Either type can be used for a value of two possible types.

        Example use: Types.EITHER(Types.VOID, Types.INT)

        Parameters:
        leftType - type information of left side / Either.Left
        rightType - type information of right side / Either.Right