[英]Is it possible to pass different subclasses to the same PCollection?
Is it possible to have a PCollection in Beam that "holds" different objects of the same superclass such that 在Beam中是否有一个PCollection可以“容纳”同一超类的不同对象,从而
PCollection<T extends SuperClass> result = input.apply(ParDo.of(new DoFnThatReturnsSubClasses()));
instead of 代替
PCollection<SubClassA> A = input.apply(ParDo.of(new DoFnSubClassesA()));
PCollection<SubClassB> B = input.apply(ParDo.of(new DoFnSubClassesB()));
My superclass holds some basic fields as follows 我的超类拥有一些基本领域,如下所示
public class SuperClass {
String key;
Long timestamp;
} }
But my subclasses will contain an additional field which have the same variable name of different data types. 但是我的子类将包含一个附加字段,该字段具有不同数据类型的相同变量名。 Ie
即
public class SubClassA extends SuperClass {
List<HashMap<String, String>> value = new ArrayList<>();
}
Another subclass, 另一个子类
public class SubClassB extends SuperClass {
String value;
}
What I really want to achieve is to allow the field value
to be of different data types. 我真正想要实现的是允许字段
value
具有不同的数据类型。 The only thing I could think was dividing this up into different subclasses. 我唯一能想到的就是将其划分为不同的子类。 I realise this might be more of a Java programming question and poor understanding of the language.
我意识到这可能更多是Java编程问题和对语言的理解不足。
Yes, this is possible. 是的,这是可能的。 You can have a PCollection that contains a mix of SubclassX objects.
您可以具有包含SubclassX对象混合的PCollection。 You'll need to make sure that the Coder that you are using for the PCollection can encode and decode all the possible subclasses.
您需要确保用于PCollection的Coder可以对所有可能的子类进行编码和解码。
Your DoFns downstream will receive Superclass objects, and you'll need to downcast to the appropriate subclass. 您的下游DoFns将接收超类对象,并且您需要向下转换到适当的子类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.