Apache Flink如何从Java ObjectNode中下沉 - > JSON字符串?

时间:2018-05-16 18:27:16

标签: apache-flink flink-streaming flink-cep

所以这需要JSON字符串 - > Java ObjectNode。

    final DataStream<ObjectNode> inputStream = env
        .addSource(new RMQSource<ObjectNode>(
            connectionConfig,                   // config for the RabbitMQ connection
            "start",                            // name of the RabbitMQ queue to consume
            true,                               // use correlation ids; can be false if only at-least-once is required
            new JSONDeserializationSchema()))   // deserialization schema to turn messages into Java objects
        .setParallelism(1);                     // non-parallel source is only required for exactly-once

如何将它们从Java ObjectNode中恢复 - &gt; JSON字符串?

stream.addSink(new RMQSink<ObjectNode>(
            connectionConfig,
            "stop",
            new JSONSerializationSchema()
        ));

JSONSerializationSchema不存在,但我需要这样的东西。

1 个答案:

答案 0 :(得分:0)

使用这样的自定义SerializationSchema

stream.addSink(new RMQSink<ObjectNode>(
            connectionConfig,
            "stop",
            new SerializationSchema<ObjectNode>() {
                    @Override
                    public byte[] serialize( ObjectNode element ) {
                        return element.toString().getBytes();
                    }
            }
        ));