use of org.apache.nifi.processor.Relationship in project nifi by apache.
the class ConsumeAMQP method processResource.
/**
* Will construct a {@link FlowFile} containing the body of the consumed AMQP message (if {@link GetResponse} returned by {@link AMQPConsumer} is
* not null) and AMQP properties that came with message which are added to a {@link FlowFile} as attributes, transferring {@link FlowFile} to
* 'success' {@link Relationship}.
*/
@Override
protected void processResource(final Connection connection, final AMQPConsumer consumer, final ProcessContext context, final ProcessSession session) {
final GetResponse response = consumer.consume();
if (response == null) {
context.yield();
return;
}
FlowFile flowFile = session.create();
flowFile = session.write(flowFile, out -> out.write(response.getBody()));
final BasicProperties amqpProperties = response.getProps();
final Map<String, String> attributes = buildAttributes(amqpProperties);
flowFile = session.putAllAttributes(flowFile, attributes);
session.getProvenanceReporter().receive(flowFile, connection.toString() + "/" + context.getProperty(QUEUE).getValue());
session.transfer(flowFile, REL_SUCCESS);
}
use of org.apache.nifi.processor.Relationship in project nifi by apache.
the class TestExtractMediaMetadata method testRelationships.
@Test
public void testRelationships() {
final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata());
ProcessContext context = runner.getProcessContext();
Set<Relationship> relationships = context.getAvailableRelationships();
assertEquals(2, relationships.size());
assertTrue(relationships.contains(ExtractMediaMetadata.SUCCESS));
assertTrue(relationships.contains(ExtractMediaMetadata.FAILURE));
}
use of org.apache.nifi.processor.Relationship in project nifi by apache.
the class RouteOnAttribute method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final ComponentLog logger = getLogger();
final Map<Relationship, PropertyValue> propMap = this.propertyMap;
final Set<Relationship> matchingRelationships = new HashSet<>();
for (final Map.Entry<Relationship, PropertyValue> entry : propMap.entrySet()) {
final PropertyValue value = entry.getValue();
final boolean matches = value.evaluateAttributeExpressions(flowFile).asBoolean();
if (matches) {
matchingRelationships.add(entry.getKey());
}
}
final Set<Relationship> destinationRelationships = new HashSet<>();
switch(context.getProperty(ROUTE_STRATEGY).getValue()) {
case routeAllMatchValue:
if (matchingRelationships.size() == propMap.size()) {
destinationRelationships.add(REL_MATCH);
} else {
destinationRelationships.add(REL_NO_MATCH);
}
break;
case routeAnyMatches:
if (matchingRelationships.isEmpty()) {
destinationRelationships.add(REL_NO_MATCH);
} else {
destinationRelationships.add(REL_MATCH);
}
break;
case routePropertyNameValue:
default:
destinationRelationships.addAll(matchingRelationships);
break;
}
if (destinationRelationships.isEmpty()) {
logger.info("Routing {} to unmatched", new Object[] { flowFile });
flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, REL_NO_MATCH.getName());
session.getProvenanceReporter().route(flowFile, REL_NO_MATCH);
session.transfer(flowFile, REL_NO_MATCH);
} else {
final Iterator<Relationship> relationshipNameIterator = destinationRelationships.iterator();
final Relationship firstRelationship = relationshipNameIterator.next();
final Map<Relationship, FlowFile> transferMap = new HashMap<>();
final Set<FlowFile> clones = new HashSet<>();
// make all the clones for any remaining relationships
while (relationshipNameIterator.hasNext()) {
final Relationship relationship = relationshipNameIterator.next();
final FlowFile cloneFlowFile = session.clone(flowFile);
clones.add(cloneFlowFile);
transferMap.put(relationship, cloneFlowFile);
}
// now transfer any clones generated
for (final Map.Entry<Relationship, FlowFile> entry : transferMap.entrySet()) {
logger.info("Cloned {} into {} and routing clone to relationship {}", new Object[] { flowFile, entry.getValue(), entry.getKey() });
FlowFile updatedFlowFile = session.putAttribute(entry.getValue(), ROUTE_ATTRIBUTE_KEY, entry.getKey().getName());
session.getProvenanceReporter().route(updatedFlowFile, entry.getKey());
session.transfer(updatedFlowFile, entry.getKey());
}
// now transfer the original flow file
logger.info("Routing {} to {}", new Object[] { flowFile, firstRelationship });
session.getProvenanceReporter().route(flowFile, firstRelationship);
flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, firstRelationship.getName());
session.transfer(flowFile, firstRelationship);
}
}
use of org.apache.nifi.processor.Relationship in project nifi by apache.
the class RouteOnContent method onPropertyModified.
@Override
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {
if (descriptor.isDynamic()) {
final Set<Relationship> relationships = new HashSet<>(this.relationships.get());
final Relationship relationship = new Relationship.Builder().name(descriptor.getName()).build();
if (newValue == null) {
relationships.remove(relationship);
} else {
relationships.add(relationship);
}
this.relationships.set(relationships);
}
}
use of org.apache.nifi.processor.Relationship in project nifi by apache.
the class RouteText method onScheduled.
/**
* When this processor is scheduled, update the dynamic properties into the map
* for quick access during each onTrigger call
*
* @param context ProcessContext used to retrieve dynamic properties
*/
@OnScheduled
public void onScheduled(final ProcessContext context) {
final String regex = context.getProperty(GROUPING_REGEX).getValue();
if (regex != null) {
groupingRegex = Pattern.compile(regex);
}
final Map<Relationship, PropertyValue> newPropertyMap = new HashMap<>();
for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
if (!descriptor.isDynamic()) {
continue;
}
getLogger().debug("Adding new dynamic property: {}", new Object[] { descriptor });
newPropertyMap.put(new Relationship.Builder().name(descriptor.getName()).build(), context.getProperty(descriptor));
}
this.propertyMap = newPropertyMap;
}
Aggregations