增加查询条件计算脚本日志,发布业务插件到apache-nifi在237环境测试

This commit is contained in:
liuy 2025-09-10 10:26:44 +08:00
parent 1dfbe07f4c
commit e318905355
4 changed files with 130 additions and 6 deletions

View File

@ -0,0 +1,76 @@
var relationships = context.getAvailableRelationships();
var REL_SUCCESS = null;
var REL_FAILURE = null;
relationships.forEach(function(rel) {
if (rel.getName() === 'success') {
REL_SUCCESS = rel;
} else if (rel.getName() === 'failure') {
REL_FAILURE = rel;
}
});
/**
* 辅助函数获取并格式化当前日期为 'YYYY-MM-DD' 格式
*/
function getCurrentDateFormatted() {
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
if (month < 10) {
month = '0' + month;
}
var day = today.getDate();
if (day < 10) {
day = '0' + day;
}
return year + '-' + month + '-' + day;
}
var flowFile = session.get();
if (flowFile === null) {
flowFile = session.create();
}
try {
if (REL_SUCCESS === null) {
throw new Error("在处理器的可用关系中找不到 'success' 关系。");
}
var jsonData = {
"erpSysFlag": "0000",
"bizContent": {
"bankAccNo": "201000270605501",
"queryBeginDate": "",
"queryEndDate": "",
"current": 1,
"size": 100
}
};
var currentDate = getCurrentDateFormatted();
//测试
jsonData.bizContent.queryBeginDate = "2021-01-01";
jsonData.bizContent.queryEndDate = "2021-01-31";
var jsonString = JSON.stringify(jsonData);
// 关键修正为了解决Nashorn引擎对 session.write 的方法签名歧义问题,
// 我们必须显式地创建一个 org.apache.nifi.processor.io.OutputStreamCallback 的实例。
flowFile = session.write(flowFile, new (Java.type('org.apache.nifi.processor.io.OutputStreamCallback'))({
process: function(outputStream) {
outputStream.write(jsonString.getBytes("UTF-8"));
}
}));
session.transfer(flowFile, REL_SUCCESS);
} catch (e) {
log.error('处理 JSON 并创建 FlowFile 时出错。', e);
if (REL_FAILURE !== null) {
session.transfer(flowFile, REL_FAILURE);
} else {
session.remove(flowFile);
}
}

View File

@ -21,7 +21,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.hzya</groupId> <groupId>com.hzya</groupId>
<artifactId>hzya-nifi-Zjnx-czb-processors</artifactId> <artifactId>hzya-nifi-Zsyh-cbs-processors</artifactId>
<version>1.0</version> <version>1.0</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -95,11 +95,11 @@
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
<version>1.7.36</version> <version>1.7.36</version>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>commons-logging</groupId> <!-- <groupId>commons-logging</groupId>-->
<artifactId>commons-logging</artifactId> <!-- <artifactId>commons-logging</artifactId>-->
<version>1.2</version> <!-- <version>1.2</version>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>commons-codec</groupId> <groupId>commons-codec</groupId>

View File

@ -0,0 +1,48 @@
//2025-09-10 10:19:05 动态计算查询结果脚本
// 从上下文中获取所有可用的关系
var relationships = context.getAvailableRelationships();
var REL_SUCCESS = null;
var REL_FAILURE = null;
relationships.forEach(function (rel) {
if (rel.getName() === 'success') {
REL_SUCCESS = rel;
} else if (rel.getName() === 'failure') {
REL_FAILURE = rel;
}
});
var flowFile = session.get();
if (flowFile === null) {
flowFile = session.create();
}
try {
if (REL_SUCCESS === null) {
throw new Error("在处理器的可用关系中找不到 'success' 关系。");
}
var currentDate = java.time.LocalDate.now().toString();
var jsonData = {
"applyDateStart": currentDate, "applyDateEnd": currentDate, "initAccountList": "591915131310106"
};
var jsonString = JSON.stringify(jsonData);
flowFile = session.write(flowFile, new (Java.type('org.apache.nifi.processor.io.OutputStreamCallback'))({
process: function (outputStream) {
outputStream.write(jsonString.getBytes("UTF-8"));
}
}));
session.transfer(flowFile, REL_SUCCESS);
} catch (e) {
log.error('处理 JSON 并创建 FlowFile 时出错。', e);
if (REL_FAILURE !== null) {
session.transfer(flowFile, REL_FAILURE);
} else {
session.remove(flowFile);
}
}