Java Code Examples for org.apache.beam.sdk.values.TypeDescriptor#of()

The following examples show how to use org.apache.beam.sdk.values.TypeDescriptor#of() . 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: SchemaRegistry.java    From beam with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public <T> SerializableFunction<Row, T> fromRowFunction(TypeDescriptor<T> typeDescriptor) {
  TypeDescriptor<?> type = typeDescriptor;
  do {
    SchemaProvider schemaProvider = providers.get(type);
    if (schemaProvider != null) {
      return (SerializableFunction<Row, T>) schemaProvider.fromRowFunction(type);
    }
    Class<?> superClass = type.getRawType().getSuperclass();
    if (superClass == null || superClass.equals(Object.class)) {
      return null;
    }
    type = TypeDescriptor.of(superClass);
  } while (true);
}
 
Example 2
Source File: SchemaRegistry.java    From beam with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public <T> Schema schemaFor(TypeDescriptor<T> typeDescriptor) {
  TypeDescriptor<?> type = typeDescriptor;
  do {
    SchemaProvider schemaProvider = providers.get(type);
    if (schemaProvider != null) {
      return schemaProvider.schemaFor(type);
    }
    Class<?> superClass = type.getRawType().getSuperclass();
    if (superClass == null || superClass.equals(Object.class)) {
      return null;
    }
    type = TypeDescriptor.of(superClass);
  } while (true);
}
 
Example 3
Source File: FieldValueTypeInformation.java    From beam with Apache License 2.0 6 votes vote down vote up
public static FieldValueTypeInformation forGetter(Method method) {
  String name;
  if (method.getName().startsWith("get")) {
    name = ReflectUtils.stripPrefix(method.getName(), "get");
  } else if (method.getName().startsWith("is")) {
    name = ReflectUtils.stripPrefix(method.getName(), "is");
  } else {
    throw new RuntimeException("Getter has wrong prefix " + method.getName());
  }

  TypeDescriptor type = TypeDescriptor.of(method.getGenericReturnType());
  boolean nullable = method.isAnnotationPresent(Nullable.class);
  return new AutoValue_FieldValueTypeInformation.Builder()
      .setName(name)
      .setNullable(nullable)
      .setType(type)
      .setRawType(type.getRawType())
      .setMethod(method)
      .setElementType(getIterableComponentType(type))
      .setMapKeyType(getMapKeyType(type))
      .setMapValueType(getMapValueType(type))
      .setOneOfTypes(Collections.emptyMap())
      .build();
}
 
Example 4
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <ElementT> TypeDescriptor<Iterable<ElementT>> createIterableType(
    TypeDescriptor<?> componentType) {
  TypeDescriptor wrappedComponentType =
      TypeDescriptor.of(ClassUtils.primitiveToWrapper(componentType.getRawType()));
  return new TypeDescriptor<Iterable<ElementT>>() {}.where(
      new TypeParameter<ElementT>() {}, wrappedComponentType);
}
 
Example 5
Source File: FieldValueTypeInformation.java    From beam with Apache License 2.0 5 votes vote down vote up
public static FieldValueTypeInformation forField(Field field) {
  TypeDescriptor type = TypeDescriptor.of(field.getGenericType());
  return new AutoValue_FieldValueTypeInformation.Builder()
      .setName(field.getName())
      .setNullable(field.isAnnotationPresent(Nullable.class))
      .setType(type)
      .setRawType(type.getRawType())
      .setField(field)
      .setElementType(getIterableComponentType(field))
      .setMapKeyType(getMapKeyType(field))
      .setMapValueType(getMapValueType(field))
      .setOneOfTypes(Collections.emptyMap())
      .build();
}
 
Example 6
Source File: SerializableCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public TypeDescriptor<T> getEncodedTypeDescriptor() {
  if (typeDescriptor == null) {
    typeDescriptor = TypeDescriptor.of(type);
  }
  return typeDescriptor;
}
 
Example 7
Source File: AvroCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
private void checkUnion(String context, TypeDescriptor<?> type, Schema schema) {
  final List<Schema> unionTypes = schema.getTypes();

  if (!type.getRawType().isAnnotationPresent(Union.class)) {
    // First check for @Nullable field, which shows up as a union of field type and null.
    if (unionTypes.size() == 2 && unionTypes.contains(AVRO_NULL_SCHEMA)) {
      // Find the Schema that is not NULL and recursively check that it is deterministic.
      Schema nullableFieldSchema =
          unionTypes.get(0).equals(AVRO_NULL_SCHEMA) ? unionTypes.get(1) : unionTypes.get(0);
      doCheck(context, type, nullableFieldSchema);
      return;
    }

    // Otherwise report a schema error.
    reportError(context, "Expected type %s to have @Union annotation", type);
    return;
  }

  // Errors associated with this union will use the base class as their context.
  String baseClassContext = type.getRawType().getName();

  // For a union, we need to make sure that each possible instantiation is deterministic.
  for (Schema concrete : unionTypes) {
    @SuppressWarnings("unchecked")
    TypeDescriptor<?> unionType = TypeDescriptor.of(ReflectData.get().getClass(concrete));

    recurse(baseClassContext, unionType, concrete);
  }
}
 
Example 8
Source File: CoderUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * If {@code coderType} is a subclass of {@code Coder<T>} for a specific type {@code T}, returns
 * {@code T.class}.
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public static TypeDescriptor getCodedType(TypeDescriptor coderDescriptor) {
  ParameterizedType coderType =
      (ParameterizedType) coderDescriptor.getSupertype(Coder.class).getType();
  return TypeDescriptor.of(coderType.getActualTypeArguments()[0]);
}
 
Example 9
Source File: ParDo.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Try to provide coders for as many of the type arguments of given {@link
 * DoFnSignature.StateDeclaration} as possible.
 */
private static <InputT> Coder[] codersForStateSpecTypes(
    DoFnSignature.StateDeclaration stateDeclaration,
    CoderRegistry coderRegistry,
    Coder<InputT> inputCoder) {
  Type stateType = stateDeclaration.stateType().getType();

  if (!(stateType instanceof ParameterizedType)) {
    // No type arguments means no coders to infer.
    return new Coder[0];
  }

  Type[] typeArguments = ((ParameterizedType) stateType).getActualTypeArguments();
  Coder[] coders = new Coder[typeArguments.length];

  for (int i = 0; i < typeArguments.length; i++) {
    Type typeArgument = typeArguments[i];
    TypeDescriptor<?> typeDescriptor = TypeDescriptor.of(typeArgument);
    try {
      coders[i] = coderRegistry.getCoder(typeDescriptor);
    } catch (CannotProvideCoderException e) {
      try {
        coders[i] =
            coderRegistry.getCoder(
                typeDescriptor, inputCoder.getEncodedTypeDescriptor(), inputCoder);
      } catch (CannotProvideCoderException ignored) {
        // Since not all type arguments will have a registered coder we ignore this exception.
      }
    }
  }

  return coders;
}
 
Example 10
Source File: ReflectUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
public static TypeDescriptor getMapType(TypeDescriptor valueType, int index) {
  TypeDescriptor mapType = null;
  if (valueType.isSubtypeOf(TypeDescriptor.of(Map.class))) {
    TypeDescriptor<Collection<?>> map = valueType.getSupertype(Map.class);
    if (map.getType() instanceof ParameterizedType) {
      ParameterizedType ptype = (ParameterizedType) map.getType();
      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      mapType = TypeDescriptor.of(params[index]);
    } else {
      throw new RuntimeException("Map type is not parameterized! " + map);
    }
  }
  return mapType;
}
 
Example 11
Source File: FieldValueTypeInformation.java    From beam with Apache License 2.0 5 votes vote down vote up
public static FieldValueTypeInformation forOneOf(
    String name, boolean nullable, Map<String, FieldValueTypeInformation> oneOfTypes) {
  final TypeDescriptor<OneOfType.Value> typeDescriptor = TypeDescriptor.of(OneOfType.Value.class);
  return new AutoValue_FieldValueTypeInformation.Builder()
      .setName(name)
      .setNullable(nullable)
      .setType(typeDescriptor)
      .setRawType(typeDescriptor.getRawType())
      .setField(null)
      .setElementType(null)
      .setMapKeyType(null)
      .setMapValueType(null)
      .setOneOfTypes(oneOfTypes)
      .build();
}
 
Example 12
Source File: CoderRegistryTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testSerializableTypeVariableDefaultCoder() throws Exception {
  CoderRegistry registry = CoderRegistry.createDefault();

  TypeDescriptor type =
      TypeDescriptor.of(TestSerializableGenericClass.class.getTypeParameters()[0]);
  assertEquals(SerializableCoder.of(type), registry.getCoder(type));
}
 
Example 13
Source File: JAXBCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public TypeDescriptor<T> getEncodedTypeDescriptor() {
  return TypeDescriptor.of(jaxbClass);
}
 
Example 14
Source File: Sessions.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public TypeDescriptor<IntervalWindow> getWindowTypeDescriptor() {
  return TypeDescriptor.of(IntervalWindow.class);
}
 
Example 15
Source File: DefaultCoderTest.java    From beam with Apache License 2.0 4 votes vote down vote up
protected OldCustomSerializableCoder() {
  super(OldCustomRecord.class, TypeDescriptor.of(OldCustomRecord.class));
}
 
Example 16
Source File: JavaBinCodecCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public TypeDescriptor<T> getEncodedTypeDescriptor() {
  return TypeDescriptor.of(clazz);
}
 
Example 17
Source File: ConvertHelpers.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a function to convert a Row into a primitive type. This only works when the row schema
 * contains a single field, and that field is convertible to the primitive type.
 */
@SuppressWarnings("unchecked")
public static <OutputT> SerializableFunction<?, OutputT> getConvertPrimitive(
    FieldType fieldType,
    TypeDescriptor<?> outputTypeDescriptor,
    TypeConversionsFactory typeConversionsFactory) {
  FieldType expectedFieldType =
      StaticSchemaInference.fieldFromType(outputTypeDescriptor, JavaFieldTypeSupplier.INSTANCE);
  if (!expectedFieldType.equals(fieldType)) {
    throw new IllegalArgumentException(
        "Element argument type "
            + outputTypeDescriptor
            + " does not work with expected schema field type "
            + fieldType);
  }

  Type expectedInputType =
      typeConversionsFactory.createTypeConversion(false).convert(outputTypeDescriptor);

  TypeDescriptor<?> outputType = outputTypeDescriptor;
  if (outputType.getRawType().isPrimitive()) {
    // A SerializableFunction can only return an Object type, so if the DoFn parameter is a
    // primitive type, then box it for the return. The return type will be unboxed before being
    // forwarded to the DoFn parameter.
    outputType = TypeDescriptor.of(Primitives.wrap(outputType.getRawType()));
  }

  TypeDescription.Generic genericType =
      TypeDescription.Generic.Builder.parameterizedType(
              SerializableFunction.class, expectedInputType, outputType.getType())
          .build();
  DynamicType.Builder<SerializableFunction> builder =
      (DynamicType.Builder<SerializableFunction>) new ByteBuddy().subclass(genericType);

  try {
    return builder
        .visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES))
        .method(ElementMatchers.named("apply"))
        .intercept(new ConvertPrimitiveInstruction(outputType, typeConversionsFactory))
        .make()
        .load(ReflectHelpers.findClassLoader(), ClassLoadingStrategy.Default.INJECTION)
        .getLoaded()
        .getDeclaredConstructor()
        .newInstance();
  } catch (InstantiationException
      | IllegalAccessException
      | NoSuchMethodException
      | InvocationTargetException e) {
    throw new RuntimeException(e);
  }
}
 
Example 18
Source File: BigQueryMerger.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Override
public TypeDescriptor getInputTypeDescriptor() {
  return TypeDescriptor.of(MergeInfo.class);
}
 
Example 19
Source File: BigQueryMerger.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Override
public TypeDescriptor getInputTypeDescriptor() {
  return TypeDescriptor.of(MergeInfo.class);
}
 
Example 20
Source File: SerializableCoder.java    From beam with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link SerializableCoder} instance for the provided element class.
 *
 * @param <T> the element type
 */
public static <T extends Serializable> SerializableCoder<T> of(Class<T> clazz) {
  checkEqualsMethodDefined(clazz);
  return new SerializableCoder<>(clazz, TypeDescriptor.of(clazz));
}