Replace a value with a variable from flowfile using apache-nifi

戏子无情 提交于 2019-12-24 23:19:22

问题


I am trying to replace a value with a variable assigned in flowfile.

In my flowfile, I have assigned flowID to flow_id variable.

In UpdateRecord processor, I try to update a column named /flow which has INFLOW and OUTFLOW I have following as ${field.value:replaceAll('INFLOW',$flow_id)}

Flowfile before UpdateRecord:

id,flow,flow_id
1,INFLOW,IN
2,OUTFLOW,OUT
3,INFLOW,IN

After the conversion flowfile should be:

id,flow,flow_id
1,IN,IN
2,OUT,OUT
3,IN,IN

But it fails with an error

unexpected token

Edit : After answer and comments

Now I have following settings:

${field.value:replace('INFLOW',flow_id)}

Due to unexpected token flow_id

Same error for Literal Value and Record path value Replacement strategies.


回答1:


Set up the Nifi Workflow as shown below :

Set your input directory in the GetFile Processor. In the first UpdateRecord Processor concatenate the flow and flow_id columns and assign it into flow. Do this by changing the configurations of the First UpdateRecord processor as shown below.

The next UpdateRecord Processor replaces INFLOW and OUTFLOW accordingly. Set the configurations of the second UpdateRecord Processor as shown below.

Because of the initial concating " and , is added in the flow column. I have used the other two processors to remove them.

The third UpdateRecord Processor must have a new property named /flow and have the value ${field.value:replaceFirst(${field.value:substringAfter(',')},'')}. The rest of the configurations remain as the Second UpdateRecord Processor.

The Fourth UpdateRecord Processor must have a new property named /flow and have the value ${field.value:replaceFirst('"',''):replaceFirst(',','')}. The rest of the configurations remain as the Second UpdateRecord Processor.

Note: This answer was based on the concepts used in this question.




回答2:


This Answer is for Attribute substitution. OP actually wants to replace the column values from the other column values.


From the documentation, you can find the detailed usage of the variables and parameters.

  • For the variables, see here
  • For the parameters, see here

When you use a Variable such as

  • Processor-specific attributes
  • FlowFile properties
  • FlowFile attributes
  • From Variable Registry:
    • User defined properties (custom properties)
    • System properties
    • Operating System environment variables

You can refer the variable such as:

${name of variable}

When you use a parameter from the parameter context, you can refer the parameter such as:

#{name of parameter}

In your case, you want to use a variable and so it should be:

${field.value:replaceAll('INFLOW',${flow_id})}

I have tested with the csv data,

index,flow
1,INFLOW
2,OUTFLOW
3,INFLOW

and the attribute flow_id with the value 'flowId'. Now, I have set the UpdateRecord processor with the options as follows:

Record Reader                  CSVReader
Record Writer                  CSVRecordSetWriter
Replacement Value Strategy     Literal Value
/flow                          ${field.value:replace('INFLOW', ${flow_id})}

The output csv is as expected.

index,flow
1,flowID
2,OUTFLOW
3,flowID


来源:https://stackoverflow.com/questions/58974062/replace-a-value-with-a-variable-from-flowfile-using-apache-nifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!