日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2025/7/14 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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识别功能的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。