• 准确率99.9%的离线IP地址定位库


    Ip2region是什么?

    ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。

    Ip2region特性

    99.9%准确率

    数据聚合了一些知名ip到地名查询提供商的数据,这些是他们官方的的准确率,经测试着实比经典的纯真IP定位准确一些。
    ip2region的数据聚合自以下服务商的开放API或者数据(升级程序每秒请求次数2到4次):
    01, >80%, 淘宝IP地址库, http://ip.taobao.com/
    02, ≈10%, GeoIP, https://geoip.com/
    03, ≈2%, 纯真IP库, http://www.cz88.net/
    备注:如果上述开放API或者数据都不给开放数据时ip2region将停止数据的更新服务。

    标准化的数据格式

    每条ip数据段都固定了格式:

    _城市Id|国家|区域|省份|城市|ISP_
    

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

    体积小

    包含了全部的IP,生成的数据库文件ip2region.db只有几MB,最小的版本只有1.5MB,随着数据的详细度增加数据库的大小也慢慢增大,目前还没超过8MB。

    查询速度快

    全部的查询客户端单次查询都在0.x毫秒级别,内置了三种查询算法

    1. memory算法:整个数据库全部载入内存,单次查询都在0.1x毫秒内,C语言的客户端单次查询在0.00x毫秒级别。
    2. binary算法:基于二分查找,基于ip2region.db文件,不需要载入内存,单次查询在0.x毫秒级别。
    3. b-tree算法:基于btree算法,基于ip2region.db文件,不需要载入内存,单词查询在0.x毫秒级别,比binary算法更快。

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

    多查询客户端的支持

    已经集成的客户端有:java、C#、php、c、python、nodejs、php扩展(php5和php7)、golang、rust、lua、lua_c, nginx。

    ip2region快速测试

    maven仓库地址
      <dependency>
         <groupId>org.lionsoul</groupId>
         <artifactId>ip2region</artifactId>
         <version>1.7.2</version>
      </dependency>
    
    Java实现
    public static String getCityInfo(String ip){
            //db
            String dbPath = "src/main/resources/ip2region.db";
            File file = new File(dbPath);
            if ( file.exists() == false ) {
                System.out.println("Error: Invalid ip2region.db file");
                return null;
            }
            //查询算法
            int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
            //DbSearcher.BINARY_ALGORITHM //Binary
            //DbSearcher.MEMORY_ALGORITYM //Memory
            try {
                DbConfig config = new DbConfig();
                DbSearcher searcher = new DbSearcher(config, dbPath);
                //define the method
                Method method = null;
                switch ( algorithm )
                {
                    case DbSearcher.BTREE_ALGORITHM:
                        method = searcher.getClass().getMethod("btreeSearch", String.class);
                        break;
                    case DbSearcher.BINARY_ALGORITHM:
                        method = searcher.getClass().getMethod("binarySearch", String.class);
                        break;
                    case DbSearcher.MEMORY_ALGORITYM:
                        method = searcher.getClass().getMethod("memorySearch", String.class);
                        break;
                }
                DataBlock dataBlock = null;
                if ( Util.isIpAddress(ip) == false ) {
                    System.out.println("Error: Invalid ip address");
                    return null;
                }
                dataBlock  = (DataBlock) method.invoke(searcher, ip);
                return dataBlock.getRegion();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    创建测试类
        @Test
        public void Ip2region(){
            String cityInfo = IpUtils.getCityInfo("182.50.124.211");
            System.out.println(cityInfo);
        }
    
    运行结果

    关注我的技术公众号,每天都有优质技术文章推送。
    微信扫一扫下方二维码即可关注:
    在这里插入图片描述

    尚未佩妥剑,转眼便江湖,愿历尽千帆,归来仍少年 
  • 相关阅读:
    对模拟器虚假设备识别能力提升15%!每日清理大师App集成系统完整性检测
    教你实现华为快应用深色主题适配
    如何实现一个malloc
    如何提高团队管理能力?
    大型网站架构演变和知识体系
    call_user_func 和 call_user_func_array用法
    源码研究:php变量
    http协议简介
    用c语言实现http请求
    用socket写一个简单的客户端和服务端程序
  • 原文地址:https://www.cnblogs.com/James-1024/p/13515838.html
Copyright © 2020-2023  润新知