欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

使用Hive UDF和GeoIP库为Hive加入IP识别功能

发布时间:2025/7/14 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 使用Hive UDF和GeoIP库为Hive加入IP识别功能 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
导读:Hive是基于Hadoop的数据管理系统,作为分析人员的即时分析工具和ETL等工作的执行引擎,对于如今的大数据管理与分析、处理有着非常大的意义。GeoIP是一套IP映射库系统,它定时更新,并且提供了各种语言的API,非常适合在做地域相关数据分析时的一个数据源。

Hive是基于Hadoop的数据管理系统,作为分析人员的即时分析工具和ETL等工作的执行引擎,对于如今的大数据管理与分析、处理有着非常大的意义。GeoIP是一套IP映射库系统,它定时更新,并且提供了各种语言的API,非常适合在做地域相关数据分析时的一个数据源。

 

UDF是Hive提供的用户自定义函数的接口,通过实现它可以扩展Hive目前已有的内置函数。而为Hive加入一个IP映射函数,我们只需要简单地在UDF中调用GeoIP的Java API即可。

GeoIP的数据文件可以从这里下载:http://www.maxmind.com/download/geoip/database/,由于需要国家和城市的信息,我这里下载的是http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

 

GeoIP的各种语言的API可以从这里下载:http://www.maxmind.com/download/geoip/api/

 

 

查看文本copy to clipboard打印?
  • import java.io.IOException;   
  •   
  • import org.apache.hadoop.hive.ql.exec.UDF;   
  •   
  • import com.maxmind.geoip.Location;   
  • import com.maxmind.geoip.LookupService;   
  • import java.util.regex.*;   
  •   
  • public class IPToCC  extends UDF {   
  •     private static LookupService cl = null;   
  •     private static String ipPattern = "\\d+\\.\\d+\\.\\d+\\.\\d+";   
  •     private static String ipNumPattern = "\\d+";   
  •        
  •     static LookupService getLS() throws IOException{   
  •         String dbfile = "GeoLiteCity.dat";   
  •         if(cl == null)   
  •             cl = new LookupService(dbfile, LookupService.GEOIP_MEMORY_CACHE);   
  •         return cl;   
  •     }   
  •        
  •     /**  
  •      * @param str like "114.43.181.143"  
  •      * */  
  •        
  •     public String evaluate(String str) {   
  •         try{   
  •             Location Al = null;   
  •             Matcher mIP = Pattern.compile(ipPattern).matcher(str);   
  •             Matcher mIPNum = Pattern.compile(ipNumPattern).matcher(str);   
  •             if(mIP.matches())   
  •                 Al = getLS().getLocation(str);   
  •             else if(mIPNum.matches())   
  •                 Al = getLS().getLocation(Long.parseLong(str));   
  •             return String.format("%s\t%s", Al.countryName, Al.city);   
  •         }catch(Exception e){   
  •             e.printStackTrace();   
  •             if(cl != null)   
  •                 cl.close();   
  •             return null;   
  •         }   
  •     }   
  •   
  • }  
  • import java.io.IOException;import org.apache.hadoop.hive.ql.exec.UDF;import com.maxmind.geoip.Location; import com.maxmind.geoip.LookupService; import java.util.regex.*;public class IPToCC extends UDF {private static LookupService cl = null;private static String ipPattern = "\\d+\\.\\d+\\.\\d+\\.\\d+";private static String ipNumPattern = "\\d+";static LookupService getLS() throws IOException{String dbfile = "GeoLiteCity.dat";if(cl == null)cl = new LookupService(dbfile, LookupService.GEOIP_MEMORY_CACHE);return cl;}/*** @param str like "114.43.181.143"* */public String evaluate(String str) {try{Location Al = null;Matcher mIP = Pattern.compile(ipPattern).matcher(str);Matcher mIPNum = Pattern.compile(ipNumPattern).matcher(str);if(mIP.matches())Al = getLS().getLocation(str);else if(mIPNum.matches())Al = getLS().getLocation(Long.parseLong(str));return String.format("%s\t%s", Al.countryName, Al.city);}catch(Exception e){e.printStackTrace();if(cl != null)cl.close();return null;}}}

     

     

     

    使用上也非常简单,将以上程序和GeoIP的API程序,一起打成JAR包iptocc.jar,和数据文件(GeoLiteCity.dat)一起放到Hive所在的服务器的一个位置。然后打开Hive执行以下语句:

     

    查看文本copy to clipboard打印?
  • add file /tje/path/to/GeoLiteCity.dat;   
  • add jar /the/path/to/iptocc.jar;   
  • create temporary function ip2cc as 'your.company.udf.IPToCC';  
  • add file /tje/path/to/GeoLiteCity.dat; add jar /the/path/to/iptocc.jar; create temporary function ip2cc as 'your.company.udf.IPToCC';
    然后就可以在Hive的CLI中使用这个函数了,这个函数接收标准的IPv4地址格式的字符串,返回国家和城市信息;同样这个函数也透明地支持长整形的IPv4地址表示格式。如果想在每次启动Hive CLI的时候都自动加载这个自定义函数,可以在hive命令同目录下建立.hiverc文件,在启动写入以上三条语句,重新启动Hive CLI即可;如果在这台服务器上启动Hive Server,使用JDBC连接,执行以上三条语句之后,也可以正常使用这个函数;但是唯一一点不足是,HUE的Beeswax不支持注册用户自定义函数。

     

     

    虽然不尽完美,但是加入这样一个函数,对于以后做地域相关的即时分析总是提供了一些方便的,还是非常值得加入的。

    转载于:https://www.cnblogs.com/xd502djj/p/3253411.html

    总结

    以上是生活随笔为你收集整理的使用Hive UDF和GeoIP库为Hive加入IP识别功能的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。