I have downloaded ip-to-country.csv that has ip ranges that are mapped to countries. How should I store this data to database and how can I query in what range Ip address is to know where Ip address is coming from?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can build index only for ipTo (high boundary) values of ranges and use query:
(it's assumed that ranges do not overlap as in MaxMind database)
I wrote a small lib called ip2c to do just that. it uses the database from webhosting.info but also supports that from Software77.
It converts the CSV info a compact binary format and can do the search straight on the file, in memory or in a memory mapped file.
The Java API usage is similar to this:
For IPv4 you can store in the following format:
When you need to look up the IP address execute the following query
This will give you the country for the IP address
Have a look to the IP-to-Country Handbook
The ip-to-country.csv file contains five fields:
You can import this data into any database by creating a table with the following fields:
You can query the above table, after you have imported the data into it, to find the country of a corresponding IP Number by issuing the following Select statement:
where IP Number of a given A.B.C.D IP, is calculated by :