如何传递函数名称作为Flink Map函数的输入

时间:2018-07-08 10:02:06

标签: lambda apache-flink

我有一个基于Flink Java API的类:

public class SP implements Serializable {

    private transient StreamExecutionEnvironment env;
    private DataStream<byte[]> data ;
}

然后,我尝试为类SP写一个方法,该方法获得一个函数名并将该函数应用于data字段行。

public DataStream<Object> myMap(Function<Object, Object> func) {
        return data.map(x -> func.apply(x));
    }

因此在main方法中,我创建了一个简单函数并将其传递给myMap函数。

public static void main(String[] args) throws Exception {
        SP temp = new SP();
        DataStream<Object> datastream = temp.getDataFromKakfa("7798", 1).myMap(Test::print) ;
        datastream.print() ;

        temp.execute();
    }

public static Object print(Object o) {
        try {
            StringBuilder res = new StringBuilder();
            for (byte b : serializeObject(o)) {
                res.append(String.format("%02X ", b));
                res.append(" "); // delimiter
            }
            return res.toString();
        } catch (NullPointerException e){
            return 0 ;
        } catch (IOException e) {
            return 0;
        }
    }
public static byte[] serializeObject(Object obj) throws IOException
    {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bytesOut);
        oos.writeObject(obj);
        oos.flush();
        byte[] bytes = bytesOut.toByteArray();
        bytesOut.close();
        oos.close();
        return bytes;
    }

但是我得到了错误:

Exception in thread "main" org.apache.flink.api.common.InvalidProgramException: The implementation of the MapFunction is not serializable. The object probably contains or references non serializable fields.

,它引用了myMap函数。我该如何解决该问题?是做这种情况的更直接的方法吗?

1 个答案:

答案 0 :(得分:0)

无需过多考虑,似乎您的Function<Object, Object> func需要实现Serializable

您可以在创建标记界面时完成此操作:

@FunctionalInterface
interface SerializableFuncton<I, O> extends Function<I, O>, Serializable { }

然后将DataStream<Object> myMap(Function<Object, Object> func)更改为DataStream<Object> myMap(SerializableFuncton<Object, Object> func)