2017-09-29 13 views
1

このコードでは、 はlastDateを配列に保存し、startDateとendDate属性値を自動的にインクリメントするために使用しますが、flowfileは生成されません。 私はそれを修正しようとしましたが、私は何を変更する必要がありますflofwileを作成することはできません?Nifi:executeScriptプロセッサのコードが正しく動作しない

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); 

**それは例外をトロウないが、** flowfileすぎ

  1. をしない私は(flowfile == null)の新規flowfile intialized cretedにするを追加する必要がありますか?
  2. 私のコードをより永続的かつ高速にするために何かをお勧めしますか?

答えて

2

コードの問題は、新しいフローファイルを作成していて、nullでないことが保証されていることです。 if (flowFile == null)を評価します。これは常にfalseを返します。変数param,endDate1startDate、およびarrは、そのコントロールブロック内でのみ初期化します。残りのコードは期待どおりに実行されず、定義されていない変数(たとえばparameter1またはvar toDate = endDate.getValue())を参照します。 flowFileには、転送時に期待する属性はありません。

これを行うには、ExecuteScriptプロセッサは必要ありません。単純な日付計算を実行するには、Apache NiFi表現言語でUpdateAttributeを使用します。

フローが開始された場合は、GenerateFlowFileプロセッサを使用して最初にフローファイルを作成し、UpdateAttributeに送信します。他の場所からフローファイルを受信して​​いる場合は、UpdateAttributeのみ必要です(ただし、の数値を作成して変数を追加するには1つ、数学を実行するには1つ、またはそのデルタが定数の場合は1つだけ変更します)変数参照はリテラル番号になります)。定義された動的特性(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 

あなたの結果flowfileは次のようになります。

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を使用するよりもはるかにパフォーマンスと安定します。

関連する問題