Apache nifi 如何使用NIFI中的自定义处理器逐个传输流文件
我正在用Nifi v 1.3编写一个自定义处理器 处理器执行从resultset读取的SQL查询,并将每一行转换为json文档,并将其存储到ArrayList中,最后它将每1000个文档(fetchSize param)传输到一个flowfile,这对我来说是可行的,但它一次发送所有flowfile 我想让它在调用transferFlowFile方法时独立地传输每个流文件,而不必等待onTrigger方法的末尾一次传输所有内容 代码如下:Apache nifi 如何使用NIFI中的自定义处理器逐个传输流文件,apache-nifi,Apache Nifi,我正在用Nifi v 1.3编写一个自定义处理器 处理器执行从resultset读取的SQL查询,并将每一行转换为json文档,并将其存储到ArrayList中,最后它将每1000个文档(fetchSize param)传输到一个flowfile,这对我来说是可行的,但它一次发送所有flowfile 我想让它在调用transferFlowFile方法时独立地传输每个流文件,而不必等待onTrigger方法的末尾一次传输所有内容 代码如下: public void onTrigger(final
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile fileToProcess = null;
if (context.hasIncomingConnection()) {
fileToProcess = session.get();
if (fileToProcess == null && context.hasNonLoopConnection()) {
return;
}
}
final ResultSet resultSet = st.executeQuery();
final ResultSetMetaData meta = resultSet.getMetaData();
final int nrOfColumns = meta.getColumnCount();
List<Map<String, Object>> documentList = new ArrayList<>();
while (resultSet.next()) {
final AtomicLong nrOfRows = new AtomicLong(0L);
cpt++;
Map<String, Object> item = new HashMap<>();
for (int i = 1; i <= nrOfColumns; i++) {
int javaSqlType = meta.getColumnType(i);
String nameOrLabel = StringUtils.isNotEmpty(meta.getColumnLabel(i)) ? meta.getColumnLabel(i)
: meta.getColumnName(i);
Object value = null;
value = resultSet.getObject(i);
if (value != null) {
item.put(nameOrLabel, value.toString());
}
}
Document document = new Document(item);
documentList.add(document);
if (fetchSize!=0 && cpt % fetchSize == 0) {
FlowFile flowFile = session.create();
transferFlowFile(flowFile, session, documentList, fileToProcess, nrOfRows, stopWatch);
}
}
if (!documentList.isEmpty()) {
final AtomicLong nrOfRows = new AtomicLong(0L);
FlowFile flowFile = session.create();
transferFlowFile(flowFile, session, documentList, fileToProcess, nrOfRows, stopWatch);
}
}
public void transferFlowFile(FlowFile flowFile, ProcessSession session, List<Map<String, Object>> documentList,
FlowFile fileToProcess, AtomicLong nrOfRows, StopWatch stopWatch) {
flowFile = session.write(flowFile, out -> {
ObjectMapper mapper = new ObjectMapper();
IOUtils.write(mapper.writeValueAsBytes(documentList), out);
});
documentList.clear();
flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json");
session.getProvenanceReporter().modifyContent(flowFile, "Retrieved " + nrOfRows.get() + " rows",
stopWatch.getElapsed(TimeUnit.MILLISECONDS));
session.transfer(flowFile, REL_SUCCESS);
}
public void onTrigger(最终ProcessContext上下文,最终ProcessSession会话)引发ProcessException{
FlowFile fileToProcess=null;
if(context.hasinomingconnection()){
fileToProcess=session.get();
if(fileToProcess==null&&context.hasNonLoopConnection()){
返回;
}
}
最终结果集ResultSet=st.executeQuery();
final ResultSetMetaData meta=resultSet.getMetaData();
final int nrOfColumns=meta.getColumnCount();
List documentList=new ArrayList();
while(resultSet.next()){
最终原子长度nrOfRows=新原子长度(0L);
cpt++;
Map item=newhashmap();
对于(int i=1;i{
ObjectMapper mapper=新的ObjectMapper();
IOUtils.write(mapper.writeValueAsBytes(documentList),out);
});
documentList.clear();
flowFile=session.puttribute(flowFile,CoreAttributes.MIME_TYPE.key(),“application/json”);
session.getProvenanceReporter().modifyContent(流文件,“检索”+nrows.get()+“行”,
stopWatch.getappeased(TimeUnit.ms));
会话传输(流文件,REL_SUCCESS);
}
调用session.commit()
之后
session.transfer(流文件,REL\u成功)
提交会话时,将传输自上次提交以来创建的任何流文件,如果从未提交,则自开始创建的任何流文件