Github: https://github.com/EeeMt/phone-number-geo
根据手机号离线查询手机号运营商及归属地, 支持包括虚拟运营商的中国大陆手机号查询. 适合对号码覆盖范围有要求的, 内网 /离线使用.
觉得可以点个Star啊.
已发布到maven
中央仓库.
<dependency> <groupId>me.ihxq.projects</groupId> <artifactId>phone-number-geo</artifactId> <version>x.x.x-xxxxxx</version> </dependency>
在这里获取最新版号.
下面都是摘抄REAMDME.md
的内容
class Demo1{ public static void main(String[] args){ PhoneNumberLookup phOneNumberLookup= new PhoneNumberLookup(); PhoneNumberInfo found = phoneNumberLookup.lookup("18798896741").orElseThrow(RuntimeException::new); } }
class Demo2{ public static void main(String[] args){ PhoneNumberLookup phOneNumberLookup= new PhoneNumberLookup(); String province = phoneNumberLookup.lookup("130898976761") .map(PhoneNumberInfo::getAttribution) .map(Attribution::getProvince) .orElse("未知"); } }
class Demo3{ public static void main(String[] args){ PhoneNumberLookup phOneNumberLookup= new PhoneNumberLookup(); PhoneNumberInfo found = phoneNumberLookup.lookup("18798896741").orElseThrow(RuntimeException::new); found.getNumber(); // 18798896741 found.getAttribution().getProvince(); // 贵州 found.getAttribution().getCity(); // 贵阳 found.getAttribution().getZipCode(); // 550000 found.getAttribution().getAreaCode(); // 0851 found.getIsp(); // ISP.CHINA_MOBILE } }
libphonenumber
libphonenumber有更多功能, 包括验证号码格式, 格式化, 时区等, 但基于xluohome/phonedata提供的dat
数据库能囊括包含虚拟运营商号段的更多号段.
至于速度, 未做比较, 选择时建议更多权衡易用性, 功能和数据覆盖范围.
工程里已内置四种算法, 跑分情况如下:
Benchmark Mode Cnt Score Error Units BenchmarkRunner.anotherBinarySearchLookup avgt 5 390.483 ± 3.544 ns/op BenchmarkRunner.binarySearchLookup avgt 5 386.357 ± 3.739 ns/op BenchmarkRunner.prospectBinarySearchLookup avgt 5 304.622 ± 1.899 ns/op BenchmarkRunner.sequenceLookup avgt 5 1555265.227 ± 48814.379 ns/op
性能测试源码位于me.ihxq.projects.pna.benchmark.BenchmarkRunner
, 基于JMH
测试样本在每次启动时生成, 供所有算子测试使用, 所以每次测试结果有差异, 结果可用于横向比较, 不适用于纵向比较.
默认使用的是me.ihxq.projects.pna.algorithm.BinarySearchAlgorithmImpl
, 可以通过new PhoneNumberLookup(new AlgorithmYouLike());
使用其他算法;
也可自行实现算法, 实现me.ihxq.projects.pna.algorithm.LookupAlgorithm
即可.
![]() | 1 monospace 2020-02-11 17:17:57 +08:00 携号转网的情况能 cover 吗? |
![]() | 2 jugelizi 2020-02-11 17:32:05 +08:00 类似于 ip 离线库? |
![]() | 3 fancy20 2020-02-11 18:18:09 +08:00 想说我也写过这个,然后看到写了参考了我的 java 实现 |