Search in sources :

Example 1 with LookupFailureException

use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.

the class HBase_1_1_2_RecordLookupService method lookup.

@Override
public Optional<Record> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    if (coordinates.get(ROW_KEY_KEY) == null) {
        return Optional.empty();
    }
    final String rowKey = coordinates.get(ROW_KEY_KEY).toString();
    if (StringUtils.isBlank(rowKey)) {
        return Optional.empty();
    }
    final byte[] rowKeyBytes = rowKey.getBytes(StandardCharsets.UTF_8);
    try {
        final Map<String, Object> values = new HashMap<>();
        hBaseClientService.scan(tableName, rowKeyBytes, rowKeyBytes, columns, (byte[] row, ResultCell[] resultCells) -> {
            for (final ResultCell cell : resultCells) {
                final byte[] qualifier = Arrays.copyOfRange(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierOffset() + cell.getQualifierLength());
                final byte[] value = Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueOffset() + cell.getValueLength());
                values.put(new String(qualifier, charset), new String(value, charset));
            }
        });
        if (values.size() > 0) {
            final List<RecordField> fields = new ArrayList<>();
            for (String key : values.keySet()) {
                fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
            }
            final RecordSchema schema = new SimpleRecordSchema(fields);
            return Optional.ofNullable(new MapRecord(schema, values));
        } else {
            return Optional.empty();
        }
    } catch (IOException e) {
        getLogger().error("Error occurred loading {}", new Object[] { coordinates.get("rowKey") }, e);
        throw new LookupFailureException(e);
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResultCell(org.apache.nifi.hbase.scan.ResultCell) IOException(java.io.IOException) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema)

Example 2 with LookupFailureException

use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.

the class IPLookupService method doLookup.

private Optional<Record> doLookup(final DatabaseReader databaseReader, final Map<String, Object> coordinates) throws LookupFailureException, InvalidDatabaseException {
    if (coordinates.get(IP_KEY) == null) {
        return Optional.empty();
    }
    final String ipAddress = coordinates.get(IP_KEY).toString();
    final InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName(ipAddress);
    } catch (final IOException ioe) {
        getLogger().warn("Could not resolve the IP for value '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the service with an invalid IP address", new Object[] { coordinates }, ioe);
        return Optional.empty();
    }
    final Record geoRecord;
    if (getProperty(LOOKUP_CITY).asBoolean()) {
        final CityResponse cityResponse;
        try {
            cityResponse = databaseReader.city(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup City information for IP Address " + inetAddress, e);
        }
        geoRecord = createRecord(cityResponse);
    } else {
        geoRecord = null;
    }
    final Record ispRecord;
    if (getProperty(LOOKUP_ISP).asBoolean()) {
        final IspResponse ispResponse;
        try {
            ispResponse = databaseReader.isp(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup ISP information for IP Address " + inetAddress, e);
        }
        ispRecord = createRecord(ispResponse);
    } else {
        ispRecord = null;
    }
    final String domainName;
    if (getProperty(LOOKUP_DOMAIN).asBoolean()) {
        final DomainResponse domainResponse;
        try {
            domainResponse = databaseReader.domain(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup Domain information for IP Address " + inetAddress, e);
        }
        domainName = domainResponse == null ? null : domainResponse.getDomain();
    } else {
        domainName = null;
    }
    final String connectionType;
    if (getProperty(LOOKUP_CONNECTION_TYPE).asBoolean()) {
        final ConnectionTypeResponse connectionTypeResponse;
        try {
            connectionTypeResponse = databaseReader.connectionType(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup Domain information for IP Address " + inetAddress, e);
        }
        if (connectionTypeResponse == null) {
            connectionType = null;
        } else {
            final ConnectionType type = connectionTypeResponse.getConnectionType();
            connectionType = type == null ? null : type.name();
        }
    } else {
        connectionType = null;
    }
    final Record anonymousIpRecord;
    if (getProperty(LOOKUP_ANONYMOUS_IP_INFO).asBoolean()) {
        final AnonymousIpResponse anonymousIpResponse;
        try {
            anonymousIpResponse = databaseReader.anonymousIp(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup Anonymous IP Information for IP Address " + inetAddress, e);
        }
        anonymousIpRecord = createRecord(anonymousIpResponse);
    } else {
        anonymousIpRecord = null;
    }
    if (geoRecord == null && ispRecord == null && domainName == null && connectionType == null && anonymousIpRecord == null) {
        return Optional.empty();
    }
    return Optional.ofNullable(createContainerRecord(geoRecord, ispRecord, domainName, connectionType, anonymousIpRecord));
}
Also used : ConnectionType(com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType) IOException(java.io.IOException) ConnectionTypeResponse(com.maxmind.geoip2.model.ConnectionTypeResponse) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) InvalidDatabaseException(com.maxmind.db.InvalidDatabaseException) IOException(java.io.IOException) AnonymousIpResponse(com.maxmind.geoip2.model.AnonymousIpResponse) CityResponse(com.maxmind.geoip2.model.CityResponse) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) IspResponse(com.maxmind.geoip2.model.IspResponse) InvalidDatabaseException(com.maxmind.db.InvalidDatabaseException) DomainResponse(com.maxmind.geoip2.model.DomainResponse) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) InetAddress(java.net.InetAddress)

Example 3 with LookupFailureException

use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.

the class MongoDBLookupService method lookup.

@Override
public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    Map<String, Object> clean = new HashMap<>();
    clean.putAll(coordinates);
    Document query = new Document(clean);
    if (coordinates.size() == 0) {
        throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
    }
    try {
        Document result = this.findOne(query);
        if (result == null) {
            return Optional.empty();
        } else if (!StringUtils.isEmpty(lookupValueField)) {
            return Optional.ofNullable(result.get(lookupValueField));
        } else {
            final List<RecordField> fields = new ArrayList<>();
            for (String key : result.keySet()) {
                if (key.equals("_id")) {
                    continue;
                }
                fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
            }
            final RecordSchema schema = new SimpleRecordSchema(fields);
            return Optional.ofNullable(new MapRecord(schema, result));
        }
    } catch (Exception ex) {
        getLogger().error("Error during lookup {}", new Object[] { query.toJson() }, ex);
        throw new LookupFailureException(ex);
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) Document(org.bson.Document) InitializationException(org.apache.nifi.reporting.InitializationException) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) IOException(java.io.IOException) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) ArrayList(java.util.ArrayList) List(java.util.List) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema)

Example 4 with LookupFailureException

use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.

the class MongoDBLookupServiceIT method testLookupSingle.

@Test
public void testLookupSingle() throws Exception {
    runner.setProperty(service, MongoDBLookupService.LOOKUP_VALUE_FIELD, "message");
    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.assertEquals("The value was wrong.", "Hello, world", result.get());
    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 : LookupFailureException(org.apache.nifi.lookup.LookupFailureException) Optional(java.util.Optional) HashMap(java.util.HashMap) Document(org.bson.Document) Test(org.junit.Test)

Example 5 with LookupFailureException

use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.

the class MongoDBLookupServiceIT method testServiceParameters.

@Test
public void testServiceParameters() throws Exception {
    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");
    boolean error = false;
    try {
        service.lookup(criteria);
    } catch (Exception ex) {
        error = true;
    }
    Assert.assertFalse("An error was thrown when no error should have been thrown.", error);
    error = false;
    try {
        service.lookup(new HashMap());
    } catch (Exception ex) {
        error = true;
        Assert.assertTrue("The exception was the wrong type", ex instanceof LookupFailureException);
    }
    Assert.assertTrue("An error was not thrown when the input was empty", error);
}
Also used : LookupFailureException(org.apache.nifi.lookup.LookupFailureException) HashMap(java.util.HashMap) Document(org.bson.Document) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) Test(org.junit.Test)

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