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库.如果二维码比较清晰,用这种效果也不错 调用微信扫一扫功能,这种效果很好 ...
随机推荐
- java提高篇之详解内部类
可以将一个类的定义放在另一个类的定义内部,这就是内部类. 内部类是一个非常有用的特性但又比较难理解使用的特性(鄙人到现在都没有怎么使用过内部类,对内部类也只是略知一二). 第一次见面 内部类我们从外面 ...
- Python学习笔记014——迭代工具函数 内置函数zip()
1 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操 ...
- 关于 Chrome Console 查看DOM详情细节的奇思淫巧
我们期待打印出的dom效果如下: 但某些时候,打印出来,或者通过$0.$1.document.getElementById('####') 等方式打印出来的效果如下: 根据第一幅图我们不难看出,当打印 ...
- django打印字典
- 利用ichart绘制网页图表
首先,最好的教程在这里:ichartjs 有了这个网站,要绘制网页图表简直方便愉快! 接下来说一下使用方法~~~ 进入网站,点击在线设计器 在线设计器的使用方法就不说了,摸索一下就会了!关键在于两个地 ...
- getXXXPos()约定
class CmyNode:public CCNode{ public: CmyNode(){ m_XXX=NULL; } virtual~CmyNode(){ } bool init(){ m_XX ...
- 分享二:架构设计分享一:关于API分布式服务提供方式
一:基于HTTP协议的Web API 1:RESTful API http://www.ruanyifeng.com/blog/2011/09/restful 二:
- [ci] jenkins的Timestamper插件-让日志显示时间
jenkins的Timestamper插件-让jenkins console带时间戳 安装插件 配置pipline,使用timestamp - 官网有说怎么用: 即用timestamps{} 包裹所有 ...
- AAA含义图解
来源: <FreeRADIUS Beginner's Guide> 这本书 1,认证 2,授权 3,审计
- CCMotionStreak(一)
void MotionStreakTest1::onEnter() { MotionStreakTest::onEnter(); CCSize s = CCDirector::sharedDirect ...