0

我正在尝试在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();

我已经提供了架构SQLTransformsetSchema但我收到了一个错误,即

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));
    }

}
4

3 回答 3

1

我认为你的 setSchema 只是设置输出 PCollection 的模式SQLTransform。您还应该在PCollection<Object> objs.

于 2019-07-08T17:58:35.060 回答
0

上面的答案是正确的,PCollection<Object>还应该调用setSchema来定义输入数据模式和行对象转换函数。如果您有多个 PCollection 来构建 PCollectionTuple,则 PCollection 应分别调用 setSchema。PCollectionTuple 不需要调用 setSchema,因为可以从 SQL 命令推断输出模式。

于 2019-09-24T09:43:56.680 回答
-1

使用 setRowSchema 如下

PCollection<Row> testApps = PBegin.in(p).apply(Create.of(row1,row2,row3).withCoder(RowCoder.of(appSchema)))
                .setRowSchema(appSchema);
于 2020-04-19T11:33:33.403 回答