当前位置:   article > 正文

flink输出到elasticsearch,ElasticsearchSinkFunction无法序列化问题_the implementation of the provided elasticsearchsi

the implementation of the provided elasticsearchsinkfunction is not serializ

问题如下:

The implementation of the provided ElasticsearchSinkFunction is not serializable. The object probably contains or references non-serializable fields
  • 1

解决方法如下:

public class MyElasticSearchSinkFunction implements ElasticsearchSinkFunction<Row>, Serializable {
    private String index;
    private String type;
    private String[] columns;

    MyElasticSearchSinkFunction(String index, String type, String[] columns) {
        this.index = index;
        this.type = type;
        this.columns = columns;
    }
    public IndexRequest createIndexRequest(Row element) {
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < element.getArity(); i++) {
            jsonObject.put(columns[i], element.getField(i).toString());
        }
        System.out.println(jsonObject.toJSONString());
        return Requests.indexRequest()
                .index(index)
                .type(type)
                .source(jsonObject, XContentType.JSON);
    }

    @Override
    public void process(Row row, RuntimeContext runtimeContext, RequestIndexer requestIndexer) {
        System.out.println(row.toString());
        requestIndexer.add(createIndexRequest(row));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

按照说法就是这个类传入了外部参数,导致无法序列化。所以就需要自己自定义一个类去实现这个类,然后用构造函数去接收参数,这样在elasticsearchSink就可以直接使用:

                ElasticsearchSink.Builder<Row> elasticSinkBuilder = new ElasticsearchSink.Builder<>(
                        httpHosts,new MyElasticSearchSinkFunction(xxxx.getIndex(),
                        xxxx.getType(),xxxx.getColumns)
                );
  • 1
  • 2
  • 3
  • 4
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号