- All Implemented Interfaces:
Closeable, AutoCloseable
The class DatabaseReader provides a reader for the GeoIP2 database format.
Usage
To use the database API, you must create a new DatabaseReader using the DatabaseReader.Builder. You must provide the Builder constructor either an InputStream or File for your GeoIP2 database. You may also specify the fileMode and the locales fallback order using the methods on the Builder object. After you have created the DatabaseReader, you may then call the appropriate method (e.g., city) for your database, passing it the IP address you want to look up.
If the lookup succeeds, the method call will return a response class for the GeoIP2 lookup. The class in turn contains multiple record classes, each of which represents part of the data returned by the database.
We recommend reusing the DatabaseReader object rather than creating a new one for each lookup. The creation of this object is relatively expensive as it must read in metadata for the file. It is safe to share the object across threads.
Caching
The database API supports pluggable caching (by default, no caching is performed). A simple implementation is provided by com.maxmind.db.CHMCache. Using this cache, lookup performance is significantly improved at the cost of a small (~2MB) memory overhead.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classConstructs a Builder for theDatabaseReader.Field Summary
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateDatabaseReader(DatabaseReader.Builder builder) Opens the database the builder describes.Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the database.Looks up the two-letter ISO country code recorded for an address.voidcountryToIP(String country, Writer out) I2P - Write all IPv4 address ranges for the given country to out.private Objectget(InetAddress ipAddress, String type) Returns a map containing: continent: Map containing: code: String names: Map of lang to translated name geoname_id: Long country: Map containing: iso_code: String names: Map of lang to translated name geoname_id: Long registered_country: Map containing: iso_code: String names: Map of lang to translated name geoname_id: LongGet the metadata recorded in the database, including its build epoch and the node count of the search tree.
Field Details
reader
The open database file.locales
Constructor Details
DatabaseReader
Opens the database the builder describes.- Parameters:
builder- the builder describing where the database is- Throws:
IOException- if the database cannot be read
Method Details
get
Returns a map containing:- continent: Map containing:
- code: String
- names: Map of lang to translated name
- geoname_id: Long
- country: Map containing:
- iso_code: String
- names: Map of lang to translated name
- geoname_id: Long
- registered_country: Map containing:
- iso_code: String
- names: Map of lang to translated name
- geoname_id: Long
- Parameters:
ipAddress- IPv4 or IPv6 address to lookup.type- the database type required, e.g. city or country- Returns:
- A Map with the data for the IP address
- Throws:
IOException- if there is an error opening or reading from the file.
- continent: Map containing:
close
Closes the database.
If you are using
FileMode.MEMORY_MAPPED, this will not unmap the underlying file due to a limitation in Java'sMappedByteBuffer. It will however set the reference to the buffer tonull, allowing the garbage collector to collect it.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs.
country
Looks up the two-letter ISO country code recorded for an address.- Parameters:
ipAddress- IPv4 or IPv6 address- Returns:
- the two-letter ISO country code, or null if the address has no country record
- Throws:
IOException- if the address cannot be resolved or the database cannot be read
countryToIP
I2P - Write all IPv4 address ranges for the given country to out.- Parameters:
country- ISO 3166-1 alpha-2 code, matched case-insensitively here and upper-cased before the lookupout- caller must close- Throws:
IOException- if the database cannot be read or the write to out fails
getMetadata
Get the metadata recorded in the database, including its build epoch and the node count of the search tree.- Returns:
- the metadata for the open MaxMind DB file.