public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: DatabaseReader geoip2-file.mmdb ip"); System.exit(1); } File f = new File(args[0]); Builder b = new Builder(f); b.withCache(new CHMCache(256)); DatabaseReader r = b.build(); System.out.println("Database Metadata: " + r.getMetadata()); String c = r.country(args[1]); System.out.println("IP: " + args[1] + " country: " + c); } }
/** * Reads a new Database from a given HDFS file * @param reader The DatabaseReader to use to read the file * @param hdfsFile The HDFS file to read * @param is An InputStream for use with the reader * @return The DatabaseReader that is set up with the new file * @throws IOException If there is an issue reading the file. */ public static DatabaseReader readNewDatabase(DatabaseReader reader, String hdfsFile, InputStream is) throws IOException { LOG.info("Update to GeoIP data started with {}", hdfsFile); // InputStream based DatabaseReaders are always in memory. DatabaseReader newReader = new DatabaseReader.Builder(is).withCache(new CHMCache()).build(); // If we've never set a reader, don't close the old one if (reader != null) { reader.close(); } LOG.info("Finished update to GeoIP data started with {}", hdfsFile); return newReader; }
.withCache(new CHMCache()).build()) { CountryResponse response = reader.country(InetAddress.getByName(rp.getRemoteAddr()));
@Override public void setConf(AbstractConfig config) { this.config = (GeoIpOperationConfig) config; AmazonS3Client client = this.s3Factory.newInstance(); AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb()); GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey()); S3Object obj = client.getObject(req); try { this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache()).build(); } catch (IOException e) { throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e); } } }
@Override public void setConf(AbstractConfig config) { this.config = (GeoIpOperationConfig) config; AmazonS3Client client = this.s3Factory.newInstance(); AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb()); GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey()); S3Object obj = client.getObject(req); try { this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache()).build(); } catch (IOException e) { throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e); } } }
private static void loadDatabaseReader() { try (InputStream gzipInputStream = new GZIPInputStream(Resources.getResource("GeoLite2-City.tar.gz").openStream())) { try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream)) { databaseReader = new DatabaseReader.Builder(seekToDbFile(tarArchiveInputStream)).withCache(new CHMCache()).build(); } } catch (Exception e) { throw new RuntimeException("failed to load geoip database", e); } }
@Override public void prepareForRun() throws InvalidDissectorException { // This creates the DatabaseReader object, which should be reused across lookups. try { reader = new DatabaseReader .Builder(openDatabaseFile(databaseFileName)) .fileMode(Reader.FileMode.MEMORY) .withCache(new CHMCache()) .build(); } catch (IOException e) { throw new InvalidDissectorException(this.getClass().getCanonicalName() + ":" + e.getMessage()); } }
/** * Open a GeoIP2 database * @since 0.9.38 */ private DatabaseReader openGeoIP2(File geoFile) throws IOException { DatabaseReader.Builder b = new DatabaseReader.Builder(geoFile); b.withCache(new CHMCache(256)); DatabaseReader rv = b.build(); if (_log.shouldDebug()) _log.debug("Opened GeoIP2 Database, Metadata: " + rv.getMetadata()); return rv; }