从处理器获取ProcessGroup

时间:2019-03-21 20:46:17

标签: apache-nifi

是否有一个API可以通过ID从自定义处理器或ExecuteScript处理器获取ProcessGroup?

我知道可以通过使用REST-API来实现,但是出于安全原因,我没有机会使用凭据从服务中调用API。

致谢

1 个答案:

答案 0 :(得分:1)

如果您通过Groovy使用InvokeScriptedProcessor,则可以使用

context.procNode.processGroup

来自ProcessContext 如果要以面包屑方式提取所有父母,则可以使用以下方法:

import groovy.json.*;
import org.apache.nifi.groups.*;

class GroovyProcessor implements Processor {

    def REL_SUCCESS = new Relationship.Builder()
        .name("success")
        .description('FlowFiles that were successfully processed are routed here').build()

    def ComponentLog log

    @Override
    void initialize(ProcessorInitializationContext context) { 
        log = context.logger 
    }

    @Override
    Set<Relationship> getRelationships() { 
        return [REL_SUCCESS] as Set 
    }

    void executeScript(ProcessSession session, ProcessContext context) {
        def flowFile = session.get()
        if (!flowFile) { return }

        def breadcrumb = getBreadCrumb(context.procNode.processGroup) + '->' + context.getName()
        flowFile = session.putAttribute(flowFile, 'breadcrumb', breadcrumb)

        // transfer
        session.transfer(flowFile, this.REL_SUCCESS) 
    }

    // Recursive funtion that gets the breadcrumb
    String getBreadCrumb(processGroup) {        
        def breadCrumb = ''
        if(processGroup.parent != null)
            breadCrumb = getBreadCrumb(processGroup.parent) + '->'

        return breadCrumb + processGroup.name
    }

    @Override
    void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {  
        def session = sessionFactory.createSession()      
        try {
            executeScript( session, context)
            session.commit()
        } 
        catch (final Throwable t) {
            log.error('{} failed to process due to {}; rolling back session', [this, t] as Object[])
            session.rollback(true)
            throw t
        }
    }

    @Override
    PropertyDescriptor getPropertyDescriptor(String name) { null }

    @Override
    List<PropertyDescriptor> getPropertyDescriptors() { 
        return [] as List
     }


    @Override
    void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) { } 

    @Override
    Collection<ValidationResult> validate(ValidationContext context) { null }   

    @Override
    String getIdentifier() { null }    
}    

processor = new GroovyProcessor()