[英]How do I use MapElements and KV in together in Apache Beam?
I wanted to do something like:我想做类似的事情:
PCollection<String> a = whatever;
PCollection<KV<String, User>> b = a.apply(
MapElements.into(TypeDescriptor.of(KV<String, User>.class))
.via(s -> KV.of(s, new User(s))));
Where User is a custom datatype with Arvo coder and a constructor that takes a string into account.其中 User 是带有 Arvo 编码器和考虑字符串的构造函数的自定义数据类型。
However, I get the following error:但是,我收到以下错误:
Cannot select from parameterized type无法从参数化类型中选择
I tried to change it to TypeDescriptor.of(KV.class)
instead, but then I get:我试图将其更改为TypeDescriptor.of(KV.class)
,但随后我得到:
Incompatible types;不兼容的类型; Required PCollection> but 'apply' was inferred to OutputT: no instance(s) of type variable(s) exists so that PCollection conforms to PCollection>必需的 PCollection> 但“应用”被推断为 OutputT:不存在类型变量的实例,因此 PCollection 符合 PCollection>
So how am I suppose to use KV
with MapElements
?那么我应该如何将KV
与MapElements
一起使用?
I know that what I want to do is doable using ParDo
where I could explicitly specify how to do Type Erasure by declearing new DoFn<String, KV<String, User>>
but ParDo
does not support lambda function.我知道我想要做的是使用ParDo
是可行的,我可以通过清除new DoFn<String, KV<String, User>>
来明确指定如何执行类型擦除,但ParDo
不支持 lambda 函数。 As we are using Java 8, this seems less elegant....当我们使用 Java 8 时,这似乎不太优雅......
Due to type erasure in Java during compilation, KV<String, User>.class
is transformed into KV.class
and at runtime KV.class
isn't enough information to infer a coder since the type variables have been erased.由于Java在编译期间的类型擦除, KV<String, User>.class
被转换为KV.class
并且在运行时KV.class
没有足够的信息来推断编码器,因为类型变量已被擦除。
To get around this limitation, you need to use a mechanism which preserves type information after compilation.要解决此限制,您需要使用一种在编译后保留类型信息的机制。 For example you could use:例如,您可以使用:
TypeDescriptors.kvs(TypeDescriptors.strings(), TypeDescriptor.of(User.class))
which is the same as providing your own anonymous class:这与提供您自己的匿名类相同:
new TypeDescriptor<KV<String, User>> {}
Providing anonymous classes with type variables bound is one of the ways to get around type erasure in Java currently.提供绑定类型变量的匿名类是当前在 Java 中绕过类型擦除的方法之一。
尝试使用 SimpleFunction - 它保留类型信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.