Created on 07-13-2018 12:18 PM - edited 09-16-2022 06:27 AM
Hi Folks,
I have written one custom java code. i want make that as custom processor.
My code has total 4 classes.
Client.java
SFTPFile.java
JobRunner.java
ExcelJob.java
Client.java class has Main function and it expects input file. as of now we have given hard coded value. like below
this job will run continuously once we start mail class. can you help me, how to add this java code to custom processor.
My custom processor will need to accept one input property. it will allow us to give input CSV file path.. Once we run that processor, it will take the input csv file and trigger the Client main class. Main class will run continuously. Code written like that.
in Below code, we hard coded csv file path. we want to make that as property allow to give path in processor.
-------------
public class Client { public static void main(String[] args) throws SchedulerException {
JobRunner job = new JobRunner("D:\\nifi_test\\java_test_nifi_csv_file.csv");
job.run();
}
-------------------------rest classes, we used inner.
I just created one custom processor as per
https://community.hortonworks.com/articles/4318/build-custom-nifi-processor.html
but confused, how to copy my code to custom processor. Can you please help me on this.
i am attaching my code for reference.
Please use attached code and share nar file if possible.sftp.zip
Thanks,
Rangareddy Y
Created 07-15-2018 08:34 AM
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.processors.rangareddy_doubt; import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.components.PropertyValue; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.flowfile.attributes.CoreAttributes; import org.apache.nifi.processor.*; import org.apache.commons.io.IOUtils; import org.apache.nifi.annotation.behavior.ReadsAttribute; import org.apache.nifi.annotation.behavior.ReadsAttributes; import org.apache.nifi.annotation.behavior.WritesAttribute; import org.apache.nifi.annotation.behavior.WritesAttributes; import org.apache.nifi.annotation.lifecycle.OnScheduled; import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.SeeAlso; import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.processor.exception.ProcessException; import org.apache.nifi.processor.io.InputStreamCallback; import org.apache.nifi.processor.io.OutputStreamCallback; import org.apache.nifi.processor.util.StandardValidators; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.*; import java.util.concurrent.atomic.AtomicReference; @Tags({"example"}) @CapabilityDescription("Provide a description") @SeeAlso({}) @ReadsAttributes({@ReadsAttribute(attribute="", description="")}) @WritesAttributes({@WritesAttribute(attribute="", description="")}) public class ClientProcessor extends AbstractProcessor { public static final PropertyDescriptor MY_PROPERTY = new PropertyDescriptor .Builder().name("My Property") .description("Example Property") .required(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .build(); public static final Relationship MY_RELATIONSHIP = new Relationship.Builder() .name("my_relationship") .description("Example relationship") .build(); private List<PropertyDescriptor> descriptors; private Set<Relationship> relationships; @Override protected void init(final ProcessorInitializationContext context) { final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>(); descriptors.add(MY_PROPERTY); this.descriptors = Collections.unmodifiableList(descriptors); final Set<Relationship> relationships = new HashSet<Relationship>(); relationships.add(MY_RELATIONSHIP); this.relationships = Collections.unmodifiableSet(relationships); } @Override public Set<Relationship> getRelationships() { return this.relationships; } @Override public final List<PropertyDescriptor> getSupportedPropertyDescriptors() { return descriptors; } @OnScheduled public void onScheduled(final ProcessContext context) { } @Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException, SchedulerException { FlowFile flowFile = session.get(); final AtomicReference<String> value = new AtomicReference<>(); if ( flowFile == null ) { return; } session.read(flowFile, new InputStreamCallback() { @Override public void process(InputStream in) throws IOException {// here InputStream "in" would be your inputstream which is coming from some other processor like GetFile or your sftp server. String input = IOUtils.toString(in, "UTF-8"); JobRunner job = new JobRunner(input); // you can pass also in as inputstream of your file or direct file content which is in string format. job.run(); value.set(input); } }); flowFile = session.write(flowFile, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { out.write(value.toString().getBytes()); } }); flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/csv"); } } it is the code to create a custom processor for nifi.
Created 07-14-2018 07:03 PM
use onTrigger method to write your code whichever there in main method, and instead of passing full path address pass from that constructor input stream of that file. Give me one day I will come with nar file of that custom processor
Created 07-15-2018 08:34 AM
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.processors.rangareddy_doubt; import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.components.PropertyValue; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.flowfile.attributes.CoreAttributes; import org.apache.nifi.processor.*; import org.apache.commons.io.IOUtils; import org.apache.nifi.annotation.behavior.ReadsAttribute; import org.apache.nifi.annotation.behavior.ReadsAttributes; import org.apache.nifi.annotation.behavior.WritesAttribute; import org.apache.nifi.annotation.behavior.WritesAttributes; import org.apache.nifi.annotation.lifecycle.OnScheduled; import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.SeeAlso; import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.processor.exception.ProcessException; import org.apache.nifi.processor.io.InputStreamCallback; import org.apache.nifi.processor.io.OutputStreamCallback; import org.apache.nifi.processor.util.StandardValidators; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.*; import java.util.concurrent.atomic.AtomicReference; @Tags({"example"}) @CapabilityDescription("Provide a description") @SeeAlso({}) @ReadsAttributes({@ReadsAttribute(attribute="", description="")}) @WritesAttributes({@WritesAttribute(attribute="", description="")}) public class ClientProcessor extends AbstractProcessor { public static final PropertyDescriptor MY_PROPERTY = new PropertyDescriptor .Builder().name("My Property") .description("Example Property") .required(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .build(); public static final Relationship MY_RELATIONSHIP = new Relationship.Builder() .name("my_relationship") .description("Example relationship") .build(); private List<PropertyDescriptor> descriptors; private Set<Relationship> relationships; @Override protected void init(final ProcessorInitializationContext context) { final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>(); descriptors.add(MY_PROPERTY); this.descriptors = Collections.unmodifiableList(descriptors); final Set<Relationship> relationships = new HashSet<Relationship>(); relationships.add(MY_RELATIONSHIP); this.relationships = Collections.unmodifiableSet(relationships); } @Override public Set<Relationship> getRelationships() { return this.relationships; } @Override public final List<PropertyDescriptor> getSupportedPropertyDescriptors() { return descriptors; } @OnScheduled public void onScheduled(final ProcessContext context) { } @Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException, SchedulerException { FlowFile flowFile = session.get(); final AtomicReference<String> value = new AtomicReference<>(); if ( flowFile == null ) { return; } session.read(flowFile, new InputStreamCallback() { @Override public void process(InputStream in) throws IOException {// here InputStream "in" would be your inputstream which is coming from some other processor like GetFile or your sftp server. String input = IOUtils.toString(in, "UTF-8"); JobRunner job = new JobRunner(input); // you can pass also in as inputstream of your file or direct file content which is in string format. job.run(); value.set(input); } }); flowFile = session.write(flowFile, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { out.write(value.toString().getBytes()); } }); flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/csv"); } } it is the code to create a custom processor for nifi.
Created 07-17-2018 06:24 AM
Thanks for the reply @Gourav Bhattacharya. Now i started the custom processor. Once jobRunner class starts, it will run continuously. i am not able to stop the process. even i stop custom process, it is not stopping the jobRunner. there is no link between custom processor stop and JobRunner class stop. Please suggest me how to implement stop mechanism in custom processor. if i stop custom processor, it shold stop jobRunner also.
Thanks,
Rangareddy Y
Created 08-03-2018 05:16 AM
can you send me the job runner code exact line with comment