@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); }
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);
map.put("city", city.getCity()); map.put("continent", city.getContinent()); map.put("country", city.getCountry()); map.put("location", location); map.put("postal", city.getPostal());
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); }
address.setCountry(response.getCountry().getIsoCode()); address.setPostalCode(response.getPostal().getCode()); address.setZone(response.getMostSpecificSubdivision().getIsoCode());
HashMap<String, String> geoInfo = new HashMap<>(); Country country = cityResponse.getCountry(); City city = cityResponse.getCity(); Postal postal = cityResponse.getPostal();
@Override public String getValue(CityResponse response) { return response.getCountry().getIsoCode(); } },
@Override public String getValue(CityResponse response) { return response.getCountry().getName(); } },
private GeoInfo _getGeoInfo(String ipAddress) { GeoInfo geoInfo = new GeoInfo(); if (ipAddress == null) { return geoInfo; } try{ InetAddress inetAddress = InetAddress.getByName(ipAddress); CityResponse response = reader.city(inetAddress); Country country = response.getCountry(); Continent continent = response.getContinent(); City city = response.getCity(); Postal postal = response.getPostal(); Location location = response.getLocation(); Subdivision subdivision = response.getMostSpecificSubdivision(); geoInfo.setRegion(subdivision.getName()); geoInfo.setCountryIsoCode(country.getIsoCode()); geoInfo.setCountry(country.getName()); geoInfo.setContinent(continent.getCode()); geoInfo.setCity(city.getName()); geoInfo.setPostalCode(postal.getCode()); geoInfo.setLatitude(location.getLatitude()); geoInfo.setLongitude(location.getLongitude()); return geoInfo; } catch(Exception ex){ return null; } }
switch (attribute) { case "country_code": put(properties,"_country_code", city.getCountry().getIsoCode()); break; case "region":
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; }
@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; }
if (l.getCountry()!=null) { if (name.length()==0) { name.append(l.getCountry().getName()); } else { name.append(" ("); name.append(l.getCountry().getIsoCode()); name.append(")");
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);
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);
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; }
if (cityResponse.getCountry().getName() != null) { session.setProperty("sessionCountryCode", cityResponse.getCountry().getIsoCode()); session.setProperty("sessionCountryName", cityResponse.getCountry().getName());
access.setDate(Instant.now().toEpochMilli()); access.setCity(cr.getCity().getName()); access.setCountry(cr.getCountry().getName());
InetAddress ipAddress = InetAddress.getByName(ip); CityResponse location = locationService.city(ipAddress); String countryCode = location.getCountry().getIsoCode(); double latitude = location.getLocation().getLatitude(); double longitude = location.getLocation().getLongitude();
InetAddress ipAddress = InetAddress.getByName(ip); CityResponse location = locationService.city(ipAddress); String countryCode = location.getCountry().getIsoCode(); double latitude = location.getLocation().getLatitude(); double longitude = location.getLocation().getLongitude();