这几天在做ip统一解析的事情,由于原来的库不支持ipv6,ipv4调整成ipv6用了maxmind解析。
https://github.com/maxmind/MaxMind-DB-Reader-java#gradle
按照官方文档是先导入maven包
<dependency>
<groupId>com.maxmind.db</groupId>
<artifactId>maxmind-db</artifactId>
<version>1.2.2</version>
</dependency>
然后解析
import java.io.*;
import java.net.InetAddress;
import com.fasterxml.jackson.databind.JsonNode;
import com.maxmind.db.Reader;
public class IP {
public static void main(String[] args) {
File database = new File("/Users/jls/Working/****/down_load_179_251/ip.mmdb");
try{
Reader reader = new Reader(database);
InetAddress address = InetAddress.getByName("24.24.24.24");
JsonNode response = reader.get(address);
System.out.println(response);
reader.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
结果报错 如下
Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.node.ArrayNode.<init>(Lcom/fasterxml/jackson/databind/node/JsonNodeFactory;Ljava/util/List;)V
at com.maxmind.db.Decoder.decodeArray(Decoder.java:272)
at com.maxmind.db.Decoder.decodeByType(Decoder.java:156)
at com.maxmind.db.Decoder.decode(Decoder.java:147)
at com.maxmind.db.Decoder.decodeMap(Decoder.java:281)
at com.maxmind.db.Decoder.decodeByType(Decoder.java:154)
at com.maxmind.db.Decoder.decode(Decoder.java:147)
at com.maxmind.db.Decoder.decode(Decoder.java:87)
at com.maxmind.db.Reader.<init>(Reader.java:132)
at com.maxmind.db.Reader.<init>(Reader.java:116)
at com.maxmind.db.Reader.<init>(Reader.java:66)
at com.maxmind.db.Reader.<init>(Reader.java:53)
网上搜索说是与hive版本冲突。
详见 https://blog.csdn.net/weixin_30530939/article/details/98170016
根据错误信息,定位到fasterxml的兼容出现故障,最后追综到hive2.3.0版本中avatica-1.8.0.jar居然包含了com.fasterxml.jackson包。
对应的jackson-datamind的版本为2.6.3,而我们要用要的geoip2依赖jackson-datamind的版本为2.9.3,这两个版本存在上面报错信息地方的方法的不同。
于是我试了下1.0.0
版本的可以用。
详见https://blog.csdn.net/u010521842/article/details/78118551
import com.fasterxml.jackson.databind.JsonNode;
import java.io.File;
import com.maxmind.db.Reader;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
public class IP {
private static Reader reader = null;
private static Map<String, String> cache = new HashMap<String, String>();
static {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResource("mmdb_ip/ip_tmp.mmdb").openStream();
reader = new Reader(in);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
File database = new File("resources/ip_tmp.mmdb");
//ipdbex.mmdb
//ipdbex_europa.mmdb
try{
System.out.println(database.getName());
// Reader reader = new Reader(database);
InetAddress address = InetAddress.getByName("223.104.14.37");
JsonNode response = reader.get(address);
System.out.println(response);
reader.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
import com.fasterxml.jackson.databind.JsonNode;
import com.maxmind.db.Reader;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
public class GeoLiteUtil {
private static Reader reader = null;
private static Map<String, String> cache = new HashMap<String, String>();
static {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResource("mmdb_ip/ip_tmp.mmdb").openStream();
reader = new Reader(in);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]){
System.out.println("======106.2.124.164====");
System.out.println(getCountry("106.2.124.164"));
System.out.println(getProvince("106.2.124.164"));
System.out.println(getCity("106.2.124.164"));
System.out.println("=====240e:bc:1c90:cc00:e8f9:4db0:2f82:c4db=====");
System.out.println(getCountry("240e:bc:1c90:cc00:e8f9:4db0:2f82:c4db"));
System.out.println(getProvince("240e:bc:1c90:cc00:e8f9:4db0:2f82:c4db"));
System.out.println(getCity("240e:bc:1c90:cc00:e8f9:4db0:2f82:c4db"));
// System.out.println(getCounty("240e:bc:1c90:cc00:e8f9:4db0:2f82:c4db"));
System.out.println(getCountry("74.125.180.0"));
System.out.println(getProvince("74.125.180.0"));
}
/**
* 获取条目
*
* @param ip
* @return
*/
public static String getEntry(String ip) {
String entry = cache.get(ip);
try {
if (entry == null) {
JsonNode node = reader.get(InetAddress.getByName(ip));
if (node == null) {
return "未知国家,未知省份";
}
String country = node.get("country").get("names").get("zh_CN").textValue();
String province = node.get("subdivisions").get("names").get("zh_CN").textValue();
String city = node.get("city").get("names").get("zh_CN").textValue();
String county = node.get("county").get("names").get("zh_CN").textValue();
entry = country+ "," + province+","+city+","+county;
cache.put(ip, entry);
}
} catch (IOException e) {
// System.out.println(ip + " = null");
}
return entry;
}
public static String getCountry(String ip) {
String entry = getEntry(ip);
if (entry != null) {
return entry.split(",")[0];
}
return null;
}
public static String getProvince(String ip) {
String entry = getEntry(ip);
if (entry != null) {
return entry.split(",")[1];
}
return null;
}
public static String getCity(String ip) {
String entry = getEntry(ip);
if (entry != null) {
return entry.split(",")[2];
}
return null;
}
public static String getCounty(String ip) {
String entry = getEntry(ip);
if (entry != null) {
return entry.split(",")[3];
}
return null;
}
}
没有评论