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();
}
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;
}
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))));
}
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);
}
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;
}
Aggregations