Class TypeDescriptors


  • public class TypeDescriptors
    extends java.lang.Object
    A utility class for creating TypeDescriptor objects for different types, such as Java primitive types, containers and KVs of other TypeDescriptor objects, and extracting type variables of parameterized types (e.g. extracting the OutputT type variable of a DoFn<InputT, OutputT>).
    • Constructor Detail

      • TypeDescriptors

        public TypeDescriptors()
    • Method Detail

      • bigdecimals

        public static TypeDescriptor<java.math.BigDecimal> bigdecimals()
        The TypeDescriptor for BigDecimal. This is the equivalent of:
         new TypeDescriptor<BigDecimal>() {};
         
        Returns:
        A TypeDescriptor for BigDecimal
      • bigintegers

        public static TypeDescriptor<java.math.BigInteger> bigintegers()
        The TypeDescriptor for BigInteger. This is the equivalent of:
         new TypeDescriptor<BigInteger>() {};
         
        Returns:
        A TypeDescriptor for BigInteger
      • characters

        public static TypeDescriptor<java.lang.Character> characters()
        The TypeDescriptor for Character. This is the equivalent of:
         new TypeDescriptor<Character>() {};
         
        Returns:
        A TypeDescriptor for Character
      • kvs

        public static <K,​V> TypeDescriptor<KV<K,​V>> kvs​(TypeDescriptor<K> key,
                                                                    TypeDescriptor<V> value)
        The TypeDescriptor for KV. This is the equivalent of:
         new TypeDescriptor<KV<K,V>>() {};
         

        Example of use:

        
         PCollection<String> words = ...;
         PCollection<KV<String, String>> words = words.apply(FlatMapElements
                 .into(TypeDescriptors.kv(TypeDescriptors.strings(), TypeDescriptors.strings()))
                 .via(...));
         
        Parameters:
        key - The TypeDescriptor for the key
        value - The TypeDescriptor for the value
        Returns:
        A TypeDescriptor for KV
      • sets

        public static <T> TypeDescriptor<java.util.Set<T>> sets​(TypeDescriptor<T> element)
        The TypeDescriptor for Set. This is the equivalent of:
         new TypeDescriptor<Set<E>>() {};
         

        Example of use:

        
         PCollection<String> words = ...;
         PCollection<Set<String>> words = words.apply(FlatMapElements
                 .into(TypeDescriptors.sets(TypeDescriptors.strings()))
                 .via(...));
         
        Parameters:
        element - The TypeDescriptor for the set
        Returns:
        A TypeDescriptor for Set
      • lists

        public static <T> TypeDescriptor<java.util.List<T>> lists​(TypeDescriptor<T> element)
        The TypeDescriptor for List. This is the equivalent of:
         new TypeDescriptor<List<E>>() {};
         

        Example of use:

        
         PCollection<String> words = ...;
         PCollection<List<String>> words = words.apply(FlatMapElements
                 .into(TypeDescriptors.lists(TypeDescriptors.strings()))
                 .via(...));
         
        Parameters:
        element - The TypeDescriptor for the list
        Returns:
        A TypeDescriptor for List
      • iterables

        public static <T> TypeDescriptor<java.lang.Iterable<T>> iterables​(TypeDescriptor<T> iterable)
        The TypeDescriptor for Iterable. This is the equivalent of:
         new TypeDescriptor<Iterable<E>>() {};
         

        Example of use:

        
         PCollection<String> words = ...;
         PCollection<Iterable<String>> words = words.apply(FlatMapElements
                 .into(TypeDescriptors.iterables(TypeDescriptors.strings()))
                 .via(...));
         
        Parameters:
        iterable - The TypeDescriptor for the iterable
        Returns:
        A TypeDescriptor for Iterable
      • extractFromTypeParameters

        public static <T,​V> TypeDescriptor<V> extractFromTypeParameters​(@NonNull T instance,
                                                                              java.lang.Class<? super T> supertype,
                                                                              TypeDescriptors.TypeVariableExtractor<T,​V> extractor)
        Extracts a type from the actual type parameters of a parameterized class, subject to Java type erasure. The type to extract is specified in a way that is safe w.r.t. changing the type signature of the parameterized class, as opposed to specifying the name or index of a type variable.

        Example of use:

        
         class Foo<BarT> {
           private ProcessFunction<BarT, String> fn;
        
           TypeDescriptor<BarT> inferBarTypeDescriptorFromFn() {
             return TypeDescriptors.extractFromTypeParameters(
               fn,
               ProcessFunction.class,
               // The actual type of "fn" is matched against the input type of the extractor,
               // and the obtained values of type variables of the superclass are substituted
               // into the output type of the extractor.
               new TypeVariableExtractor<ProcessFunction<BarT, String>, BarT>() {});
           }
         }
         
        Parameters:
        instance - The object being analyzed
        supertype - Parameterized superclass of interest
        extractor - A class for specifying the type to extract from the supertype
        Returns:
        A TypeDescriptor for the actual value of the result type of the extractor, potentially containing unresolved type variables if the type was erased.
      • inputOf

        public static <InputT,​OutputT> TypeDescriptor<InputT> inputOf​(ProcessFunction<InputT,​OutputT> fn)
        Returns a type descriptor for the input of the given ProcessFunction, subject to Java type erasure: may contain unresolved type variables if the type was erased.
      • outputOf

        public static <InputT,​OutputT> TypeDescriptor<OutputT> outputOf​(ProcessFunction<InputT,​OutputT> fn)
        Returns a type descriptor for the output of the given ProcessFunction, subject to Java type erasure: may contain unresolved type variables if the type was erased.