Search in sources :

Example 6 with LookupFailureException

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());
}
Also used : MapRecord(org.apache.nifi.serialization.record.MapRecord) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) Optional(java.util.Optional) HashMap(java.util.HashMap) Document(org.bson.Document) Test(org.junit.Test)

Example 7 with LookupFailureException

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);
    }
}
Also used : LookupFailureException(org.apache.nifi.lookup.LookupFailureException) ProcessException(org.apache.nifi.processor.exception.ProcessException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) PropertyValue(org.apache.nifi.components.PropertyValue) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with LookupFailureException

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);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) HashMap(java.util.HashMap) Record(org.apache.nifi.serialization.record.Record)

Aggregations

LookupFailureException (org.apache.nifi.lookup.LookupFailureException)8 HashMap (java.util.HashMap)7 MapRecord (org.apache.nifi.serialization.record.MapRecord)4 Document (org.bson.Document)4 IOException (java.io.IOException)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)2 Record (org.apache.nifi.serialization.record.Record)2 RecordField (org.apache.nifi.serialization.record.RecordField)2 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)2 InvalidDatabaseException (com.maxmind.db.InvalidDatabaseException)1 AnonymousIpResponse (com.maxmind.geoip2.model.AnonymousIpResponse)1 CityResponse (com.maxmind.geoip2.model.CityResponse)1 ConnectionTypeResponse (com.maxmind.geoip2.model.ConnectionTypeResponse)1 ConnectionType (com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType)1 DomainResponse (com.maxmind.geoip2.model.DomainResponse)1 IspResponse (com.maxmind.geoip2.model.IspResponse)1 InetAddress (java.net.InetAddress)1