use of org.apache.nifi.processors.maxmind.DatabaseReader in project nifi by apache.
the class AbstractEnrichIP method onScheduled.
@OnScheduled
public void onScheduled(final ProcessContext context) throws IOException {
final String dbFileString = context.getProperty(GEO_DATABASE_FILE).getValue();
final File dbFile = new File(dbFileString);
final StopWatch stopWatch = new StopWatch(true);
final DatabaseReader reader = new DatabaseReader.Builder(dbFile).build();
stopWatch.stop();
getLogger().info("Completed loading of Maxmind Database. Elapsed time was {} milliseconds.", new Object[] { stopWatch.getDuration(TimeUnit.MILLISECONDS) });
databaseReaderRef.set(reader);
}
use of org.apache.nifi.processors.maxmind.DatabaseReader in project nifi by apache.
the class GeoEnrichIP method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final DatabaseReader dbReader = databaseReaderRef.get();
final String ipAttributeName = context.getProperty(IP_ADDRESS_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
final String ipAttributeValue = flowFile.getAttribute(ipAttributeName);
if (StringUtils.isEmpty(ipAttributeName)) {
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("FlowFile '{}' attribute '{}' was empty. Routing to failure", new Object[] { flowFile, IP_ADDRESS_ATTRIBUTE.getDisplayName() });
return;
}
InetAddress inetAddress = null;
CityResponse response = null;
try {
inetAddress = InetAddress.getByName(ipAttributeValue);
} catch (final IOException ioe) {
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("Could not resolve the IP for value '{}', contained within the attribute '{}' in " + "FlowFile '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the processor with an invalid IP address ", new Object[] { ipAttributeValue, IP_ADDRESS_ATTRIBUTE.getDisplayName(), flowFile }, ioe);
return;
}
final StopWatch stopWatch = new StopWatch(true);
try {
response = dbReader.city(inetAddress);
stopWatch.stop();
} catch (final IOException | GeoIp2Exception ex) {
// Note IOException is captured again as dbReader also makes InetAddress.getByName() calls.
// Most name or IP resolutions failure should have been triggered in the try loop above but
// environmental conditions may trigger errors during the second resolution as well.
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("Failure while trying to find enrichment data for {} due to {}", new Object[] { flowFile, ex }, ex);
return;
}
if (response == null) {
session.transfer(flowFile, REL_NOT_FOUND);
return;
}
final Map<String, String> attrs = new HashMap<>();
attrs.put(new StringBuilder(ipAttributeName).append(".geo.lookup.micros").toString(), String.valueOf(stopWatch.getDuration(TimeUnit.MICROSECONDS)));
attrs.put(new StringBuilder(ipAttributeName).append(".geo.city").toString(), response.getCity().getName());
final Double latitude = response.getLocation().getLatitude();
if (latitude != null) {
attrs.put(new StringBuilder(ipAttributeName).append(".geo.latitude").toString(), latitude.toString());
}
final Double longitude = response.getLocation().getLongitude();
if (longitude != null) {
attrs.put(new StringBuilder(ipAttributeName).append(".geo.longitude").toString(), longitude.toString());
}
final Integer accuracy = response.getLocation().getAccuracyRadius();
if (accuracy != null) {
attrs.put(new StringBuilder(ipAttributeName).append(".accuracy").toString(), String.valueOf(accuracy));
}
int i = 0;
for (final Subdivision subd : response.getSubdivisions()) {
attrs.put(new StringBuilder(ipAttributeName).append(".geo.subdivision.").append(i).toString(), subd.getName());
attrs.put(new StringBuilder(ipAttributeName).append(".geo.subdivision.isocode.").append(i).toString(), subd.getIsoCode());
i++;
}
attrs.put(new StringBuilder(ipAttributeName).append(".geo.country").toString(), response.getCountry().getName());
attrs.put(new StringBuilder(ipAttributeName).append(".geo.country.isocode").toString(), response.getCountry().getIsoCode());
attrs.put(new StringBuilder(ipAttributeName).append(".geo.postalcode").toString(), response.getPostal().getCode());
flowFile = session.putAllAttributes(flowFile, attrs);
session.transfer(flowFile, REL_FOUND);
}
use of org.apache.nifi.processors.maxmind.DatabaseReader in project nifi by apache.
the class ISPEnrichIP method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final DatabaseReader dbReader = databaseReaderRef.get();
final String ipAttributeName = context.getProperty(IP_ADDRESS_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
final String ipAttributeValue = flowFile.getAttribute(ipAttributeName);
if (StringUtils.isEmpty(ipAttributeName)) {
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("FlowFile '{}' attribute '{}' was empty. Routing to failure", new Object[] { flowFile, IP_ADDRESS_ATTRIBUTE.getDisplayName() });
return;
}
InetAddress inetAddress = null;
IspResponse response = null;
try {
inetAddress = InetAddress.getByName(ipAttributeValue);
} catch (final IOException ioe) {
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("Could not resolve the IP for value '{}', contained within the attribute '{}' in " + "FlowFile '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the processor with an invalid IP address ", new Object[] { ipAttributeValue, IP_ADDRESS_ATTRIBUTE.getDisplayName(), flowFile }, ioe);
return;
}
final StopWatch stopWatch = new StopWatch(true);
try {
response = dbReader.isp(inetAddress);
stopWatch.stop();
} catch (final IOException | GeoIp2Exception ex) {
// Note IOException is captured again as dbReader also makes InetAddress.getByName() calls.
// Most name or IP resolutions failure should have been triggered in the try loop above but
// environmental conditions may trigger errors during the second resolution as well.
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("Failure while trying to find enrichment data for {} due to {}", new Object[] { flowFile, ex }, ex);
return;
}
if (response == null) {
session.transfer(flowFile, REL_NOT_FOUND);
return;
}
final Map<String, String> attrs = new HashMap<>();
attrs.put(new StringBuilder(ipAttributeName).append(".isp.lookup.micros").toString(), String.valueOf(stopWatch.getDuration(TimeUnit.MICROSECONDS)));
// seem like good option to "final int asn ..." as with the other returned data.
if (!(response.getAutonomousSystemNumber() == null)) {
attrs.put(new StringBuilder(ipAttributeName).append(".isp.asn").toString(), String.valueOf(response.getAutonomousSystemNumber()));
}
final String asnOrg = response.getAutonomousSystemOrganization();
if (asnOrg != null) {
attrs.put(new StringBuilder(ipAttributeName).append(".isp.asn.organization").toString(), asnOrg);
}
final String ispName = response.getIsp();
if (ispName != null) {
attrs.put(new StringBuilder(ipAttributeName).append(".isp.name").toString(), ispName);
}
final String organisation = response.getOrganization();
if (organisation != null) {
attrs.put(new StringBuilder(ipAttributeName).append(".isp.organization").toString(), organisation);
}
flowFile = session.putAllAttributes(flowFile, attrs);
session.transfer(flowFile, REL_FOUND);
}
Aggregations