我正在尝试在PCollection<Object>
. 在这里,CustomSource 转换在运行时生成一个 Pojo。因此,在编译时不知道运行 SQLTransform 的对象的类型。
Pipeline p = Pipeline.create(options);
PCollection<Object> objs = p.apply(new CustomSource());
Schema type = Schema.builder().addInt32Field("c1").addStringField("c2").addDoubleField("c3").build();
PCollectionTuple.of(new TupleTag<>("somedata"), objs).apply(SqlTransform.query("SELECT c1 FROM somedata"))
.setSchema(type, SerializableFunctions.identity(), SerializableFunctions.identity());
p.run().waitUntilFinish();
我已经提供了架构SQLTransform
,setSchema
但我收到了一个错误,即
java.lang.IllegalStateException: Cannot call getSchema when there is no schema
at org.apache.beam.sdk.values.PCollection.getSchema(PCollection.java:328)
PCollection.java:328
at org.apache.beam.sdk.extensions.sql.impl.schema.BeamPCollectionTable.<init>(BeamPCollectionTable.java:34)
是否可以在运行时生成 Pojo 对象并通过向转换提供模式信息在它们上运行 sqltransforms ?
这是 CustomSource 类供参考:
import java.util.HashMap;
import java.util.Map;
import com.beaconinside.messages.PojoGenerator;
import org.apache.beam.sdk.coders.StringUtf8Coder;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.values.PBegin;
import org.apache.beam.sdk.values.PCollection;
import javassist.CannotCompileException;
import javassist.NotFoundException;
public class CustomSource extends PTransform<PBegin, PCollection<Object>> {
Map<String, Class<?>> props;
Class<?> clazz;
String data = "{\"c1\": 1, \"c2\": \"row\", \"c3\": 2.0}";
public CustomSource() throws NotFoundException, CannotCompileException {
props = new HashMap<String, Class<?>>();
props.put("c1", Integer.class);
props.put("c2", String.class);
props.put("c3", Double.class);
clazz = PojoGenerator.generate("net.javaforge.blog.javassist.PojoGenerated", props);
}
@Override
public PCollection<Object> expand(PBegin input) {
return input.apply(Create.of(data)).setCoder(StringUtf8Coder.of()).apply(new SensorSource(clazz, props));
// return input.apply(Create.of(data));
}
}