nifi更改属性的方法——UpdateAttribute

参考:

https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi.processors.attributes.UpdateAttribute/additionalDetails.html

在nifi中FlowFile的组成包括属性和内容两部分:

属性:给用户数据提供上下文和附加信息的特征信息。由键值对表示。属性的例子有uuid、filename、path等。

内容:实际的数据


在nifi中更改属性的方法有三种,分别是

1. Basic Usage:更改经过这个Processor的FlowFile的默认属性。

2. Advanced Usage:这个允许更改满足一定条件的属性。

3. Delete Attributes Expression:提供一个正则表达式,带有匹配名称的任何属性将被删除。

下面就这三种方法分别讲述


Basic Usage:

通过添加一个新的processor property和想要改变的属性的名字引用来更改,之后加入期望的属性值作为Value。这个Value可以和任何文本字符串一样简单,或者它可以是一个指明如何用公式表达这个值的nifi表达式语言的声明。

举例说明:

要改变标准的"filename" 属性以便在它的结尾加上".txt",添加一个新的property,并将其命名为"filename",它的Value值使用下面的nifi表达式语言表示:

  • Property: filename
  • Value: ${filename}.txt

前面的例子阐明了如何修改一个已经存在的属性,如果一个属性之前并未存在,这个Processor同样可以被使用,用来添加一个新的属性。比如下面的property可以被添加,用来创建一个叫做myAttribute 的新属性,它的Value值为myValue:

  • Property: myAttribute
  • Value: myValue

在这个例子中,所有经过这个Processor的FlowFile将接收一个额外的属性,属性名为myAttribute,属性值为myValue。这种类型的配置可能会用在为每一个FlowFile添加一个标记以在随后的Processor中使用。


Advanced Usage:

前面的例子阐述的是如何对通过这个Processor的每一个FlowFile进行修改。除此之外,UpdateAttribute这个Processor也被用来按照一定条件修改。

基于一定的条件修改属性通过在Processor右下角点击Advanced按钮使用 Advanced User Interface实现。

点击Advanced按钮之后出现Advanced界面,在Advanced界面条件和相关的操作作为一种规则输入。每一条规则基本上说如果条件满足则他们执行这个操作。在给的的规则中一个或者多个条件可能被使用,而且他们都必须要被满足,以便采取指定行动。

添加规则:
添加规则及其相关的条件和操作,点击位于"Rules", "Conditions"和"Actions"标签上的+按钮,接着添加为规则添加条件和操作,之后点击右下角的Save按钮保存。

规则示例:
这个示例有两个规则:CheckForLargeFiles和CheckForGiantFiles。CheckForLargeFiles有如下条件:

  • ${filename:equals('fileOfInterest')}
  • ${fileSize:toNumber():ge(1048576)}
  • ${fileSize:toNumber():lt(1073741824)}
它对应的对filename的操作属性为
  • ${filename}.meg
放在一起,这个规则就是:
如果filename属性的值是fileOfInterest,并且文件大小大于或等于(GE)1兆字节(1048576字节),文件大小小于(LT)的一个字节(1073741824字节),则filename属性的值加上".meg"

nifi更改属性的方法——UpdateAttribute_第1张图片

另一个规则CheckForGiantFiles:

检查已大于一个字节的文件,在添加这个规则的时候,我们可以使用以前的规则作为模板,所以说通过选择"Copy from existing rule"选项。只需输入一个现有的规则的名称,它将显示在一个下拉菜单下输入字段。
在这示例中CheckForGiantFiles规则含有如下条件:

  • ${filename:equals('fileOfInterest')}
  • ${fileSize:toNumber():gt(1073741824)}
它对应的filename属性操作如下:
  • ${filename}.gig
放在一起这个规则就是:
如果filename属性的值是fileOfInterest,文件大小大于(GT)的一个字节(1073741824字节),就为filename属性的值添加".gig"

nifi更改属性的方法——UpdateAttribute_第2张图片

将Basic Usage用法与Advanced Usage结合
UpdateAttribute Processor允许同时使用Basic Usage和 advanced usage,如果它们同时影响同一个属性,则优先选择advanced usage,如果没有规则匹配则Basic Usage将被使用。



Deleting Attributes

Deleting attributes is a simple as providing a regular expression for attribute names to be deleted. This can be a simple regular expression that will match a single attribute or more complex regular expression to match a group of similarly named attributes or even seveal individual attribute names.

  • lastUser - will delete an attribute with the name "lastUser".
  • user.* - will delete attributes beginning with "user", including for example "username, "userName", "userID", and "users". But it will not delete "User" or "localuser".
  • (user.*|host.*|.*Date) - will delete "user", "username", "userName", "hostInfo", "hosts", and "updateDate", but not "User", "HOST", "update", or "updatedate".

The delete attributes function does not produce a Provenance Event if the alternate.identified Attribute is deleted.

FlowFile Policy

Another setting in the Advanced UI is the FlowFile Policy. It is located in the upper-left corner of the UI, and it defines the processor's behavior when multiple rules match. It may be changed using the dropdown menu. By default, the FlowFile Policy is set to "use clone".

If the FlowFile policy is set to "use clone", and multiple rules match, then a copy of the incoming FlowFile is created, such that the number of outgoing FlowFiles is equal to the number of rules that match. In other words, if two rules (A and B) both match, then there will be two outgoing FlowFiles, one for Rule A and one for Rule B. This can be useful in situations where you want to add an attribute to use as a flag for routing later. In this example, there will be two copies of the file available, one to route for the A path, and one to route for the B path.

If the FlowFile policy is set to "use original", then all matching rules are applied to the same incoming FlowFile, and there is only one outgoing FlowFile with all the attribute changes applied. In this case, the order of the rules matters and the action for each rule that matches will be applied in that order. If multiple rules contain actions that update the same attribute, the action from the last matching rule will take precedence. Notably, you can drag and drop the rules into a certain order within the Rules list.

Filtering Rules

The Advanced UI supports the creation of an arbitrarily large number of rules. In order to manage large rule sets, the listing of rules may be filtered using the Filter mechanism in the lower left corner. Rules may be filtered by any text in the name, condition, or action.

Closing the Advanced UI

Once all changes have been saved in the Advanced UI, the UI can be closed using the X in the top right corner.

Properties:

The properties in this processor are added by the user. The expression language is supported in user-added properties for this processor. See the NiFi Expression Language Guide to learn how to formulate proper expression language statements to perform the desired functions.

If an Attribute is added with the name alternate.identifier and that attribute's value is a URI, an ADD_INFO Provenance Event will be registered, correlating the FlowFile with the given alternate identifier.

Relationships:

  • success
    • If the processor successfully updates the specified attribute(s), then the FlowFile follows this relationship.

你可能感兴趣的:(nifi更改属性的方法——UpdateAttribute)