最近写flink消费kafka数据的时候,一直出现消费一部分数据成功后,后面的数据就不消费了,开始一直认为是kafka数据阻塞或者是数据单条过单导致的(之前用storm消费kafka出现过单条数据10M的情况),所以一直在怀疑是不是kafka的问题,我将所有代码注释掉只留消费kafka的代码,发现数据既然正常消费了,why?
原因在我的代码中使用了异步IO (AsyncDataStream)的方法,由于我在写返回数据的时候asyncInvoke不符合要求,我就不处理了,导致该方法一直在等待,只有等待超时后才会返回数据,很快所有的异步方法都调用完了,就卡住了
public class EsAsyncIOFunction extends RichAsyncFunction<OdsInvBiaoEntity, OdsInvBiaoEntity> {
private TransportClient es6Client;
@Override
public void open(Configuration parameters) throws Exception {
System.setProperty("es.set.netty.runtime.available.processors", "false");
ES6Conf es6Conf = new ES6Conf();
es6Client = es6Conf.getES6Client();
}
@Override
public void close() throws Exception {
es6Client.close();
}
@Override
public void asyncInvoke(OdsInvBiaoEntity input, ResultFuture<OdsInvBiaoEntity> resultFuture) throws Exception {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if (input.getUrl() != null) {
boolQuery = boolQuery.must(QueryBuilders.termQuery("url", input.getUrl()));
SearchResponse searchResponse = es6Client.prepareSearch("ods_invbiaos20200324").setTypes("ods_invbiao")
.setQuery(boolQuery).addSort("up_time", SortOrder.DESC).setFrom(0).setSize(1).get();
SearchHit[] hits = searchResponse.getHits().getHits();
long up_time = 0;
for (SearchHit hit : hits) {
//20200324100002
up_time = Long.parseLong(hit.getSourceAsMap().get("up_time").toString());
}
long inputUp_time = Long.parseLong(input.getUp_time());
if (inputUp_time > up_time) {
resultFuture.complete(Collections.singletonList(input));
}
else {/这是后面加的,加上之后就正常了,之前没有这个,导致不符合要求的不返回数据了
resultFuture.complete(Collections.singletonList(new OdsInvBiaoEntity()));
}
}else {
resultFuture.complete(Collections.singletonList(new OdsInvBiaoEntity()));
}
}
/**
* {@link AsyncFunction#asyncInvoke} timeout occurred.
* By default, the result future is exceptionally completed with a timeout exception.
*
* @param input element coming from an upstream task
* @param resultFuture to be completed with the result data
*/
@Override
public void timeout(OdsInvBiaoEntity input, ResultFuture<OdsInvBiaoEntity> resultFuture) throws Exception {
resultFuture.complete(Collections.singletonList(input));
}
}
总结:固定思维!一直以为是kakfa的问题,因为以前用storm也出现这种情况,所以一直在kafka那里去排查问题。