开源 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. Knowledge Point 20180305 补位的两种方式

    我们都知道Java的基本数据类型内存中都有一个固定的位数(内存分配空间),如byte占8位,int占32位等.正因如此,当把一个低精度的数据类型转成一个高精度的数据类型时,必然会涉及到如何扩展位数的问 ...

  2. [HTML]在页面中输出空格的几种方式

    JS中如何输出空格 在写JS代码的时候,大家可以会发现这样现象: document.write("   1      2                3  "); 结果: 1 2 ...

  3. Subclass UICollectionViewFlowLayout,自定义流布局

    需求:为实现第一行显示一个,第二行以后显示两个 方案1:用系统自带的流布局,实现的效果是,若第二行只有一个,则系统默认会居中显示,不是左对齐(如下图),不符合项目要求. 方案2:自定义系统的UICol ...

  4. free -g 说明

    free -g 说明: free -g -/+ buffers/cache 说明: buffer 写缓存,表示脏数据写入磁盘之前缓存一段时间,可以释放.sync命令可以把buffer强制写入硬盘 ca ...

  5. git 之忽略文件 gitignore 创建和使用规则

    1..gitignore文件的创建:首先要强调一点,这个文件的完整文件名就是“.gitignore”,注意最前面有个“.”.这样没有扩展名的文件在Windows下不太好创建,这里给出win7的创建方法 ...

  6. 二、linux编译环境的搭建

    1.linux编译工具安装 vim安装:apt-get install vim 注意:使用C语言源代码语法加亮功能,需要配置文件/etc/vim/vimrc,加入代码syntaxon.文件后缀必须为. ...

  7. pip快速git项目安装

    pip install git+https://github.com/xx/xx.git

  8. rsync同步常用命令

    转载源地址http://blog.csdn.net/niushuai666/article/details/16880061 如果你是一位运维工程师,你很可能会面对几十台.几百台甚至上千台服务器,除了 ...

  9. Prism for WPF 搭建一个简单的模块化开发框架(二)

    原文:Prism for WPF 搭建一个简单的模块化开发框架(二) 今天又有时间了,再改改,加了一些控件全局的样式 样式代码 <ResourceDictionary xmlns="h ...

  10. BZOJ1053_反素数_KEY

    题目传送门 初看这道题,以为是一道挺难的题目,但仔细看发现,不是只要爆搜就好了吗? 只需要对前12个素数进行爆搜即可. 一个数的因数个数=素数次数+1全部乘起来. code: /*********** ...