在Scala中使用SinkFunction创建我自己的接收器

时间:2019-12-20 14:28:24

标签: scala apache-flink

我想使用Scala为Flink创建自己的接收器,为此,我需要扩展接口SinkFunction

但是我不能覆盖下面的invoke方法。

default void invoke(IN value, Context context) throws Exception {
    invoke(value);
}

这是我的代码:

class MySinkFunction(schema: String) extends SinkFunction[List[GenericRecord]] {
    override def invoke(elements: List[GenericRecord], context: Context) { ... }

这会出现以下错误:

Type Context takes type parameters

如果我更改代码以添加任何类型参数:

class MySinkFunction(schema: String) extends SinkFunction[List[GenericRecord]] {
    override def invoke(elements: List[GenericRecord], context: Context[Xpto]) { ... }

错误消息是不同的:

Method `invoke` overrides nothing.

我是Scala的新手,是否有一些可以解决的问题?

我在scala中看到的所有examples都使用以下不推荐使用的方法:

/**
 * @deprecated Use {@link #invoke(Object, Context)}.
 */
@Deprecated
default void invoke(IN value) throws Exception {}

StreamingFileSink.java使用以下方法实现:

...

@Public // Interface might be extended in the future with additional methods.
interface Context<T> {

    /** Returns the current processing time. */
    long currentProcessingTime();

    /** Returns the current event-time watermark. */
    long currentWatermark();

    /**
     * Returns the timestamp of the current input record or {@code null} if the element does not
     * have an assigned timestamp.
     */
    Long timestamp();
}

<T>是否放置错误,因为SinkFunction.Context的任何地方都没有使用它?

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以简单地使用:

override def invoke(elements: List[GenericRecord], context: SinkFunction.Context[_]) { ... }

它应该像魅力一样工作。