使用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/

  1. import java.io.IOException;   
  2.   
  3. import org.apache.hadoop.hive.ql.exec.UDF;   
  4.   
  5. import com.maxmind.geoip.Location;   
  6. import com.maxmind.geoip.LookupService;   
  7. import java.util.regex.*;   
  8.   
  9. public class IPToCC  extends UDF {   
  10.     private static LookupService cl = null;   
  11.     private static String ipPattern = "\d+\.\d+\.\d+\.\d+";   
  12.     private static String ipNumPattern = "\d+";   
  13.        
  14.     static LookupService getLS() throws IOException{   
  15.         String dbfile = "GeoLiteCity.dat";   
  16.         if(cl == null)   
  17.             cl = new LookupService(dbfile, LookupService.GEOIP_MEMORY_CACHE);   
  18.         return cl;   
  19.     }   
  20.        
  21.     /**  
  22.      * @param str like "114.43.181.143"  
  23.      * */  
  24.        
  25.     public String evaluate(String str) {   
  26.         try{   
  27.             Location Al = null;   
  28.             Matcher mIP = Pattern.compile(ipPattern).matcher(str);   
  29.             Matcher mIPNum = Pattern.compile(ipNumPattern).matcher(str);   
  30.             if(mIP.matches())   
  31.                 Al = getLS().getLocation(str);   
  32.             else if(mIPNum.matches())   
  33.                 Al = getLS().getLocation(Long.parseLong(str));   
  34.             return String.format("%s %s", Al.countryName, Al.city);   
  35.         }catch(Exception e){   
  36.             e.printStackTrace();   
  37.             if(cl != null)   
  38.                 cl.close();   
  39.             return null;   
  40.         }   
  41.     }   
  42.   
  43. }  
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	%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执行以下语句:
  1. add file /tje/path/to/GeoLiteCity.dat;   
  2. add jar /the/path/to/iptocc.jar;   
  3. 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