[英]Get ProcessGroup from processor
是否有 API 可以通過 id 從自定義處理器或 ExecuteScript 處理器獲取 ProcessGroup?
我知道使用 REST-API 是可能的,但出於安全原因,我沒有機會使用憑據從服務調用 API。
問候
如果您使用 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.