Hive UDF IP解析(二):使用geoip2数据库自定义UDF
开发中经常会碰到将IP转为地域的问题,所以以下记录Hive中自定义UDF来解析IP。
使用到的地域库位maxmind公司的geoIP2数据库,分为免费版GeoLite2-City.mmdb和收费版GeoIP2-City.mmdb,不管哪个版本,开发的接口都是相同。
开发环境:
hive-2.3.0
hadoop 2.7.3
jdk 1.8
1. 新建maven项目regionParse,加入以下依赖包
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.maxmind.db</groupId>
<artifactId>maxmind-db</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.11.0</version>
</dependency>
2.1 新建IP2Region.java,继承UDF(中文版)
package com.gw.udf; import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException; import org.apache.hadoop.hive.ql.exec.UDF;
//import org.apache.log4j.Logger; import com.gw.util.Constants;
import com.gw.util.geoip2.GWDatabaseReader; //重写了com.maxmind.geoip2.DatabaseReader类
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Subdivision; public class IP2Region extends UDF { // private static Logger log = Logger.getLogger(IP2Region.class); private static GWDatabaseReader reader = null; static {
String path = IP2Region.class.getResource("/").getFile().toString();
try {
System.out.println(path + Constants.geoIPFile);
File database = new File(path + Constants.geoIPFile);
reader = new GWDatabaseReader.Builder(database).build(); } catch (IOException e) {
// log.error(e.getMessage());
} } @SuppressWarnings("finally")
public static String evaluate(String ip,int i) {
InetAddress ipAddress;
String region = null;
try {
ipAddress = InetAddress.getByName(ip);
CityResponse response = null;
response = reader.city(ipAddress);
Country country = response.getCountry();
Subdivision subdivision = response.getMostSpecificSubdivision();
City city = response.getCity();
if(country!=null && i==0){
region = country.getNames().get("zh-CN") != null ? country.getNames().get("zh-CN") : country.getNames().get("en") ;
}
else if(subdivision!=null && i == 1 ){ region = Constants.provinceCN.get(subdivision.getIsoCode()) != null ?
Constants.provinceCN.get(subdivision.getIsoCode()) :
subdivision.getNames().get("en") ;
}
else if(city!=null && i == 2){
String province = city.getNames().get("zh-CN");
if(province != null && province.lastIndexOf("市") > 0){
province = province.substring(0,province.length()-1);
}
region = province != null ? province : city.getNames().get("en");
} // Postal postal = response.getPostal();
//获取经纬度,暂时不需要
// Location location = response.getLocation();
// System.out.println(location.getLatitude()); // 44.9733
// System.out.println(location.getLongitude()); // -93.2323 } catch (Exception e) {
// log.error(e.getMessage());
} finally{
return region;
} } public static void main(String[] args) throws UnknownHostException,
IOException, GeoIp2Exception {
String ip = "223.104.6.25";
System.out.println(evaluate(ip,0) + ":" + evaluate(ip,1) + ":" + evaluate(ip,2)); } }
2.2 新建IP2RegionEN.java, 继承UDF(英文版)
package com.gw.udf; import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException; import org.apache.hadoop.hive.ql.exec.UDF;
//import org.apache.log4j.Logger; import com.gw.util.Constants;
import com.gw.util.geoip2.GWDatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Subdivision; public class IP2RegionEN extends UDF { // private static Logger log = Logger.getLogger(IP2Region.class); private static GWDatabaseReader reader = null; static {
String path = IP2RegionEN.class.getResource("/").getFile().toString();
try {
System.out.println(path + Constants.geoIPFile);
File database = new File(path + Constants.geoIPFile);
reader = new GWDatabaseReader.Builder(database).build(); } catch (IOException e) {
// log.error(e.getMessage());
} } @SuppressWarnings("finally")
public static String evaluate(String ip,int i) {
InetAddress ipAddress;
String region = null;
try {
ipAddress = InetAddress.getByName(ip);
CityResponse response = null;
response = reader.city(ipAddress);
Country country = response.getCountry();
Subdivision subdivision = response.getMostSpecificSubdivision();
City city = response.getCity();
if(country!=null && i==0){
region = country.getNames().get("en") ;
}
else if(subdivision!=null && i == 1 ){ region = subdivision.getNames().get("en") ;
}
else if(city!=null && i == 2){
region = city.getNames().get("en");
} // Postal postal = response.getPostal();
//获取经纬度,暂时不需要
// Location location = response.getLocation();
// System.out.println(location.getLatitude()); // 44.9733
// System.out.println(location.getLongitude()); // -93.2323 } catch (Exception e) {
// log.error(e.getMessage());
} finally{
return region;
} } public static void main(String[] args) throws UnknownHostException,
IOException, GeoIp2Exception {
String ip = "223.104.6.25";
System.out.println(evaluate(ip,0) + ":" + evaluate(ip,1) + ":" + evaluate(ip,2)); } }
3. 由于免费的数据库解析出来的中文省份含有简称比如福建直接显示为闽,比较坑。
为便于统一,所以自己定义了一个isocode的转换类Constants.java 用于省份的统一显示。
package com.gw.util; import java.util.HashMap;
import java.util.Map; public class Constants { public static final String geoIPFile= "GeoLite2-City.mmdb";
public static final String geollFile = "geo.csv";
public static final Map<String,String> provinceCN = new HashMap<String,String>(); static {
provinceCN.put("AH","安徽");
provinceCN.put("BJ","北京");
provinceCN.put("CQ","重庆");
provinceCN.put("FJ","福建");
provinceCN.put("GD","广东");
provinceCN.put("GS","甘肃");
provinceCN.put("GX","广西");
provinceCN.put("GZ","贵州");
provinceCN.put("HA","河南");
provinceCN.put("HB","湖北");
provinceCN.put("HE","河北");
provinceCN.put("HI","海南");
provinceCN.put("HK","香港");
provinceCN.put("HL","黑龙江");
provinceCN.put("HN","湖南");
provinceCN.put("JL","吉林");
provinceCN.put("JS","江苏");
provinceCN.put("JX","江西");
provinceCN.put("LN","辽宁");
provinceCN.put("MO","澳门");
provinceCN.put("NM","内蒙古");
provinceCN.put("NX","宁夏");
provinceCN.put("QH","青海");
provinceCN.put("SC","四川");
provinceCN.put("SD","山东");
provinceCN.put("SH","上海");
provinceCN.put("SN","陕西");
provinceCN.put("SX","山西");
provinceCN.put("TJ","天津");
provinceCN.put("TW","台湾");
provinceCN.put("XJ","新疆");
provinceCN.put("XZ","西藏");
provinceCN.put("YN","云南");
provinceCN.put("ZJ","浙江");
} }
4. 在测试过程中发现已有的geoip2的jar和hive系统中其他jar有冲突,所以又修改了geoip2的部分源码。

最后,打包编译提交到hive的external_lib(自定义包统一存放的文件夹)中。
5. 测试结果:
ip2region(ip,0)表示国家
ip2region(ip,1)表示省份
ip2region(ip,2)表示城市
select ip,ip2region(ip,0),ip2region(ip,1),ip2region(ip,2) from xxx_table where ip is not null limit 300;
测试结果看到部分城市给出的是拼音(Shijiazhuang),这个免费库还是有点坑爹,暂时可以将就使用了。
中文版本如下:

英文版本如下:

最后附上整个部署的说明:
1. 将数据库文件添加到hive的路径 $HIVE_HOME/conf目录下
以后如果有IP数据库城市的更新,直接更新这个目录下的文件。
省份文件固定,不需要修改。
数据库文件有三个:geoCN.csv、geoEN.csv、GeoLite2-City.mmdb 2. 删除$HIVE_HOME/lib/目录下 geoip2-0.4.0.jar、maxminddb-0.2.0.jar 文件
使用maxmind-db-1.2.2.jar、geoip2-2.11.0.jar替换放入。 3. 将编译的regionalParse-0.0.1-SNAPSHOT.jar放到external_lib目录。 4. 修改.hiverc文件,添加以下内容
add jar /usr/local/hive/external_lib/regionalParse-0.0.1-SNAPSHOT.jar; create temporary function ll2province as 'com.gw.udf.LL2Province';
create temporary function ll2provinceen as 'com.gw.udf.LL2ProvinceEN';
create temporary function indexof as 'com.gw.udf.IndexOf';
create temporary function ip2region as 'com.gw.udf.IP2Region';
create temporary function ip2regionen as 'com.gw.udf.IP2RegionEN'; 5. 重启hiveServer2 #!/bin/sh
nohup $HIVE_HOME/bin/hive --service hiveserver2 & 6. 测试方法: 测试中文:
select ip,ip2region(ip,0),ip2region(ip,1),ip2region(ip,2),
ll2province(substring(coordinator, 0, indexof(coordinator, ":")), substring(coordinator, indexof(coordinator, ":") + 2))
from xxx where ip is not null limit 10; 测试英文:
select ip,ip2regionen(ip,0),ip2regionen(ip,1),ip2regionen(ip,2),
ll2provinceen(substring(coordinator, 0, indexof(coordinator, ":")), substring(coordinator, indexof(coordinator, ":") + 2))
from xxx where ip is not null limit 10;
Hive UDF IP解析(二):使用geoip2数据库自定义UDF的更多相关文章
- Hive UDF IP解析(一):依赖包兼容性问题
Java依赖环境: <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-e ...
- 自定义UDF函数应用异常
自定义UDF函数应用异常 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: http://www.cnblogs.com/sxt-zkys/QQ技术交流群:299142667 ...
- Hive框架基础(二)
* Hive框架基础(二) 我们继续讨论hive框架 * Hive的外部表与内部表 内部表:hive默认创建的是内部表 例如: create table table001 (name string , ...
- IP工具类-自己动手做个ip解析器
IP工具类-自己动手做个ip解析器 一.资料准备 导入依赖包:
- hive 存储,解析,处理json数据
hive 处理json数据总体来说有两个方向的路走 1.将json以字符串的方式整个入Hive表,然后通过使用UDF函数解析已经导入到hive中的数据,比如使用LATERAL VIEW json_tu ...
- Spring Security 解析(二) —— 认证过程
Spring Security 解析(二) -- 认证过程 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .S ...
- 企业运维实践-Nginx使用geoip2模块并利用MaxMind的GeoIP2数据库实现处理不同国家或城市的访问最佳实践指南
关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x00 前言 ...
- MySQL 系列(二) 你不知道的数据库操作
第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 本章内容: 查看\创建\使用\删除 数据库 用户管理及授权实战 局域网 ...
- .net Core 调用微信Jsapi接口,H5解析二维码
项目里需要用到扫描二维码,自己实现,不会. 找到了两种解决方案: 通过reqrcode.js,这是一个前端解析二维码内容的js库.如果二维码比较清晰,用这种效果也不错 调用微信扫一扫功能,这种效果很好 ...
随机推荐
- web服务器一些概念
一.Web服务器 Web服务基于HTTP协议,HTTP协议基于TCP/IP协议. HTTP请求底层必然是套接字,也就是TCP协议,HTTP服务器就是监听端口的一个守护进程. 它负责HTTP连接的建立. ...
- 【LeetCode】13. Roman to Integer (2 solutions)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 理解over()函数
1.1.两个order by的执行时机分析函数(以及与其配合的开窗函数over())是在整个sql查询结束后(sql语句中的order by的执行比较特殊)再进行的操作, 也就是说sql语句中的ord ...
- 在windows下部署Redis 当作系统服务器自动启动
部署Redis 其实Redis是可以安装成windows服务的,开机自启动,命令如下: redis-server --service-install redis.windows.conf 安装完之后, ...
- 不停mysql服务添加从库的两种方式
现在生产环境MySQL数据库是一主一从,由于业务量访问不断增大,故再增加一台从库.前提是不能影响线上业务使用,也就是说不能重启MySQL服务,为了避免出现其他情况,选择在网站访问量低峰期时间段操作. ...
- OpenGl学习 glenable()函数理解
glEnable用于启用各种功能.功能由参数决定.与glDisable相对应.glDisable是用来关闭的.两个函数参数取值是一至的. 参数说明:void glEnable(GLenum cap)G ...
- WCF入门学习1-最简单的一次通信
跟着msdn的教程试了一下wcf,真心好用 1.先创建一个wcf服务库,是服务类库,远程的lib 2.全部按照默认设置,不修改.然后点发布,会出现一个wcf测试客户端,可以看有没有发布成功. 3.ms ...
- SourceInsight-查看java中接口对应的实现类
1.双击选中需要查看的接口名称,然后右击选择“Show in Relation Window” 2.然后在右侧会弹出一个Relation的窗口 如果没有列出对应的实现类,可以在接口名上再次右击,依次选 ...
- cocos2d-x 父节点和子节点执行不同动作
Test6::Test6() { CCSprite* sp1 = CCSprite::create(s_pPathSister1); CCSprite* sp11 = CCSprite::create ...
- Not get deviceToken yet. Maybe: your certificate not configured APNs? or current network is not so good so APNs registration failed? or there is no APNs register code? Please refer to JPush docs.”
今天在项目中集成了极光推送,一切都配置完毕,把程序运行起来的时候,报了下面的错误: Not get deviceToken yet. Maybe: your certificate not confi ...