准確率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);
    }
運行結果

關注我的技術公眾號,每天都有優質技術文章推送。
微信掃一掃下方二維碼即可關注:
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM