简体   繁体   English

从处理器获取 ProcessGroup

[英]Get ProcessGroup from processor

Is there an API to get a ProcessGroup by id from a custom processor or an ExecuteScript processor?是否有 API 可以通过 id 从自定义处理器或 ExecuteScript 处理器获取 ProcessGroup?

I know that it is possible by using the REST-API, but for security reasons, I don't have the chance to use the credentials to invoke the API from a service.我知道使用 REST-API 是可能的,但出于安全原因,我没有机会使用凭据从服务调用 API。

Regards问候

If you use a InvokeScriptedProcessor using groovy you can use如果您使用 groovy 使用 InvokeScriptedProcessor,则可以使用

context.procNode.processGroup

from the ProcessContext If you want to extract all the parents in a breadcrum way, you can use this:从 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM