MMDB ip address library operation

Posted by Orio on Sat, 08 Jun 2019 01:30:39 +0200

MMDB ip address library operation

@(JAVA)[java]

MMDB, Maxmind DB, is a database designed to store data information of IPv4 and IPv6. The MMDB file is a binary format file, which uses a binary lookup tree to speed up the query of IP information.

1. Data Format Description

{
    continent => {                    //Continent
        code        => string,        //Continental codes, such as AS, may be empty
        names       => {              //Continental Name
            en      => string,        //Continental English names, such as Asia, may be empty
            zh_CN   => string,        //Chinese names for continents, such as Asia, may be empty
        },
    },
    country   => {                    //Country
        iso_code    => string,        //National iso codes, such as CN, may be empty
        names       => {              //Name of country
            en      => string,        //National English names, such as China, may be empty
            zh_CN   => string,        //The Chinese name of a country, such as China, may be empty.
        },
    },
    subdivisions => {                 //Province
        iso_code    => string,        //Provide ISO code Provide ISO code, such as 44, may be empty
        names       => {              //Name of province
            en      => string,        //Provincial English names, such as Guangdong, may be empty
            zh_CN   => string,        //Chinese names of provinces, such as Guangdong, may be empty
        },
    },
    city      => {                    //City
        id          => int,           //City id, such as 440000, may be empty
        names       => {              //City Name
            en      => string,        //The English name of a city, such as Guangzhou, may be empty.
            zh_CN   => string,        //Chinese names of cities, such as Guangzhou, may be empty
        },
    },
    location  => {                    //geographical position
        latitude    => double,        //Latitude, possibly empty
        longitude   => double,        //Longitude, possibly empty
        time_zone   => string,        //Time zone, possibly empty
    },
    isp       => {                    //Operators
        id          => int,           //Operator id, may be empty
        names       => {              //Operator Name
            zh_CN   => string,        //Operator Chinese names, such as telecommunications, may be empty
    },
}

Among them:
* The national ISO code can refer to Wikipedia ISO_3166-2.
* Provincial ISO codes and domestic provincial ISO codes are the first two of the provincial citizenship card numbers. Foreign provincial ISO codes may exist in English, not all of them are digital. Specifically, you can refer to Wikipedia ISO_3166-2.
* City id, only Chinese IP has city ID (including Hong Kong, Macao and Taiwan), which is the top six of the city's citizen ID number. Specific reference can be made to the website of the National Bureau of Statistics.
* Operator id, only IP in mainland China has operator field. The existing ID rules are as follows: Telecom 10000, Unicom 10010, Mobile 10086, Tietong 10050, Education Network 985211, Dr. Peng 600804, Huanu 96171, Founder Network 9990, Gehua Network 96196, Aliyun 1688, and other (including unknown) 1. Operator ID of foreign IP is empty.
* Operator name, Chinese, such as telecommunications, may be empty. Only IP in mainland China may have operator field. IP in mainland China may also have unknown operator. At this time, operator field is also empty.
* Parsing mmdb data in a programming language may return array, dictionary, or hash table data, depending on the programming language.

2. java reads mmdb files

You can use the maxmind-db library. Add the following to pom.xml:

    <dependency>
        <groupId>com.maxmind.db</groupId>
        <artifactId>maxmind-db</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.0</version>
    </dependency>

The code is as follows:

public class IpDemo {
    public static void main(String[] args) throws Exception {
        File database = new File("resources/ip.mmdb");
        Reader reader = new Reader(database);
        InetAddress address = InetAddress.getByName("24.24.24.24");
        JsonNode response = reader.get(address);
        System.out.println(response);
        reader.close();
    }
}

Where ip.mmdb is placed in the resources directory, which is src folder. The output is as follows:

{"location":{"time_zone":"America/New_York","longitude":-76.1474,"latitude":43.0481},"isp":{"id":0,"names":{"zh_CN":""}},"continent":{"names":{"zh_CN":"North America","en":"North America"},"code":"NA"},"subdivisions":{"names":{"zh_CN":"New York State","en":"New York"},"iso_code":"NY"},"country":{"names":{"zh_CN":"U.S.A","en":"United States"},"iso_code":"US"},"city":{"id":0,"names":{"zh_CN":"Syracuse","en":"Syracuse"}}}

Topics: Java Database network Programming