在日誌處理過程中,常常會遇到ip地址的分析,就要涉及到ip地址解析爲城市的操作。
下面介紹2種方法:
方法一:只能夠進行中國的ip地址轉換爲城市的操作,國外的解析不是很實用。
方法二:可以進行全球的ip地址的各項解析,解析城市、國家、經緯度等等。
方法一:
一個使用他人的源碼來實現此功能的方法,希望能幫助有需要的人。
步驟如下:
1)將源碼從github上下載下來
$ git clone https://github.com/wzhe06/ipdatabase
或者:從百度雲中下載:
鏈接:https://pan.baidu.com/s/1Rm9d44bv3Ewd5LZm1pUtwg
提取碼:e6x9
2)進行編譯
$ mvn clean package -DskipTests
3)將編譯好的jar包安裝到maven庫裏
$ mvn clean install -DskipTest
在安裝過程中,查看文件將jar放入到了maven的哪個目錄下
4)將自己項目的pom.xml文件中,添加此源碼的依賴
<dependency>
<groupId>com.ggstar</groupId>
<artifactId>ipdatabase</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
5)編碼應用
package com.hzkj.spark.utils
import com.ggstar.util.ip.IpHelper
/**
* @Title: IpUtils
* @ProjectName SparkStreamingProject
* @Description: IP解析工具類
* @author fanyanyan
*/
object IpUtils {
def getCity(ip: String)={
IpHelper.findRegionByIp(ip)
}
def main(args: Array[String]): Unit = {
println(getCity("123.125.71.38"))
}
}
6)另外該外部源碼的運行,需要依賴兩個文件(可從源碼resources中找到)
ipDatabase.csv
ipRegion.xlsx
記得要將這兩個文件放入到自己項目的resources下
方法二:
該方法需要下載好一個外部數據文件:
地址如下:
鏈接:https://pan.baidu.com/s/1u4_7dfspUYmKt21QBhjc7g
提取碼:619p
下載好之後,將數據文件放入到指定路徑下,便於後期調用。
在pom.xml文件中導入依賴:
<!--ip地址解析依賴-->
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.12.0</version>
</dependency>
編寫測試用例(IpUtils.java):
package com.fyy.spark.project.utils;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.*;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
/**
* @author fanyanyan
* @Title: IpUtils
* @ProjectName SparkStreamingProject
* @Description: 全球Ip地址轉義(國家、城市、經緯度等)
*/
public class IpUtils{
/**
* Ip轉換國家
* @param ip
*/
public static String getCountry(String ip) throws Exception{
File database = new File("E:\\IDEA\\IdeaProject\\SparkStreamingProject\\data\\GeoLite2-City\\GeoLite2-City.mmdb");
DatabaseReader reader = new DatabaseReader.Builder(database).build();
String countryName = "未知";
InetAddress ipAddress = InetAddress.getByName(ip);
CityResponse response = reader.city(ipAddress);
Country country = response.getCountry();
countryName = country.getNames().get("zh-CN");
return countryName;
}
/**
* Ip轉換城市
* @param ip
*/
public static String getCity(String ip) throws Exception{
File database = new File("E:\\IDEA\\IdeaProject\\SparkStreamingProject\\data\\GeoLite2-City\\GeoLite2-City.mmdb");
DatabaseReader reader = new DatabaseReader.Builder(database).build();
String cityName = "未知";
InetAddress ipAddress = InetAddress.getByName(ip);
CityResponse response = reader.city(ipAddress);
City city = response.getCity();
cityName = city.getNames().get("zh-CN");
return cityName;
}
/**
* Ip轉換經緯度
* @param ip
*/
public static Location getLocation(String ip) throws Exception{
File database = new File("E:\\IDEA\\IdeaProject\\SparkStreamingProject\\data\\GeoLite2-City\\GeoLite2-City.mmdb");
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName(ip);
CityResponse response = reader.city(ipAddress);
Location location = response.getLocation();
return location;
}
public static void main(String agrs[]) throws Exception {
IpUtils ipUtils = new IpUtils();
String ip = "123.125.71.38";
String city = ipUtils.getCity(ip);
System.out.println(city);
Location location = ipUtils.getLocation(ip);
System.out.println(location);
}
}
爬坑:
1、錯誤一
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Incompatible Jackson version: 2.9.5
請修改依賴的版本爲2.5.0即可。
<!--ip地址解析依賴-->
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.5.0</version>
</dependency>
2、錯誤二:
Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.node.ArrayNode.<init>(Lcom/fasterxml/jackson/databind/node/JsonNodeFactory;Ljava/util/List;)V
請調整pom.xml文件中依賴的優先級即可。(未出現問題,請略過)
其他方案請參考github:
https://github.com/maxmind/GeoIP2-java