在这个代码中我 想在数组中保存lastDate并使用它来自动增加startDate和endDate属性值。但它不会生成流文件。 我尝试修复它,但它无法创建我应该更改的内容?
var OutputStreamCallback = Java.type("org.apache.nifi.processor.io.OutputStreamCallback");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
Date.prototype.isValid = function () {
return (Object.prototype.toString.call(this) === "[object Date]")
&& !isNaN(this.getTime());
};
var toDate = endDate.getValue(),
parameter1=parameter.getValue(),
count1=count.getValue();
function addDays(date, days) {
var result =new Date(date);
result.setDate(result.getDate() + days);
return formatDate(result);
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
var flowFile = session.create();
if(flowFile==null) {
var param = 8;
//count = Number(count1);
//var item = item + count1;
var endDate1 = addDays(toDate, param);
var startDate = toDate;
var arr = [];
arr.push(endDate1);
}
if(arr.length>1){
startDate1=arr.pop();
var endDate1 = addDays(startDate1, param);
var startDate = startDate1;
flowFile = session.putAttribute(flowFile, 'startDate', startDate1);
flowFile = session.putAttribute(flowFile, 'endDate', endDate1);
flowFile = session.putAttribute(flowFile, 'parameter', parameter);
}
else {
var param = 8;
var endDate1 = addDays(toDate, param);
var startDate = toDate;
flowFile = session.putAttribute(flowFile, 'count', 1);
flowFile = session.putAttribute(flowFile, 'startDate', startDate);
flowFile = session.putAttribute(flowFile, 'endDate', endDate1);
flowFile = session.putAttribute(flowFile, 'parameter', parameter1);
}
session.transfer(flowFile, REL_SUCCESS);
**它不会引发异常,但也不会生成流程文件
答案 0 :(得分:2)
您的代码存在的问题是您正在创建一个新的流文件,保证不会是null
。然后,您评估if (flowFile == null)
,它将始终返回false
。您只需在该控制块内初始化变量param
,endDate1
,startDate
和arr
。其余代码不会按预期执行,并且您引用尚未定义的变量(例如parameter1
或var toDate = endDate.getValue()
)。 flowFile
在转移时不会拥有您期望的任何属性。
您不需要ExecuteScript
处理器来执行此操作。使用UpdateAttribute
和Apache NiFi表达式语言来执行简单的日期数学运算。
如果这启动了流,请使用GenerateFlowFile
处理器初始创建流文件并将其发送到UpdateAttribute
。如果您从其他地方收到流文件,则只需要UpdateAttribute
(但您需要两个;一个用于创建添加变量的天数,一个用于执行数学运算 - 或者如果delta是常量,只有一个,并将变量引用更改为文字数字)。
定义了动态属性的处理器(template as GitHub Gist):
GenerateFlowFile:
startDate: ${now():toNumber()} <- puts the start date in "number of milliseconds since Jan 1, 1970 00:00:00.000 GMT" format
numberOfDaysToAdd: 8 <- or whatever static or dynamic value you want here
startDateFormatted: ${now():format("YYYY-MM-dd")} <- (optional) startDate in readable format if you need it
UpdateAttribute:
endDate: ${startDate:plus(${numberOfDaysToAdd:multiply(86400000)}):format("YYYY-MM-dd")} <- adds the number of milliseconds in a day * the number of days to the start date and formats it the way you want
生成的流文件如下所示:
o.a.n.processors.standard.LogAttribute LogAttribute[id=d06d3a2d-015e-1000-0820-087660238327] logging for flow file StandardFlowFileRecord[uuid=6d26df1a-fd52-407e-b549-0599d6ab3a21,claim=,offset=0,name=1636687405556264,size=0]
--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
Value: 'Fri Sep 29 18:40:06 PDT 2017'
Key: 'lineageStartDate'
Value: 'Fri Sep 29 18:40:06 PDT 2017'
Key: 'fileSize'
Value: '0'
FlowFile Attribute Map Content
Key: 'endDate'
Value: '2017-10-07'
Key: 'filename'
Value: '1636687405556264'
Key: 'numberOfDaysToAdd'
Value: '8'
Key: 'path'
Value: './'
Key: 'startDate'
Value: '1506735606982'
Key: 'startDateFormatted'
Value: '2017-09-29'
Key: 'uuid'
Value: '6d26df1a-fd52-407e-b549-0599d6ab3a21'
--------------------------------------------------
与使用ExecuteScript
相比,这将更加高效和稳定。