Solved
How to create a flow files and add attributes in a custom processor
/*
* 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.demo.processors;
import org.apache.commons.lang3.StringUtils;
import org.apache.nifi.annotation.behavior.InputRequirement;
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.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.SeeAlso;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.annotation.lifecycle.OnScheduled;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.ProcessorInitializationContext;
import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.processors.standard.InvokeHTTP;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Tags({"GetMember"})
@CapabilityDescription("Get a Member details from member collection")
@SeeAlso({})
@ReadsAttributes({@ReadsAttribute(attribute = "", description = "")})
@WritesAttributes({@WritesAttribute(attribute = "", description = "")})
@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN)
public class GetMemberProcessor extends InvokeHTTP {
public static final PropertyDescriptor ID = new PropertyDescriptor
.Builder().name("ID")
.displayName("Id")
.description("Member Id")
.required(false)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();
private static final String CUSTOM_URL = "https://my-url/api/v1";
private static final String GET_MEMBER_PATH = "/members";
private List<PropertyDescriptor> descriptors;
@Override
protected void init(final ProcessorInitializationContext context) {
super.init(context);
List<PropertyDescriptor> invokeDescriptors = super.getSupportedPropertyDescriptors();
descriptors = new ArrayList<>();
if (invokeDescriptors != null) {
for (PropertyDescriptor descriptor : invokeDescriptors) {
if (descriptor.getName().equalsIgnoreCase("HTTP URL")) {
continue;
}
descriptors.add(descriptor);
}
}
PropertyDescriptor HTTP_URL = new PropertyDescriptor.Builder()
.name("HTTP URL")
.description("Get Members URL")
.required(false)
.defaultValue(CUSTOM_URL + GET_MEMBER_PATH + "/${MEMBER_ID}")
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.URL_VALIDATOR)
.build();
descriptors.add(ID);
descriptors.add(HTTP_URL);
descriptors = Collections.unmodifiableList(descriptors);
}
@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) {
String memberId = StringUtils.trimToEmpty(context.getProperty("ID").evaluateAttributeExpressions().getValue());
String uri = StringUtils.trimToEmpty(context.getProperty("HTTP URL").evaluateAttributeExpressions().getValue());
getLogger().info("ID: " + memberId + " URI Found: " + uri);
FlowFile requestFlowFile = session.get();
if (requestFlowFile == null) {
requestFlowFile = session.create();
}
session.putAttribute(requestFlowFile, "MEMBER_ID", memberId);
String uri2 = StringUtils.trimToEmpty(context.getProperty("HTTP URL").evaluateAttributeExpressions().getValue());
getLogger().info("ID: " + memberId + " URI Found after: " + uri2);
super.onTrigger(context, session);
}
}
Hi all, I am new to nifi and trying to create a new custom processor extending the invokeHttp processor. I am trying to write a processor will some default values pre configured in to invokeHttp and ask my team to use this processor to add properties that will be used to update properties in the invokeHttp properties.
I am getting the following error while trying to run the custom processor
Processing halted: yielding [1 sec]
org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=5ff2cede-6663-401e-ba5c-d72e62a00913,claim=,offset=0,name=5ff2cede-6663-401e-ba5c-d72e62a00913,size=0] transfer relationship not specified. This FlowFile was created in this session and was not transferred to any Relationship via ProcessSession.transfer()
at org.apache.nifi.controller.repository.StandardProcessSession.validateCommitState(StandardProcessSession.java:261)
at org.apache.nifi.controller.repository.StandardProcessSession.checkpoint(StandardProcessSession.java:276)
at org.apache.nifi.controller.repository.StandardProcessSession.commit(StandardProcessSession.java:559)
at org.apache.nifi.controller.repository.StandardProcessSession.commitAsync(StandardProcessSession.java:514)
at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:28)
at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1274)
at org.apache.nifi.controller.tasks.ConnectableTask.invoke(ConnectableTask.java:244)
at org.apache.nifi.controller.scheduling.AbstractTimeBasedSchedulingAgent.lambda$doScheduleOnce$0(AbstractTimeBasedSchedulingAgent.java:59)
at org.apache.nifi.engine.FlowEngine$2.run(FlowEngine.java:110)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)

