简体   繁体   English

如何在 Apache Beam 中一起使用 MapElements 和 KV?

[英]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 ?那么我应该如何将KVMapElements一起使用?

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.

相关问题 如何在apache_beam中的MapElements中动态添加字段? - How can I dynamically add field in MapElements in apache_beam? 我收到错误:使用Kotlin时,Apache Beam中MapElements转换产生“重载分辨率歧义” - I get error: “Overload resolution ambiguity” from MapElements transform in Apache Beam when using Kotlin Java如何在Apache Beam的KV实例中区分两个键? - How does java differentiate two keys in KV instance in apache beam? 如何在 Apache Beam 中为我的 PCollection 使用 AutoValue 数据类型? - How do I use an AutoValue data type for my PCollection in Apache Beam? 如何为 PCollection 设置编码器<List<String> &gt; 在 Apache Beam 中? - How do I set the coder for a PCollection<List<String>> in Apache Beam? 如何在处理PCollection中的元素时将元素发布到kafka主题 <KV<String,String> &gt;在apache梁中的ParDo功能? - How to publish elements to a kafka topic while processing the elements in the PCollection<KV<String,String>> in ParDo function in apache beam? 如何在 Apache Beam / Google Dataflow 中使用 ParseJsons? - How to use ParseJsons in Apache Beam / Google Dataflow? 如何使用Apache Beam DSL API? - How to use Apache Beam DSL APIs? Apache Beam 如何对文件使用 TestStream - Apache Beam How to use TestStream with files 如何一起使用Tomcat的非阻塞连接器(NIO或APR)和Apache Httpd? - How do I use Tomcat's non-blocking connectors (NIO or APR) and Apache Httpd together?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM