use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class MongoDBLookupServiceIT method testLookupRecord.
@Test
public void testLookupRecord() throws Exception {
runner.setProperty(service, MongoDBLookupService.LOOKUP_VALUE_FIELD, "");
runner.enableControllerService(service);
Document document = service.convertJson("{ \"uuid\": \"x-y-z\", \"message\": \"Hello, world\" }");
service.insert(document);
Map<String, Object> criteria = new HashMap<>();
criteria.put("uuid", "x-y-z");
Optional result = service.lookup(criteria);
Assert.assertNotNull("The value was null.", result.get());
Assert.assertTrue("The value was wrong.", result.get() instanceof MapRecord);
MapRecord record = (MapRecord) result.get();
Assert.assertEquals("The value was wrong.", "Hello, world", record.getAsString("message"));
Assert.assertEquals("The value was wrong.", "x-y-z", record.getAsString("uuid"));
Map<String, Object> clean = new HashMap<>();
clean.putAll(criteria);
service.delete(new Document(clean));
try {
result = service.lookup(criteria);
} catch (LookupFailureException ex) {
Assert.fail();
}
Assert.assertTrue(!result.isPresent());
}
use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class LookupAttribute method onTrigger.
private void onTrigger(ComponentLog logger, LookupService lookupService, boolean includeEmptyValues, FlowFile flowFile, ProcessSession session) throws ProcessException, IOException {
final Map<String, String> attributes = new HashMap<>(flowFile.getAttributes());
boolean matched = false;
try {
final Set<String> requiredKeys = lookupService.getRequiredKeys();
if (requiredKeys == null || requiredKeys.size() != 1) {
throw new ProcessException("LookupAttribute requires a key-value lookup service supporting exactly one required key, was: " + (requiredKeys == null ? "null" : String.valueOf(requiredKeys.size())));
}
final String coordinateKey = requiredKeys.iterator().next();
for (final Map.Entry<PropertyDescriptor, PropertyValue> e : dynamicProperties.entrySet()) {
final PropertyValue lookupKeyExpression = e.getValue();
final String lookupKey = lookupKeyExpression.evaluateAttributeExpressions(flowFile).getValue();
final String attributeName = e.getKey().getName();
final Optional<String> attributeValue = lookupService.lookup(Collections.singletonMap(coordinateKey, lookupKey));
matched = putAttribute(attributeName, attributeValue, attributes, includeEmptyValues, logger) || matched;
if (!matched && logger.isDebugEnabled()) {
logger.debug("No such value for key: {}", new Object[] { lookupKey });
}
}
flowFile = session.putAllAttributes(flowFile, attributes);
session.transfer(flowFile, matched ? REL_MATCHED : REL_UNMATCHED);
} catch (final LookupFailureException e) {
logger.error(e.getMessage(), e);
session.transfer(flowFile, REL_FAILURE);
}
}
use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class TestRecordLookupProcessor method onTrigger.
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final String rowKey = context.getProperty(HBASE_ROW).getValue();
final Map<String, Object> coordinates = new HashMap<>();
coordinates.put(HBase_1_1_2_RecordLookupService.ROW_KEY_KEY, rowKey);
final LookupService<Record> lookupService = context.getProperty(HBASE_LOOKUP_SERVICE).asControllerService(LookupService.class);
try {
final Optional<Record> record = lookupService.lookup(coordinates);
if (record.isPresent()) {
lookedupRecords.add(record.get());
session.transfer(flowFile, REL_SUCCESS);
} else {
session.transfer(flowFile, REL_FAILURE);
}
} catch (LookupFailureException e) {
session.transfer(flowFile, REL_FAILURE);
}
}
Aggregations