Ip2region - 基于xdb离线库的Java IP查询工具提供给脚本调用
文章目录
- Pre
- 效果
- 实现
- git clone
- 编译测试程序
- 将ip2region.xdb放到指定目录
- 使用
- 改进
- 最终效果
Pre
OpenSource - Ip2region 离线IP地址定位库和IP定位数据管理框架
Ip2region - xdb java 查询客户端实现
效果


最终效果

实现
git clone
git clone https://github.com/lionsoul2014/ip2region.git

编译测试程序
cd binding/java/
mvn compile package
然后会在当前目录的 target 目录下得到一个 ip2region-{version}.jar 的打包文件。

将ip2region.xdb放到指定目录

使用


改进
// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
// @Author Lion <chenxin619315@gmail.com>
// @Date 2022/06/23package org.lionsoul.ip2region;import org.lionsoul.ip2region.xdb.Searcher;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;public class SearchTest {public static void printHelp(String[] args) {System.out.print("ip2region xdb searcher\n");System.out.print("java -jar ip2region-{version}.jar [command] [command options]\n");System.out.print("Command: \n");System.out.print(" search search input test\n");System.out.print(" bench search bench test\n");}public static Searcher createSearcher(String dbPath, String cachePolicy) throws IOException {if ("file".equals(cachePolicy)) {return Searcher.newWithFileOnly(dbPath);} else if ("vectorIndex".equals(cachePolicy)) {byte[] vIndex = Searcher.loadVectorIndexFromFile(dbPath);return Searcher.newWithVectorIndex(dbPath, vIndex);} else if ("content".equals(cachePolicy)) {byte[] cBuff = Searcher.loadContentFromFile(dbPath);return Searcher.newWithBuffer(cBuff);} else {throw new IOException("invalid cache policy `" + cachePolicy + "`, options: file/vectorIndex/content");}}public static void searchTest(String[] args) throws IOException {String dbPath = "", cachePolicy = "vectorIndex";for (final String r : args) {if (r.length() < 5) {continue;}if (r.indexOf("--") != 0) {continue;}int sIdx = r.indexOf('=');if (sIdx < 0) {System.out.printf("missing = for args pair `%s`\n", r);return;}String key = r.substring(2, sIdx);String val = r.substring(sIdx + 1);// System.out.printf("key=%s, val=%s\n", key, val);if ("db".equals(key)) {dbPath = val;} else if ("cache-policy".equals(key)) {cachePolicy = val;} else {System.out.printf("undefined option `%s`\n", r);return;}}if (dbPath.length() < 1) {System.out.print("java -jar ip2region-{version}.jar search [command options]\n");System.out.print("options:\n");System.out.print(" --db string ip2region binary xdb file path\n");System.out.print(" --cache-policy string cache policy: file/vectorIndex/content\n");return;}Searcher searcher = createSearcher(dbPath, cachePolicy);Scanner scanner = new Scanner(System.in);String line = scanner.nextLine();try {String region = searcher.search(line.trim());System.out.printf("ip: %s , region: %s\n", line, region);} catch (Exception e) {System.out.printf("{err: %s, ioCount: %d}\n", e, searcher.getIOCount());}searcher.close();}public static void benchTest(String[] args) throws IOException {String dbPath = "", srcPath = "", cachePolicy = "vectorIndex";for (final String r : args) {if (r.length() < 5) {continue;}if (r.indexOf("--") != 0) {continue;}int sIdx = r.indexOf('=');if (sIdx < 0) {System.out.printf("missing = for args pair `%s`\n", r);return;}String key = r.substring(2, sIdx);String val = r.substring(sIdx + 1);if ("db".equals(key)) {dbPath = val;} else if ("src".equals(key)) {srcPath = val;} else if ("cache-policy".equals(key)) {cachePolicy = val;} else {System.out.printf("undefined option `%s`\n", r);return;}}if (dbPath.length() < 1 || srcPath.length() < 1) {System.out.print("java -jar ip2region-{version}.jar bench [command options]\n");System.out.print("options:\n");System.out.print(" --db string ip2region binary xdb file path\n");System.out.print(" --src string source ip text file path\n");System.out.print(" --cache-policy string cache policy: file/vectorIndex/content\n");return;}Searcher searcher = createSearcher(dbPath, cachePolicy);long count = 0, costs = 0, tStart = System.nanoTime();String line;final Charset charset = Charset.forName("utf-8");final FileInputStream fis = new FileInputStream(srcPath);final BufferedReader reader = new BufferedReader(new InputStreamReader(fis, charset));while ((line = reader.readLine()) != null) {String l = line.trim();String[] ps = l.split("\\|", 3);if (ps.length != 3) {System.out.printf("invalid ip segment `%s`\n", l);return;}long sip;try {sip = Searcher.checkIP(ps[0]);} catch (Exception e) {System.out.printf("check start ip `%s`: %s\n", ps[0], e);return;}long eip;try {eip = Searcher.checkIP(ps[1]);} catch (Exception e) {System.out.printf("check end ip `%s`: %s\n", ps[1], e);return;}if (sip > eip) {System.out.printf("start ip(%s) should not be greater than end ip(%s)\n", ps[0], ps[1]);return;}long mip = (sip + eip) >> 1;for (final long ip : new long[]{sip, (sip + mip) >> 1, mip, (mip + eip) >> 1, eip}) {long sTime = System.nanoTime();String region = searcher.search(ip);costs += System.nanoTime() - sTime;// check the region infoif (!ps[2].equals(region)) {System.out.printf("failed search(%s) with (%s != %s)\n", Searcher.long2ip(ip), region, ps[2]);return;}count++;}}reader.close();searcher.close();long took = System.nanoTime() - tStart;System.out.printf("Bench finished, {cachePolicy: %s, total: %d, took: %ds, cost: %d μs/op}\n",cachePolicy, count, TimeUnit.NANOSECONDS.toSeconds(took),count == 0 ? 0 : TimeUnit.NANOSECONDS.toMicros(costs / count));}public static void main(String[] args) {if (args.length < 1) {printHelp(args);return;}if ("search".equals(args[0])) {try {searchTest(args);} catch (IOException e) {System.out.printf("failed running search test: %s\n", e);}} else if ("bench".equals(args[0])) {try {benchTest(args);} catch (IOException e) {System.out.printf("failed running bench test: %s\n", e);}} else {printHelp(args);}}}
重新编译 ,执行
最终效果

这样就可以愉快的在脚本中调用了
当然了,启动java进程的过程,相对还是比较耗时的,这里仅提供一种思路

相关文章:
Ip2region - 基于xdb离线库的Java IP查询工具提供给脚本调用
文章目录 Pre效果实现git clone编译测试程序将ip2region.xdb放到指定目录使用改进最终效果 Pre OpenSource - Ip2region 离线IP地址定位库和IP定位数据管理框架 Ip2region - xdb java 查询客户端实现 效果 最终效果 实现 git clone git clone https://github.com/lionsou…...
研发管理革命:探索顶尖的工时系统选择
国内外主流的10款研发工时管理系统对比:PingCode、Worktile、无鱼项目工时系统、Toggl Track、泽众ALM、Asana、Jira、GitHub、Trello、TrackingTime。 在研发团队中,工时管理常常成为效率瓶颈,尤其是在资源分配和项目进度跟踪方面。选择合适…...
微服务-MybatisPlus下
微服务-MybatisPlus下 文章目录 微服务-MybatisPlus下1 MybatisPlus扩展功能1.1 代码生成1.2 静态工具1.3 逻辑删除1.4 枚举处理器1.5 JSON处理器**1.5.1.定义实体****1.5.2.使用类型处理器** **1.6 配置加密(选学)**1.6.1.生成秘钥**1.6.2.修改配置****…...
【python_将一个列表中的几个字典改成二维列表,并删除不需要的列】
def 将一个列表中的几个字典改成二维列表(original_list,headersToRemove_list):# 初始化一个列表用于存储遇到的键,保持顺序ordered_keys []# 遍历data中的每个字典,添加其键到ordered_keys,如果该键还未被添加for d in original_list:for …...
IDEA的pom.xml显示ignored 的解决办法
问题: idea中创建Maven module时,pom.xml出现ignored。 原因: 相同名称的module在之前被创建删除过,IDEA会误以为新的同名文件是之前删除掉的,将这个新的module的pom.xml文件忽略掉显示ignored. 解决: 在…...
2. 卷积神经网络无法绕开的神——LeNet
卷积神经网络无法绕开的大神——LeNet 1. 基本架构2. LeNet 53. LeNet 5 代码 1. 基本架构 特征抽取模块可学习的分类器模块 2. LeNet 5 LeNet 5: 5 表示的是5个核心层,2个卷积层,3个全连接层.核心权重层:卷积层、全连接层、循环层ÿ…...
【区块链】JavaScript连接web3钱包,实现测试网络中的 Sepolia ETH余额查询、转账功能
审核看清楚了 ! 这是以太坊测试网络!用于学习的测试网络!!! 有关web3 和区块链的内容为什么要给我审核不通过? 别人凭什么可以发! 目标成果: 实现功能分析: 显示账户信…...
关于珞石机器人二次开发SDK的posture函数的算法RX RY RZ纠正 C#
在珞石SDK二次开发的函数钟,获取当前机器人位姿的函数posture函数在输出时会发现数据不正确,与示教器数据不一致。 其中第一个数据正确 第二三各数据为相反 第四五六各数据为弧度制 转换方法为(弧度/PI)*180度 然后发现第四个数据还要加上180度 第五…...
【Three.js基础学习】17.imported-models
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 前言 课程回顾: 如何在three.js 中引入不同的模型? 1. 格式 (不同的格式) https://en.wikipedia.org/wiki/List_of_file_form…...
Spring Bean - xml 配置文件创建对象
类型: 1、值类型 2、null (标签) 3、特殊符号 (< -> < ) 4、CDATA <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/bea…...
uniapp map组件自定义markers标记点
需求是根据后端返回数据在地图上显示标记点,并且根据数据状态控制标记点颜色,标记点背景通过两张图片实现控制 <mapstyle"width: 100vw; height: 100vh;":markers"markers":longitude"locaInfo.longitude":latitude&…...
Windows:批处理脚本学习
目录 一、第一个批处理文件 1. &&和 | | 2. | 和 & 二、变量 1.传参变量%name 2.初始化变量set命令 3.变量的使用 4.局部变量与全局变量 5.使用环境变量 6.扩充变量语法 三、注释REM和 :: 四:函数 1.定义函数 2.…...
Dav_笔记10:Using SQL Plan Management之4
SQL管理库 SQL管理库(SMB)是驻留在SYSAUX表空间中的数据字典的一部分。它存储语句日志,计划历史记录,SQL计划基准和SQL配置文件。为了允许每周清除未使用的计划和日志,SMB使用自动空间管理。 您还可以手动将计划添加到SMB以获取一组SQL语句。从Oracle Database 11g之前的…...
通过json传递请求参数,如何处理动态参数和接口依赖
嗨,大家好,我是兰若姐姐,今天给大家讲一下如何通过json传递请求参数,如何处理动态参数和接口依赖 1. 使用配置文件和模板 在 test_data.json 中,你可以使用一些占位符或模板变量,然后在运行测试之前&…...
[240727] Qt Creator 14 发布 | AMD 推迟 Ryzen 9000芯片发布
目录 Qt Creator 14 发布Qt Creator 14 版本发布,带来一系列新功能和改进终端用户可通过命令行方式查看此新闻终端用户可通过命令行方式安装软件: AMD 推迟 Ryzen 9000芯片发布 Qt Creator 14 发布 Qt Creator 14 版本发布,带来一系列新功能…...
PLSQL Developer工具查询数据,报错(动态性能表不可访问)
解决的问题: 解决方案: 在配置-首选项-选项,取消勾选“自动统计”,保存之后即可查询数据...
基于 HTML+ECharts 实现智慧交通数据可视化大屏(含源码)
构建智慧交通数据可视化大屏:基于 HTML 和 ECharts 的实现 随着城市化进程的加快,智慧交通系统已成为提升城市管理效率和居民生活质量的关键。通过数据可视化,交通管理部门可以实时监控交通流量、事故发生率、道路状况等关键指标,…...
探索 IT 领域的新宠儿:量子计算
目录 引言:从经典到量子的飞跃 量子计算的基本概念 量子计算的独特优势 量子计算的深度剖析 量子计算的最新进展 量子计算的行业应用前景 面临的挑战与未来展望 结语:迎接量子计算的新时代 引言:从经典到量子的飞跃 在信息技术飞速发…...
TSPNet代码分析
论文《Realigning Confidence with Temporal Saliency Information for Point-Level Weakly-Supervised Temporal Action Localization》的official code分析 论文解读 代码分析 先看看训练过程,执行main if __name__ == __main__:exp = Exp()if exp.config.mode == eval:…...
Ubuntu上安装anaconda创建虚拟环境(各种踩坑版)
之前都是在Windows桌面版进行深度学习的环境部署及训练,今天尝试了一下在Ubuntu上进行环境部署,踩了不少坑,提供一些解决办法给大家避雷。 目录 一、下载和安装anaconda 1. 下载 2. 安装 二、创建虚拟环境 一、下载和安装anaconda 1. …...
java_网络服务相关_gateway_nacos_feign区别联系
1. spring-cloud-starter-gateway 作用:作为微服务架构的网关,统一入口,处理所有外部请求。 核心能力: 路由转发(基于路径、服务名等)过滤器(鉴权、限流、日志、Header 处理)支持负…...
Python爬虫实战:研究feedparser库相关技术
1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...
论文浅尝 | 基于判别指令微调生成式大语言模型的知识图谱补全方法(ISWC2024)
笔记整理:刘治强,浙江大学硕士生,研究方向为知识图谱表示学习,大语言模型 论文链接:http://arxiv.org/abs/2407.16127 发表会议:ISWC 2024 1. 动机 传统的知识图谱补全(KGC)模型通过…...
JDK 17 新特性
#JDK 17 新特性 /**************** 文本块 *****************/ python/scala中早就支持,不稀奇 String json “”" { “name”: “Java”, “version”: 17 } “”"; /**************** Switch 语句 -> 表达式 *****************/ 挺好的ÿ…...
代理篇12|深入理解 Vite中的Proxy接口代理配置
在前端开发中,常常会遇到 跨域请求接口 的情况。为了解决这个问题,Vite 和 Webpack 都提供了 proxy 代理功能,用于将本地开发请求转发到后端服务器。 什么是代理(proxy)? 代理是在开发过程中,前端项目通过开发服务器,将指定的请求“转发”到真实的后端服务器,从而绕…...
Springboot社区养老保险系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,社区养老保险系统小程序被用户普遍使用,为方…...
A2A JS SDK 完整教程:快速入门指南
目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库ÿ…...
处理vxe-table 表尾数据是单独一个接口,表格tableData数据更新后,需要点击两下,表尾才是正确的
修改bug思路: 分别把 tabledata 和 表尾相关数据 console.log() 发现 更新数据先后顺序不对 settimeout延迟查询表格接口 ——测试可行 升级↑:async await 等接口返回后再开始下一个接口查询 ________________________________________________________…...
Kubernetes 网络模型深度解析:Pod IP 与 Service 的负载均衡机制,Service到底是什么?
Pod IP 的本质与特性 Pod IP 的定位 纯端点地址:Pod IP 是分配给 Pod 网络命名空间的真实 IP 地址(如 10.244.1.2)无特殊名称:在 Kubernetes 中,它通常被称为 “Pod IP” 或 “容器 IP”生命周期:与 Pod …...
第一篇:Liunx环境下搭建PaddlePaddle 3.0基础环境(Liunx Centos8.5安装Python3.10+pip3.10)
第一篇:Liunx环境下搭建PaddlePaddle 3.0基础环境(Liunx Centos8.5安装Python3.10pip3.10) 一:前言二:安装编译依赖二:安装Python3.10三:安装PIP3.10四:安装Paddlepaddle基础框架4.1…...
