简体   繁体   中英

Creating Custom Processor In Apache Nifi

I am building an custom processor to process flow file , to process the flow file i need to read an CSV file from my local file system. I created an proerty descriptor CSV_PATH as follows

public static final PropertyDescriptor CSV_PATH = new 
PropertyDescriptor
.Builder().name("CSV Path")
.displayName("CSV Path")
.description("CSV Path Reader")
.required(true)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();



@Override

protected void init(final ProcessorInitializationContext context) {

final List<PropertyDescriptor> descriptors = new 
ArrayList<PropertyDescriptor>();

descriptors.add(JSON_PATH);

descriptors.add(CSV_PATH);

this.descriptors = Collections.unmodifiableList(descriptors);



final Set<Relationship> relationships = new HashSet<Relationship>();

relationships.add(SUCCESS);

this.relationships = Collections.unmodifiableSet(relationships);

}

Now I wants to get the value of CSV_PATH property set in UI while configuring processor. I am not able to get the CSV_PATH value. Also If I hardcode filepath in code then still I am not able to read CSV from local file system.

You want to use the following code to retrieve the value of the PropertyDescriptor from the ProcessContext :

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final String csvPath = context.getProperty(CSV_PATH).getValue();

    // Do something with csvPath

}

If you decide to support NiFi Expression Language in that property descriptor, you will also want to evaluate for that:

final String csvPath = context.getProperty(CSV_PATH).evaluateAttributeExpressions().getValue();

There are additional method overrides for that, including flowfile attributes, variable registry, custom decorators, etc.

This is documented in the Apache NiFi Developer's Guide . I recently did a presentation at Dataworks Summit Barcelona 2019 covering custom processor development with some best practices included and examples that may be helpful. You can also look at any existing processor in the NiFi codebase to see examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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