private Record createRecord(final CityResponse city) { if (city == null) { return null; } final Map<String, Object> values = new HashMap<>(); values.put(CitySchema.CITY.getFieldName(), city.getCity().getName()); final Location location = city.getLocation(); values.put(CitySchema.ACCURACY.getFieldName(), location.getAccuracyRadius()); values.put(CitySchema.METRO_CODE.getFieldName(), location.getMetroCode()); values.put(CitySchema.TIMEZONE.getFieldName(), location.getTimeZone()); values.put(CitySchema.LATITUDE.getFieldName(), location.getLatitude()); values.put(CitySchema.LONGITUDE.getFieldName(), location.getLongitude()); values.put(CitySchema.CONTINENT.getFieldName(), city.getContinent().getName()); values.put(CitySchema.POSTALCODE.getFieldName(), city.getPostal().getCode()); values.put(CitySchema.COUNTRY.getFieldName(), createRecord(city.getCountry())); final Object[] subdivisions = new Object[city.getSubdivisions().size()]; int i = 0; for (final Subdivision subdivision : city.getSubdivisions()) { subdivisions[i++] = createRecord(subdivision); } values.put(CitySchema.SUBDIVISIONS.getFieldName(), subdivisions); return new MapRecord(CitySchema.GEO_SCHEMA, values); }
@VisibleForTesting Optional<GeoLocationInformation> extractGeoLocationInformation(Object fieldValue) { final InetAddress ipAddress; if (fieldValue instanceof InetAddress) { ipAddress = (InetAddress) fieldValue; } else if (fieldValue instanceof String) { ipAddress = getIpFromFieldValue((String) fieldValue); } else { ipAddress = null; } GeoLocationInformation geoLocationInformation = null; if (ipAddress != null) { try (Timer.Context ignored = resolveTime.time()) { final CityResponse response = databaseReader.city(ipAddress); final Location location = response.getLocation(); final Country country = response.getCountry(); final City city = response.getCity(); geoLocationInformation = GeoLocationInformation.create( location.getLatitude(), location.getLongitude(), country.getGeoNameId() != null ? country.getIsoCode() : "N/A", city.getGeoNameId() != null ? city.getName() : "N/A" // calling to .getName() may throw a NPE ); } catch (Exception e) { LOG.debug("Could not get location from IP {}", ipAddress.getHostAddress(), e); } } return Optional.ofNullable(geoLocationInformation); }
final Location location = city.getLocation(); final Map<Object, Object> map = new HashMap<>(); map.put("city", city.getCity()); map.put("continent", city.getContinent()); map.put("country", city.getCountry()); map.put("location", location); map.put("postal", city.getPostal()); map.put("registered_country", city.getRegisteredCountry()); map.put("represented_country", city.getRepresentedCountry()); map.put("subdivisions", city.getSubdivisions()); map.put("traits", city.getTraits());
switch (attribute) { case "country_code": put(properties,"_country_code", city.getCountry().getIsoCode()); break; case "region": put(properties,"_region", city.getContinent().getName()); break; case "city": put(properties,"_city", city.getCity().getName()); break; case "latitude": put(properties,"_latitude", city.getLocation().getLatitude()); break; case "longitude": put(properties,"_longitude", city.getLocation().getLongitude()); break; case "timezone": put(properties,"_timezone", city.getLocation().getTimeZone()); break;
@Override public Object getValue(CityResponse response) { return response.getLocation().getTimeZone(); } },
@Override public Location resolve(String ip) { try { CityResponse response = reader.city(InetAddress.getByName(ip)); Location location = new Location(); location.setCity(response.getCity().getName()); location.setCountryName(response.getCountry().getName()); location.setCountryCode(response.getCountry().getIsoCode()); location.setIp(ip); return location; } catch (IOException | GeoIp2Exception ex) { LOG.debug("Unable to resolve ip location", ex); } return null; }
@Override public String getValue(CityResponse response) { return response.getCountry().getName(); } },
@Override public String getValue(CityResponse response) { return response.getCity().getName(); } },
@Override public Object getValue(CityResponse response) { return response.getPostal().getCode(); } },
@Override public String getValue(CityResponse response) { return response.getContinent().getCode(); } },
private Map<String, Object> ip2SpatialData(InetAddress ip) { if (!enabled) { return null; } if (reader == null) { LOG.warn("Location database is not initialized. Exiting."); return null; } try { Map<String, Object> holder = new HashMap<>(3); if (dbType == LocationDatabaseType.COUNTRY) { Country country = reader.country(ip).getCountry(); holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), country.getIsoCode()); } else { CityResponse city = reader.city(ip); Location loc = city.getLocation(); holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), city.getCountry().getIsoCode()); holder.put(ObjectEsParameterFactory.GEOLOC_CITY_NAME.getName(), city.getCity().getName()); holder.put(ObjectEsParameterFactory.GEOLOC_GEO_POINT.getName(), new GeoPoint(loc.getLatitude(), loc.getLongitude())); } return holder; } catch (Throwable e) { LOG.warn("Can't convert IP to GeoIp", e); } return null; }
throw event.buildException("City not found for " + ipInfo.toString()); country = response.getCountry(); city = response.getCity(); continent = response.getContinent(); location = response.getLocation(); postal = response.getPostal(); registred_country = response.getRegisteredCountry(); represented_country = response.getRepresentedCountry(); subdivision = response.getSubdivisions(); break;
@Override public Double getValue(CityResponse response) { return response.getLocation().getLatitude(); } },
@Override public String getValue(CityResponse response) { return response.getCountry().getIsoCode(); } },
address.setCountry(response.getCountry().getIsoCode()); address.setPostalCode(response.getPostal().getCode()); address.setZone(response.getMostSpecificSubdivision().getIsoCode()); address.setCity(response.getCity().getName());
InetAddress ipAddress = InetAddress.getByName(ip); CityResponse cityResponse = geoipLookup.city(ipAddress); city = cityResponse.getCity().getName(); country = cityResponse.getCountry().getName(); countryCode = cityResponse.getCountry().getIsoCode(); longitude = cityResponse.getLocation().getLongitude(); latitude = cityResponse.getLocation().getLatitude(); if (verbose) { data += (", country = " + country);
@Override public Object getValue(CityResponse response) { return response.getLocation().getLongitude(); } },
public void map(Object key, Text line, Context context) throws IOException, InterruptedException { InetAddress ipAddress = InetAddress.getByName(address.getHostAddress()); CityResponse response = null; try { response = reader.city(ipAddress); } catch (GeoIp2Exception ex) { ex.printStackTrace(); return; } Country country = response.getCountry(); String count = country.getName(); // 'US' if (country.getName() == null) { return; }
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)); 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);
access.setIp(ip); access.setDate(Instant.now().toEpochMilli()); access.setCity(cr.getCity().getName()); access.setCountry(cr.getCountry().getName()); access.setMessage(null); access.setLl(new Double[] { cr.getLocation().getLatitude(), cr.getLocation().getLongitude() });