赞
踩
问题如下:
The implementation of the provided ElasticsearchSinkFunction is not serializable. The object probably contains or references non-serializable fields
解决方法如下:
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)); } }
按照说法就是这个类传入了外部参数,导致无法序列化。所以就需要自己自定义一个类去实现这个类,然后用构造函数去接收参数,这样在elasticsearchSink就可以直接使用:
ElasticsearchSink.Builder<Row> elasticSinkBuilder = new ElasticsearchSink.Builder<>(
httpHosts,new MyElasticSearchSinkFunction(xxxx.getIndex(),
xxxx.getType(),xxxx.getColumns)
);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。