Ip2region is an offline IP-to-Region localization library and IP data management framework with both IPv4 and IPv6 supports, 10-microsecond level query efficiency, xdb search client for many programming languages
Go to file
lei.lei facd3ee689 The code computes the average of two integers using either division or signed right shift, and then uses the result as the index of an array. If the values being averaged are very large, this can overflow (resulting in the computation of a negative average). 2018-06-20 14:14:28 +08:00
binding The code computes the average of two integers using either division or signed right shift, and then uses the result as the index of an array. If the values being averaged are very large, this can overflow (resulting in the computation of a negative average). 2018-06-20 14:14:28 +08:00
data Update the data to version 2018/01/12 2018-01-16 15:08:30 +08:00
.gitignore Untrack the binding/java/target/ directory 2017-10-28 15:38:09 +08:00
CHANGES.md Update the 1.2.4 version desc 2017-04-07 19:04:12 +08:00
LICENSE.md stdlize the license add free statement 2016-12-28 09:53:13 +08:00
README.md Fix the doc error 2017-11-23 21:25:32 +08:00
dbMaker-1.2.2.jar java binding maven upload & track the latest dbMaker.jar 2017-10-23 13:48:49 +08:00

README.md

ip2region - 最自由的ip地址查询库ip到地区的映射库提供Binary,B树和纯内存三种查询算法妈妈再也不用担心我的ip地址定位。

1. 99.9%准确率,定时更新:

数据聚合了一些知名ip到地名查询提供商的数据这些是他们官方的的准确率经测试着实比纯真啥的准确多了。
每次聚合一下数据需要1-2天会不定时更新。

2. 标准化的数据格式:

每条ip数据段都固定了格式城市Id|国家|区域|省份|城市|ISP

只有中国的数据精确到了城市其他国家只能定位到国家后前的选项全部是0已经包含了全部你能查到的大大小小的国家。 请忽略前面的城市Id个人项目需求

3. 体积小:

生成的数据库文件ip2region.db只有1.5M1.2版本前是3.5M

4. 多查询客户端的支持0.0x毫秒级别的查询

已经集成的客户端有java, php, c, pythonnodejsphp扩展(支持linux, php5, php7版本已支持)golang。

提供了两种查询算法,响应时间如下:
客户端/binary算法/b-tree算法/Memory算法
java/0.x毫秒/0.x毫秒/0.1x毫秒 (使用RandomAccessFile)
php/0.x毫秒/0.1x毫秒/0.1x毫秒
c/0.0x毫秒/0.0x毫秒/0.00x毫秒(b-tree算法基本稳定在0.02x毫秒级别)
python/0.x毫秒/0.1x毫秒/未知

任何客户端b-tree都比binary算法快当然Memory算法固然是最快的

maven仓库地址

<dependency>
    <groupId>org.lionsoul</groupId>
    <artifactId>ip2region</artifactId>
    <version>1.4</version>
</dependency>

5. 测试程序:

java:

cd binding/java
ant all
java -jar ip2region-{version}.jar ./data/ip2region.db

php:

php binding/php/testSearcher.php ./data/ip2region.db

c:

cd binding/c/
gcc -g -O2 testSearcher.c ip2region.c
./a.out ../../data/ip2region.db

python:

python binding/python/testSearcher.py ./data/ip2region.db

均会看到如下界面:

initializing  B-tree ... 
+----------------------------------+
| ip2region test script            |
| Author: chenxin619315@gmail.com  |
| Type 'quit' to exit program      |
+----------------------------------+
p2region>> 101.105.35.57
2163|中国|华南|广东省|深圳市|鹏博士 in 0.02295 millseconds

输入ip地址开始测试第一次会稍微有点慢在运行命令后面接入binary,memory来尝试其他算法建议使用b-tree算法速度和并发需求的可以使用memory算法。

具体集成请参考不同客户端的测试源码。

6. 如何生成ip2region.db文件

从ip2region 1.2.2版本开始里面提交了一个dbMaker-{version}.jar的可以执行jar文件用它来完成这个工作

  • 1, 确保你安装好了java环境不玩Java的童鞋就自己谷歌找找拉临时用一用几分钟的事情
  • 2, cd到ip2region的根目录然后运行如下命令
java -jar dbMaker-{version}.jar -src 文本数据文件 -region 地域csv文件 [-dst 生成的ip2region.db文件的目录]

# 文本数据文件db文件的原始文本数据文件路径自带的ip2region.db文件就是/data/ip.merge.txt生成而来的你可以换成自己的或者更改/data/ip.merge.txt重新生成
# 地域csv文件该文件目的是方便配置ip2region进行数据关系的存储得到的数据包含一个city_id这个直接使用/data/origin/global_region.csv文件即可
# ip2region.db文件的目录是可选参数没有指定的话会在当前目录生成一份./data/ip2region.db文件
  • 3, 获取生成的ip2region.db文件覆盖原来的ip2region.db文件即可
  • 4, 默认的ip2region.db文件生成命令:
cd ip2region项目根目录
java -jar dbMaker-1.2.2.jar -src ./data/ip.merge.txt -region ./data/global_region.csv

# 会看到一大片的输出