现在的位置: 首页 > 综合 > 正文

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

2013年08月04日 ⁄ 综合 ⁄ 共 1884字 ⁄ 字号 评论关闭

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/

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执行以下语句:

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不支持注册用户自定义函数。

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

抱歉!评论已关闭.