简体   繁体   English

是否可以将不同的子类传递给同一PCollection?

[英]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.

相关问题 同一属性的不同子类 - Different subclasses for same attribute 不同子类中的相同实现 - Same implementation in different subclasses JPA继承:不同子类中的相同属性 - JPA Inheritance: same property in different subclasses 用相同的附加字段包装不同的子类 - Wrap different subclasses with same additional field 接受两个具有相同@RequestMapping 的不同子类 - Accept two different subclasses with same @RequestMapping 具有不同原始数据成员但方法相同的Java子类 - Java Subclasses that have different primitive data members, but same methods Java-包含一组相同超类的不同子类的数据结构 - Java - data structure to contain a set of different subclasses of the same superclass Java Vs C#:带有方法覆盖的Java和C#子类在同一场景中输出不同的结果 - Java Vs C#: Java and C# subclasses with method overrides output different results in same scenario 是否可以在不为 Java 中的每个 pcollection 断行的情况下编写? - Is it possible to write without breaking line for each pcollection in Java? 使用每个层次结构的表时,将同一数据库列映射到Hibernate中子类的不同字段/属性 - Mapping same DB column to different fields/properties of subclasses in Hibernate when using table per hierarchy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM