开源 IP 地址定位库 ip2region 1.4

ip2region 是准确率 99.9% 的 IP 地址定位库,0.0x毫秒级查询,数据库文件大小只有 2.7M,提供了 Java、PHP、C、Python、Node.js、Golang 的查询绑定和 Binary、B树、内存三种查询算法,妈妈再也不同担心我的 IP 地址定位!

ip2region 1.4 更新如下:

  1. 数据升级至 2018/01/12 的版本,包括 ip.merge.txt 和 ip2region.db。

  2. 升级的数据增加了 ipip 的缺失验证,主数据源缺失的数据使用 ipip 补充。

升级提醒:因为每次更新都有可能会更改 ip2region.db 的生成配置,请保持使用最新版本的 binding。

下载地址:

码云:https://gitee.com/lionsoul/ip2region/tree/v1.4-release
github:https://github.com/lionsoul2014/ip2region/releases/tag/v1.4-release

一.Java - 使用Ip2Region(ip地址定位库)转换IP和地区

创建Maven工程(略)

如果使用IDEA,可先创建普通工程,然后右键点击工程,执行“Add Frameowrk support”,选择Maven,即可快速转换为Maven工程。

引入Maven依赖

   <dependencies>
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
 import org.lionsoul.ip2region.*;

 /**
* Ip2Region测试类
* @author Zebe
*/
public class Ip2RegionTest { /**
* 程序入口
* @param args 运行参数
*/
public static void main(String[] args) throws Exception {
String ip = "47.95.196.158";
// 判断是否为IP地址
boolean isIpAddress = Util.isIpAddress("12123.34"); // false
isIpAddress = Util.isIpAddress(ip); // true // IP地址与long互转
long ipLong = Util.ip2long(ip); //
String strIp = Util.long2ip(ipLong); // 47.95.196.158 // 根据IP搜索地址信息
DbConfig config = new DbConfig();
String dbfile = "D:\\data\\ip2region.db"; // 这个文件若没有请到以下地址下载:
// https://gitee.com/lionsoul/ip2region/tree/master/data
DbSearcher searcher = new DbSearcher(config, dbfile); // 二分搜索
long start = System.currentTimeMillis();
DataBlock block1 = searcher.binarySearch(ip);
long end = System.currentTimeMillis();
System.out.println(block1.getRegion()); // 中国|华东|浙江省|杭州市|阿里巴巴
System.out.println("使用二分搜索,耗时:" + (end - start) + " ms"); // 1ms // B树搜索(更快)
start = System.currentTimeMillis();
DataBlock block2 = searcher.btreeSearch(ip);
end = System.currentTimeMillis();
System.out.println("使用B树搜索,耗时:" + (end - start) + " ms"); // 0ms
}
}

其中需要下载ip2region.db:

二.使用Ip2Region(ip地址定位库)转换IP和地区

     public static void DownLoadDbFile(String path) {

         FileSystem fs,local;
try {
fs = FileSystem.get(new URI(path.substring(0, path.indexOf('/', path.indexOf(':') + 3))), new Configuration());
local = FileSystem.getLocal(new Configuration());
local.delete(new Path("/tmp/ip2region.db"), true);
fs.copyToLocalFile(false, new Path(path), new Path("/tmp/ip2region.db"));
} catch (IOException e) {
// TODO 自动生成的 catch 块
SparkLog.error(e);
} catch (URISyntaxException e) {
// TODO 自动生成的 catch 块
SparkLog.error(e);
}
} public static void Ip2RegionTest() {
FileSystem fs,local;
String path="hdfs://10.8.18.74:8020/user/lyy/data/ip2region.db";//先将db文件上传到环境中的hdfs上
try {
fs = FileSystem.get(new URI(path.substring(0, path.indexOf('/', path.indexOf(':') + 3))), new Configuration());
local = FileSystem.getLocal(new Configuration());
local.delete(new Path("/tmp/ip2region.db"), true);
fs.copyToLocalFile(false, new Path(path), new Path("/tmp/ip2region.db"));//下载db文件到相应的driver和excutor节点本地的/tmp的目录下
} catch (Throwable e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} String ip =""; DbConfig config;
try {
JavaSparkContext sc = SparkCommon.getJavaSparkContext(); JavaRDD<String> rdd = sc.parallelize(Arrays.asList("91.235.133.117","62.87.232.88")); config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, "/tmp/ip2region.db");
SparkLog.info(rdd.count());
//以下的迭代及转化都在driver端完成,这里存在风险,当数据量超大的时候,driver可能出现OOM
Iterator<String> It = rdd.collect().iterator();
while(It.hasNext()){
ip = It.next();
DataBlock block2 = searcher.btreeSearch(ip);
SparkLog.info(block2.getRegion());
} } catch (Throwable e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/*这里展示的转换后的ip和region的信息如何再次组织成rdd,并转换成dataset的一种方法
List<Row> lst = new ArrayList<>();
while(It.hasNext()){
ip = It.next();
DataBlock block2 = searcher.btreeSearch(ip);
lst.add(RowFactory.create(ip,block2.getRegion()));
}
SparkSession session = SparkCommon.getSparkSessionInstance(); StructType schema = new StructType(new StructField[]{
new StructField("hostname1", DataTypes.StringType, false, Metadata.empty()),
new StructField("country", DataTypes.StringType, false, Metadata.empty()),
}); Dataset<Row> ip2region = session.createDataFrame(SparkCommon.getJavaSparkContext().parallelize(lst), schema); return df.join(ip2region,df.col("hostname").equalTo(ip2region.col("hostname1")));*/ }

Spark- 根据IP获取城市(java)的更多相关文章

  1. 根据Ip获取城市帮助类

    思路构建 1.先通过本地的测IP地址库进行匹配 2.如果本地IP地址库存在此IP的城市信息,就直接返回,调用速度也快 3.如果本地没有对应的IP城市信息,必须通过调用网络的IP查询的API了,这里我使 ...

  2. php 根据ip获取城市以及网络运营商名称(利用qqwry.dat)

    根据用户IP地址判定出所在城市以及网络运营商 qqwry.dat下载地址:http://files.cnblogs.com/guangxiaoluo/qqwry.rar  解压出来即可 //获取用户真 ...

  3. c#根据ip获取城市地址

    用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...

  4. 通过外部接口 根据ip获取城市名

    3种接口 淘宝/百度/不知名/   推荐淘宝接口 ip自个去获取,下附带php 获取ip的示例 function getIP() { static $realip; if (isset($_SERVE ...

  5. C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

    百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...

  6. 百度地图API的IP定位城市和浏览器定位(转)

    百度地图API提供了Geolocation 和 LocalCity两个服务类. 这俩API可以分别供用户在JavaScript中进行定位和城市确认. 1 本质上,Geolocation这个类是使用了支 ...

  7. 根据IP获得城市信息(百度API的运用)

    /**      * 根据IP获取城市      * @param string $ip ip地址      * @return array      * http://api.map.baidu.c ...

  8. java 根据ip获取地区信息(淘宝和新浪)

    package com.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...

  9. Java 根据IP获取地址

    用淘宝接口:(源码:java 根据IP地址获取地理位置) pom.xml: <!-- https://mvnrepository.com/artifact/net.sourceforge.jre ...

随机推荐

  1. css百分比问题——`top`、`left`、'translate'的百分比参照谁?

    比如 top.left.transform属性的translate方法,他们的百分比都是相较谁而言的? top.left是基于父元素的: .parent { position: relative; b ...

  2. Servlet过滤器Filter和监听器

    一.Servlet过滤器的概念: *********************************************************************************** ...

  3. Resharp常用设置收集整理

    F12跳转的问题:

  4. table表单制作个人简历

    应用table表单,编程个人简历表单,同时运用了跨行rowspan和跨列colspan. <!DOCTYPE html> <html> <head> <met ...

  5. TortoiseGit —— 配置密钥

    TortoiseGit 使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥.使用命令ssh-keygen -C "邮箱地址" -t rsa产生的密钥在Tortoi ...

  6. robotframework+appium,数字键盘输入问题,keycode,press keycode

    需要注意事项 appium自带的输入法应该是无法模拟控制键和基本键的,需要自行使用adb切换成搜狗或者android输入法,然后case完成之后记得切回appium输入法 appium模拟发送基本键命 ...

  7. EEPROM读写学习笔记与I2C总线(二)

    无论任何电子产品都会涉及到数据的产生与数据的保存,这个数据可能并不是用来长久保存,只是在运行程序才会用到,有些数据体量较大对于获取时效性并不太强,各种各样的数据也就有不同的存储载体,这次在EEPROM ...

  8. LeetCode初级算法的Python实现--动态规划

    动态规划的本质是递归:所以做题之前一定要会递归:递归式就是状态转移方程:这里将会介绍使用动态规划做题的思维方式. 统一的做题步骤: 1.用递归的方式写出代码:(此方法写的代码在leetcode中一定会 ...

  9. Python概述

    1.什么是Python? Python是一种解释型,面向对象,动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年. 像 ...

  10. day 6 老王开枪打人

    1.图示 2 程序 1)版本1:框架的搭建 def main(): '''用来控制这个程序的流程''' pass #1.创建alex对象 #2.创建1个枪对象 #3.创建1个弹夹对象 #4.创建子弹对 ...