本文整理汇总了Java中com.maxmind.geoip2.DatabaseReader类的典型用法代码示例。如果您正苦于以下问题:Java DatabaseReader类的具体用法?Java DatabaseReader怎么用?Java DatabaseReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DatabaseReader类属于com.maxmind.geoip2包,在下文中一共展示了DatabaseReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadDatabaseReaders
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
static Map<String, DatabaseReaderLazyLoader> loadDatabaseReaders(Path geoIpConfigDirectory, NodeCache cache) throws IOException {
if (Files.exists(geoIpConfigDirectory) == false && Files.isDirectory(geoIpConfigDirectory)) {
throw new IllegalStateException("the geoip directory [" + geoIpConfigDirectory + "] containing databases doesn't exist");
}
Map<String, DatabaseReaderLazyLoader> databaseReaders = new HashMap<>();
try (Stream<Path> databaseFiles = Files.list(geoIpConfigDirectory)) {
PathMatcher pathMatcher = geoIpConfigDirectory.getFileSystem().getPathMatcher("glob:**.mmdb.gz");
// Use iterator instead of forEach otherwise IOException needs to be caught twice...
Iterator<Path> iterator = databaseFiles.iterator();
while (iterator.hasNext()) {
Path databasePath = iterator.next();
if (Files.isRegularFile(databasePath) && pathMatcher.matches(databasePath)) {
String databaseFileName = databasePath.getFileName().toString();
DatabaseReaderLazyLoader holder = new DatabaseReaderLazyLoader(databaseFileName, () -> {
try (InputStream inputStream = new GZIPInputStream(Files.newInputStream(databasePath, StandardOpenOption.READ))) {
return new DatabaseReader.Builder(inputStream).withCache(cache).build();
}
});
databaseReaders.put(databaseFileName, holder);
}
}
}
return Collections.unmodifiableMap(databaseReaders);
}
示例2: testCity
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public void testCity() throws Exception {
InputStream database = getDatabaseFileInputStream("/GeoLite2-City.mmdb.gz");
GeoIpProcessor processor = new GeoIpProcessor(randomAsciiOfLength(10), "source_field",
new DatabaseReader.Builder(database).build(), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false);
Map<String, Object> document = new HashMap<>();
document.put("source_field", "8.8.8.8");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
processor.execute(ingestDocument);
assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo("8.8.8.8"));
@SuppressWarnings("unchecked")
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
assertThat(geoData.size(), equalTo(8));
assertThat(geoData.get("ip"), equalTo("8.8.8.8"));
assertThat(geoData.get("country_iso_code"), equalTo("US"));
assertThat(geoData.get("country_name"), equalTo("United States"));
assertThat(geoData.get("continent_name"), equalTo("North America"));
assertThat(geoData.get("region_name"), equalTo("California"));
assertThat(geoData.get("city_name"), equalTo("Mountain View"));
assertThat(geoData.get("timezone"), equalTo("America/Los_Angeles"));
Map<String, Object> location = new HashMap<>();
location.put("lat", 37.386d);
location.put("lon", -122.0838d);
assertThat(geoData.get("location"), equalTo(location));
}
示例3: testCityWithMissingLocation
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public void testCityWithMissingLocation() throws Exception {
InputStream database = getDatabaseFileInputStream("/GeoLite2-City.mmdb.gz");
GeoIpProcessor processor = new GeoIpProcessor(randomAsciiOfLength(10), "source_field",
new DatabaseReader.Builder(database).build(), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false);
Map<String, Object> document = new HashMap<>();
document.put("source_field", "93.114.45.13");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
processor.execute(ingestDocument);
assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo("93.114.45.13"));
@SuppressWarnings("unchecked")
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
assertThat(geoData.size(), equalTo(1));
assertThat(geoData.get("ip"), equalTo("93.114.45.13"));
}
示例4: testCountry
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public void testCountry() throws Exception {
InputStream database = getDatabaseFileInputStream("/GeoLite2-Country.mmdb.gz");
GeoIpProcessor processor = new GeoIpProcessor(randomAsciiOfLength(10), "source_field",
new DatabaseReader.Builder(database).build(), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false);
Map<String, Object> document = new HashMap<>();
document.put("source_field", "82.170.213.79");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
processor.execute(ingestDocument);
assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo("82.170.213.79"));
@SuppressWarnings("unchecked")
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
assertThat(geoData.size(), equalTo(4));
assertThat(geoData.get("ip"), equalTo("82.170.213.79"));
assertThat(geoData.get("country_iso_code"), equalTo("NL"));
assertThat(geoData.get("country_name"), equalTo("Netherlands"));
assertThat(geoData.get("continent_name"), equalTo("Europe"));
}
示例5: testCountryWithMissingLocation
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public void testCountryWithMissingLocation() throws Exception {
InputStream database = getDatabaseFileInputStream("/GeoLite2-Country.mmdb.gz");
GeoIpProcessor processor = new GeoIpProcessor(randomAsciiOfLength(10), "source_field",
new DatabaseReader.Builder(database).build(), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false);
Map<String, Object> document = new HashMap<>();
document.put("source_field", "93.114.45.13");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
processor.execute(ingestDocument);
assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo("93.114.45.13"));
@SuppressWarnings("unchecked")
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
assertThat(geoData.size(), equalTo(1));
assertThat(geoData.get("ip"), equalTo("93.114.45.13"));
}
示例6: testInvalid
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
/** Don't silently do DNS lookups or anything trappy on bogus data */
public void testInvalid() throws Exception {
InputStream database = getDatabaseFileInputStream("/GeoLite2-City.mmdb.gz");
GeoIpProcessor processor = new GeoIpProcessor(randomAsciiOfLength(10), "source_field",
new DatabaseReader.Builder(database).build(), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false);
Map<String, Object> document = new HashMap<>();
document.put("source_field", "www.google.com");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
try {
processor.execute(ingestDocument);
fail("did not get expected exception");
} catch (IllegalArgumentException expected) {
assertNotNull(expected.getMessage());
assertThat(expected.getMessage(), containsString("not an IP string literal"));
}
}
示例7: setConf
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
@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);
}
}
示例8: PerformanceLineAnalyzer
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
/**
*
*/
public PerformanceLineAnalyzer(LogParserConfiguration logParserConfiguration) throws IOException {
super(new File(logParserConfiguration.getOutputDirectory(), LINE_ANALYZER_KEY + "-details"),
new File(logParserConfiguration.getOutputDirectory(), LINE_ANALYZER_KEY + "-summary"),
logParserConfiguration.getLogEntryWriterFactories());
if (logParserConfiguration.getPatternList() != null && logParserConfiguration.getPatternList().size() > 0) {
linePattern = Pattern.compile((String)logParserConfiguration.getPatternList().get(0));
} else {
linePattern = Pattern.compile(logParserConfiguration.getPerfMatchingPattern());
}
dateFormat = new SimpleDateFormat(logParserConfiguration.getDateFormatString());
contextMapping = logParserConfiguration.getContextMapping();
servletMapping = logParserConfiguration.getServletMapping();
InputStream maxMindDBStream = this.getClass().getResourceAsStream("/maxmind-db/GeoLite2-City.mmdb");
if (maxMindDBStream != null) {
databaseReader = new DatabaseReader.Builder(maxMindDBStream).withCache(new CHMCache()).build();
}
}
示例9: create
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public static GeoIP create(String source, Optional<String> target, Optional<File> path, Optional<List<String>> fields) {
String theTarget = target.isPresent() ? target.get() : DEFAULT_TARGET;
List<String> theFields = fields.isPresent() ? fields.get() : DEFAULT_FIELDS;
String key = key(source, theTarget, path, theFields);
if (CACHE.containsKey(key)) {
return CACHE.get(key);
}
DatabaseReader theReader = path.isPresent() ? fromFile(path.get()) : fromClasspath();
GeoIP geoIP = new GeoIP(source, theTarget, theReader, theFields);
CACHE.put(key, geoIP);
return geoIP;
}
示例10: GeoIpResolverEngine
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public GeoIpResolverEngine(GeoIpResolverConfig config, MetricRegistry metricRegistry) {
this.resolveTime = metricRegistry.timer(name(GeoIpResolverEngine.class, "resolveTime"));
try {
final File database = new File(config.dbPath());
if (Files.exists(database.toPath())) {
this.databaseReader = new DatabaseReader.Builder(database).build();
this.enabled = config.enabled();
} else {
LOG.warn("GeoIP database file does not exist: {}", config.dbPath());
this.enabled = false;
}
} catch (IOException e) {
LOG.error("Could not open GeoIP database {}", config.dbPath(), e);
this.enabled = false;
}
}
示例11: getIPLocation
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
/**
* Returns a {@link Location} object with location information which may
* not have very strictly accurate information.
*
* @param ipStr the IP Address for which a {@link Location} will be obtained.
* @return
* @throws Exception
*/
public static Location getIPLocation(String ipStr) throws Exception {
System.out.println("gres = " + Locator.class.getClassLoader().getResource("GeoLite2-City.mmdb"));
InputStream is = Locator.class.getClassLoader().getResourceAsStream("GeoLite2-City.mmdb");
DatabaseReader reader = new DatabaseReader.Builder(is).build();
CityResponse response = reader.city(InetAddress.getByName(ipStr));
System.out.println("City " +response.getCity());
System.out.println("ZIP Code " +response.getPostal().getCode());
System.out.println("Country " +response.getCountry());
System.out.println("Location " +response.getLocation());
return new Location(response.getCity().toString(), response.getPostal().getCode(),
response.getCountry().toString(), response.getLocation().getTimeZone(), response.getLocation().getLatitude(),
response.getLocation().getLongitude(), response.getPostal().getConfidence(),
response.getLocation().getAccuracyRadius(), response.getLocation().getPopulationDensity(),
response.getLocation().getAverageIncome());
}
示例12: getReader
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
protected synchronized DatabaseReader getReader() throws IOException
{
synchronized (READER_LOCK)
{
if ( reader != null ) {
return reader;
}
final InputStream stream = MaxMindGeoLocator.class.getResourceAsStream( classpath );
if ( stream == null ) {
throw new IOException("Failed to open classpath resource "+classpath);
}
reader = new DatabaseReader.Builder(stream).build();
return reader;
}
}
示例13: main
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public static void main(final String... args) throws Exception {
final ConcurrentMap<String, Boolean> locations = locations();
scheduledExecutorService.scheduleAtFixedRate(new ShutdownWatcher(locations),
15L,
15L,
SECONDS);
final DatabaseReader databaseReader = databaseReader();
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
for (int k = 0; k < 256; k++) {
final InetAddress address = InetAddress.getByName(format("%d.%d.%d.0", i, j, k));
if (executorService.isShutdown() || executorService.isTerminated()) {
return;
}
executorService.execute(new AddressChecker(databaseReader, address, locations));
}
}
}
}
示例14: MaxmindDatabaseGeoLocationService
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
public MaxmindDatabaseGeoLocationService(final MaxmindProperties properties) {
try {
if (properties.getCityDatabase().exists()) {
this.cityDatabaseReader = new DatabaseReader.Builder(properties.getCityDatabase().getFile())
.withCache(new CHMCache()).build();
}
if (properties.getCountryDatabase().exists()) {
this.countryDatabaseReader = new DatabaseReader.Builder(properties.getCountryDatabase().getFile())
.withCache(new CHMCache()).build();
}
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
示例15: GeoIpProcessor
import com.maxmind.geoip2.DatabaseReader; //导入依赖的package包/类
GeoIpProcessor(String tag, String field, DatabaseReader dbReader, String targetField, Set<Property> properties,
boolean ignoreMissing) throws IOException {
super(tag);
this.field = field;
this.targetField = targetField;
this.dbReader = dbReader;
this.properties = properties;
this.ignoreMissing = ignoreMissing;
}