Search in sources :

Example 16 with TypeDescriptor

use of org.apache.beam.sdk.values.TypeDescriptor in project beam by apache.

the class FieldValueTypeInformation method forGetter.

public static FieldValueTypeInformation forGetter(Method method, int index) {
    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 = hasNullableReturnType(method);
    return new AutoValue_FieldValueTypeInformation.Builder().setName(getNameOverride(name, method)).setNumber(getNumberOverride(index, method)).setNullable(nullable).setType(type).setRawType(type.getRawType()).setMethod(method).setElementType(getIterableComponentType(type)).setMapKeyType(getMapKeyType(type)).setMapValueType(getMapValueType(type)).setOneOfTypes(Collections.emptyMap()).build();
}
Also used : TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor)

Example 17 with TypeDescriptor

use of org.apache.beam.sdk.values.TypeDescriptor in project beam by apache.

the class ParDo method schemasForStateSpecTypes.

private static SchemaCoder[] schemasForStateSpecTypes(DoFnSignature.StateDeclaration stateDeclaration, SchemaRegistry schemaRegistry) throws NoSuchSchemaException {
    Type stateType = stateDeclaration.stateType().getType();
    if (!(stateType instanceof ParameterizedType)) {
        // No type arguments means no coders to infer.
        return new SchemaCoder[0];
    }
    Type[] typeArguments = ((ParameterizedType) stateType).getActualTypeArguments();
    SchemaCoder[] coders = new SchemaCoder[typeArguments.length];
    for (int i = 0; i < typeArguments.length; i++) {
        Type typeArgument = typeArguments[i];
        TypeDescriptor typeDescriptor = TypeDescriptor.of(typeArgument);
        coders[i] = SchemaCoder.of(schemaRegistry.getSchema(typeDescriptor), typeDescriptor, schemaRegistry.getToRowFunction(typeDescriptor), schemaRegistry.getFromRowFunction(typeDescriptor));
    }
    return coders;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) SchemaCoder(org.apache.beam.sdk.schemas.SchemaCoder)

Example 18 with TypeDescriptor

use of org.apache.beam.sdk.values.TypeDescriptor in project beam by apache.

the class DefaultCoderTest method testDefaultCoderInCollection.

@Test
public void testDefaultCoderInCollection() throws Exception {
    CoderRegistry registry = CoderRegistry.createDefault();
    registry.registerCoderProvider(new DefaultCoderProvider());
    Coder<List<AvroRecord>> avroRecordCoder = registry.getCoder(new TypeDescriptor<List<AvroRecord>>() {
    });
    assertThat(avroRecordCoder, instanceOf(ListCoder.class));
    assertThat(((ListCoder) avroRecordCoder).getElemCoder(), instanceOf(AvroCoder.class));
    assertThat(registry.getCoder(new TypeDescriptor<List<SerializableRecord>>() {
    }), Matchers.equalTo(ListCoder.of(SerializableCoder.of(SerializableRecord.class))));
}
Also used : DefaultCoderProvider(org.apache.beam.sdk.coders.DefaultCoder.DefaultCoderProviderRegistrar.DefaultCoderProvider) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) List(java.util.List) Test(org.junit.Test)

Example 19 with TypeDescriptor

use of org.apache.beam.sdk.values.TypeDescriptor in project beam by apache.

the class DoFnSignaturesSplittableDoFnTest method testSplittableProcessElementMustNotHaveUnsupportedParams.

@Test
// used via reflection
@SuppressWarnings("unused")
public void testSplittableProcessElementMustNotHaveUnsupportedParams() throws Exception {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Illegal parameter");
    thrown.expectMessage("ValueState");
    DoFn<Integer, String> doFn = new DoFn<Integer, String>() {

        @StateId("my-state-id")
        public final StateSpec<ValueState<String>> myStateSpec = StateSpecs.value(StringUtf8Coder.of());

        @ProcessElement
        public void method(DoFn<Integer, String>.ProcessContext context, SomeRestrictionTracker tracker, @StateId("my-state-id") ValueState<String> myState) {
        }
    };
    Method processElementMethod = null;
    for (Method method : doFn.getClass().getDeclaredMethods()) {
        if ("method".equals(method.getName())) {
            processElementMethod = method;
        }
    }
    checkState(processElementMethod != null);
    FnAnalysisContext context = FnAnalysisContext.create();
    context.addStateDeclaration(DoFnSignature.StateDeclaration.create("my-state-id", doFn.getClass().getField("myStateSpec"), new TypeDescriptor<ValueState<String>>() {
    }));
    DoFnSignatures.analyzeProcessElementMethod(errors(), new TypeDescriptor<DoFn<Integer, String>>() {
    }, processElementMethod, TypeDescriptor.of(Integer.class), TypeDescriptor.of(String.class), context);
}
Also used : DoFnSignaturesTestUtils.analyzeProcessElementMethod(org.apache.beam.sdk.transforms.reflect.DoFnSignaturesTestUtils.analyzeProcessElementMethod) AnonymousMethod(org.apache.beam.sdk.transforms.reflect.DoFnSignaturesTestUtils.AnonymousMethod) Method(java.lang.reflect.Method) StateSpec(org.apache.beam.sdk.state.StateSpec) DoFn(org.apache.beam.sdk.transforms.DoFn) FakeDoFn(org.apache.beam.sdk.transforms.reflect.DoFnSignaturesTestUtils.FakeDoFn) ValueState(org.apache.beam.sdk.state.ValueState) FnAnalysisContext(org.apache.beam.sdk.transforms.reflect.DoFnSignatures.FnAnalysisContext) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) Test(org.junit.Test)

Example 20 with TypeDescriptor

use of org.apache.beam.sdk.values.TypeDescriptor in project beam by apache.

the class TypedCombineFnDelegate method getGenericSuperTypeAtIndex.

@SuppressWarnings("unchecked")
@Nullable
private <T> TypeDescriptor<T> getGenericSuperTypeAtIndex(int index) {
    Class<?> cls = Preconditions.checkArgumentNotNull(getClass());
    do {
        Class<?> superClass = cls.getSuperclass();
        if (superClass == null) {
            break;
        }
        if (superClass.equals(TypedCombineFnDelegate.class)) {
            @Nonnull ParameterizedType superType = (ParameterizedType) Preconditions.checkArgumentNotNull(cls.getGenericSuperclass());
            TypeDescriptor<T> candidate = (TypeDescriptor<T>) TypeDescriptor.of(superType.getActualTypeArguments()[index]);
            if (!(candidate instanceof TypeVariable)) {
                return candidate;
            }
        }
        cls = superClass;
    } while (true);
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) Nonnull(javax.annotation.Nonnull) TypeVariable(java.lang.reflect.TypeVariable) Nullable(javax.annotation.Nullable)

Aggregations

TypeDescriptor (org.apache.beam.sdk.values.TypeDescriptor)20 ParameterizedType (java.lang.reflect.ParameterizedType)8 Type (java.lang.reflect.Type)6 DoFn (org.apache.beam.sdk.transforms.DoFn)6 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)5 LinkedHashMap (java.util.LinkedHashMap)4 Timer (org.apache.beam.sdk.state.Timer)4 KV (org.apache.beam.sdk.values.KV)4 Test (org.junit.Test)4 FormatString (com.google.errorprone.annotations.FormatString)3 List (java.util.List)3 Map (java.util.Map)3 BagState (org.apache.beam.sdk.state.BagState)3 MapState (org.apache.beam.sdk.state.MapState)3 OrderedListState (org.apache.beam.sdk.state.OrderedListState)3 ReadableState (org.apache.beam.sdk.state.ReadableState)3 SetState (org.apache.beam.sdk.state.SetState)3 State (org.apache.beam.sdk.state.State)3 ValueState (org.apache.beam.sdk.state.ValueState)3 WatermarkHoldState (org.apache.beam.sdk.state.WatermarkHoldState)3