• Stars
    star
    109
  • Rank 308,740 (Top 7 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated 26 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Java reader for the MaxMind DB format

MaxMind DB Reader

Description

This is the Java API for reading MaxMind DB files. MaxMind DB is a binary file format that stores data indexed by IP address subnets (IPv4 or IPv6).

Installation

Maven

We recommend installing this package with Maven. To do this, add the dependency to your pom.xml:

    <dependency>
        <groupId>com.maxmind.db</groupId>
        <artifactId>maxmind-db</artifactId>
        <version>3.0.0</version>
    </dependency>

Gradle

Add the following to your build.gradle file:

repositories {
    mavenCentral()
}
dependencies {
    compile 'com.maxmind.db:maxmind-db:3.0.0'
}

Usage

Note: For accessing MaxMind GeoIP2 databases, we generally recommend using the GeoIP2 Java API rather than using this package directly.

To use the API, you must first create a Reader object. The constructor for the reader object takes a File representing your MaxMind DB. Optionally you may pass a second parameter with a FileMode with a value of MEMORY_MAP or MEMORY. The default mode is MEMORY_MAP, which maps the file to virtual memory. This often provides performance comparable to loading the file into real memory with MEMORY.

To look up an IP address, pass the address as an InetAddress to the get method on Reader, along with the class of the object you want to deserialize into. This method will create an instance of the class and populate it. See examples below.

We recommend reusing the Reader 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.

Example

import com.maxmind.db.MaxMindDbConstructor;
import com.maxmind.db.MaxMindDbParameter;
import com.maxmind.db.Reader;
import com.maxmind.db.DatabaseRecord;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

public class Lookup {
    public static void main(String[] args) throws IOException {
        File database = new File("/path/to/database/GeoIP2-City.mmdb");
        try (Reader reader = new Reader(database)) {
            InetAddress address = InetAddress.getByName("24.24.24.24");

            // get() returns just the data for the associated record
            LookupResult result = reader.get(address, LookupResult.class);

            System.out.println(result.getCountry().getIsoCode());

            // getRecord() returns a DatabaseRecord class that contains both
            // the data for the record and associated metadata.
            DatabaseRecord<LookupResult> record
                = reader.getRecord(address, LookupResult.class);

            System.out.println(record.getData().getCountry().getIsoCode());
            System.out.println(record.getNetwork());
        }
    }

    public static class LookupResult {
        private final Country country;

        @MaxMindDbConstructor
        public LookupResult (
            @MaxMindDbParameter(name="country") Country country
        ) {
            this.country = country;
        }

        public Country getCountry() {
            return this.country;
        }
    }

    public static class Country {
        private final String isoCode;

        @MaxMindDbConstructor
        public Country (
            @MaxMindDbParameter(name="iso_code") String isoCode
        ) {
            this.isoCode = isoCode;
        }

        public String getIsoCode() {
            return this.isoCode;
        }
    }
}

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.

Usage:

Reader reader = new Reader(database, new CHMCache());

Please note that the cache will hold references to the objects created during the lookup. If you mutate the objects, the mutated objects will be returned from the cache on subsequent lookups.

Multi-Threaded Use

This API fully supports use in multi-threaded applications. In such applications, we suggest creating one Reader object and sharing that among threads.

Common Problems

File Lock on Windows

By default, this API uses the MEMORY_MAP mode, which memory maps the file. On Windows, this may create an exclusive lock on the file that prevents it from being renamed or deleted. Due to the implementation of memory mapping in Java, this lock will not be released when the DatabaseReader is closed; it will only be released when the object and the MappedByteBuffer it uses are garbage collected. Older JVM versions may also not release the lock on exit.

To work around this problem, use the MEMORY mode or try upgrading your JVM version. You may also call System.gc() after dereferencing the DatabaseReader object to encourage the JVM to garbage collect sooner.

Packaging Database in a JAR

If you are packaging the database file as a resource in a JAR file using Maven, you must disable binary file filtering. Failure to do so will result in InvalidDatabaseException exceptions being thrown when querying the database.

Format

The MaxMind DB format is an open format for quickly mapping IP addresses to records. The specification is available, as is our Perl writer for the format.

Bug Tracker

Please report all issues with this code using the GitHub issue tracker.

If you are having an issue with a MaxMind database or service that is not specific to this reader, please contact MaxMind support.

Requirements

This API requires Java 11 or greater.

Contributing

Patches and pull requests are encouraged. Please include unit tests whenever possible.

Versioning

The MaxMind DB Reader API uses Semantic Versioning.

Copyright and License

This software is Copyright (c) 2014-2022 by MaxMind, Inc.

This is free software, licensed under the Apache License, Version 2.0.

More Repositories

1

GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
PHP
2,279
star
2

GeoIP2-python

Python code for GeoIP2 webservice client and database reader
Python
1,067
star
3

libmaxminddb

C library for the MaxMind DB file format
C
875
star
4

GeoIP2-java

Java API for GeoIP2 webservice client and database reader
Java
748
star
5

geoipupdate

GeoIP update client code
Go
669
star
6

MaxMind-DB-Reader-php

PHP Reader for the MaxMind DB Database Format
PHP
632
star
7

geoip-api-php

DEPRECATED GeoIP Legacy PHP API
PHP
523
star
8

geoip-api-c

DEPRECATED GeoIP Legacy C API
C
369
star
9

GeoIP2-dotnet

MaxMind GeoIP2 .NET API
C#
331
star
10

web-service-common-php

Shared code for the MaxMind Web Service PHP client APIs
PHP
283
star
11

MaxMind-DB

Spec and test data for the MaxMind DB file format
Go
266
star
12

geoipupdate-legacy

GeoIP update client code
C
258
star
13

geoip-api-python

DEPRECATED GeoIP Legacy Python API
C
233
star
14

GeoIP2-node

Node.js API for GeoIP2 webservice client and database reader
TypeScript
205
star
15

geoip2-csv-converter

GeoIP2 CSV Format Converter
Go
198
star
16

geoip-api-java

DEPRECATED GeoIP Legacy Java API
Java
176
star
17

MaxMind-DB-Reader-python

Python MaxMind DB reader extension
Python
173
star
18

mod_maxminddb

MaxMind DB Apache Module
C
123
star
19

mmdbinspect

look up records for one or more IPs/networks in one or more .mmdb databases
Go
114
star
20

mmdbwriter

Go library for writing MaxMind DB (mmdb) files
Go
100
star
21

MaxMind-DB-Reader-dotnet

.NET Reader for the MaxMind DB Database Format
C#
98
star
22

MaxMind-DB-Writer-perl

Create MaxMind DB database files
Perl
74
star
23

GeoIP2-ruby

Ruby API for GeoIP2 webservice client and database reader
Ruby
55
star
24

minfraud-api-php

PHP API for minFraud Score, Insights, and Factors
PHP
49
star
25

geoip-api-mod_geoip2

DEPRECATED GeoIP Legacy module for Apache 2
C
48
star
26

geoip-api-csharp2

DEPRECATED GeoIP Legacy C# API
C#
47
star
27

MaxMind-DB-Reader-ruby

Ruby reader for the MaxMind DB Database Format
Ruby
45
star
28

getting-started-with-mmdb

A quick guide to writing and reading from your own MMDB databases.
Perl
37
star
29

minfraud-api-python

Python API for minFraud Score, Insights, and Factors
Python
27
star
30

mmdb-from-go-blogpost

Enriching MMDB files with your own data using Go.
Go
23
star
31

ccfd-api-php

Deprecated minFraud Legacy PHP API
PHP
23
star
32

minfraud-api-dotnet

.NET API for MaxMind minFraud Score, Insights, and Factors
C#
19
star
33

GeoIP2-perl

Perl API for MaxMind's GeoIP2 web services and databases
Perl
18
star
34

minfraud-api-java

Java API for minFraud Score, Insights, and Factors
Java
18
star
35

minfraud-api-ruby

Ruby API for minFraud Score, Insights, and Factors
Ruby
14
star
36

mm-geofeed-verifier

Verify the format of a geofeed file, and make some comparisons to data in an MMDB file.
Go
14
star
37

dev-hire-homework

A homework exercise for engineering applicants
Perl
13
star
38

minfraud-api-node

Node.js API for MaxMind minFraud Score, Insights, and Factors
TypeScript
13
star
39

mm-network-analyzer

A program to aid in diagnosing networking issues
Go
12
star
40

MaxMind-DB-Reader-perl

Read MaxMind DB files and look up IP addresses
Perl
12
star
41

mmdbverify

Verifier for the MaxMind DB format
Go
10
star
42

geoip-api-perl

DEPRECATED GeoIP Legacy Perl API
Perl
10
star
43

Stepford

A vaguely Rake/Make/Cake-like thing for Perl - create steps and let a runner run them
Perl
9
star
44

Locale-Country-Multilingual

mapping ISO codes to localized country names
Perl
7
star
45

ccfd-api-java

Deprecated minFraud Legacy Java API
Java
7
star
46

Database-Migrator

Mirror of Database-Migrator on urth.org
Perl
5
star
47

Net-Works

Sane APIs for IP addresses and networks
Perl
5
star
48

MaxMind-DB-Reader-XS

Fast XS implementation of MaxMind DB reader
Perl
5
star
49

ccfd-api-asp

minFraud ASP API
ASP
3
star
50

webservice-paypal-paymentsadvanced

A simple wrapper around the PayPal Payments Advanced web service
Perl
3
star
51

dev-site

Static site generator for https://dev.maxmind.com.
MDX
3
star
52

xgb2code

A converter for xgboost model dumps to code.
Go
3
star
53

geoip-api-mscom

DEPRECATED GeoIP Legacy MS COM API
C
2
star
54

gatling-gen

C++
2
star
55

geolite2-ws-blogpost

Integrating MaxMind's Free and Paid IP Geolocation Web Services (in PHP)
PHP
2
star
56

App-CISetup

Command line tools to generate and update Travis and AppVeyor configs for Perl libraries
Perl
2
star
57

MaxMind-DB-Common-perl

Code shared by the MaxMind DB reader and writer modules
Perl
2
star
58

minfraud-api-perl

Perl API for minFraud Score, Insights, and Factors
Perl
2
star
59

WebService-PivotalTracker

Perl library for the Pivotal Tracker REST API
Perl
2
star
60

fuzzing-workshop

Code for Summit Fuzzing Workshop
Go
1
star
61

Dist-Zilla-PluginBundle-MAXMIND

Perl
1
star
62

TeamCity-Message

Generate TeamCity build messages
Perl
1
star
63

blog-site

Static site generator for https://blog.maxmind.com.
SCSS
1
star
64

api-specs

TypeScript
1
star
65

TAP-Formatter-TeamCity

Emit test results as TeamCity build messages
Perl
1
star