MaxMind从IP地址获取Lat / Lng,InvalidDatabaseException错误

时间:2015-03-03 20:39:14

标签: java geolocation maxmind

我的最终目标是获取与提供的IP地址相关联的纬度,经度和国家/地区。我正在使用java和MaxMind GeoIP2 Java API。我首选来使用他们的webapi,因为我需要快速转换时间。

当我调用与MaxMind api调用绑定的函数(错误后显示的代码)时,有人能告诉我为什么会出现此错误吗?

我不确定MaxMind DB元数据标记是什么。它抱怨我抓住了MaxMind网站的文件。我认为它是一个有效的MaxMind DB文件。

com.maxmind.db.InvalidDatabaseException: Could not find a MaxMind DB metadata marker in this file (GeoLite2-City-Blocks-IPv6.csv). Is this a valid MaxMind DB file?
at com.maxmind.db.Reader.findMetadataStart(Reader.java:231)
at com.maxmind.db.Reader.<init>(Reader.java:82)
at com.maxmind.db.Reader.<init>(Reader.java:74)
at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:41)
at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:31)
at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:126)
at com.tutelatechnologies.tapfortap.server.maxmind.MaxMindLookUp.maxMindLookup(MaxMindLookUp.java:20)
at com.tutelatechnologies.tapfortap.server.cidlaclookup.T4TUtilitiesTest.testMaxMindLookUpTest(T4TUtilitiesTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我按照MaxMind网站上的说明和示例代码查看了数据使用部分:http://maxmind.github.io/GeoIP2-java/

我选择使用从此处获得的GeoLite2 csv dbs:http://dev.maxmind.com/geoip/geoip2/geolite2/

我的代码:

public class MaxMindLookUp {

public static final void maxMindLookup(final String ipaddress) {
    File db = new File(
            "src/main/java/com/tutelatechnologies/tapfortap/server/maxmind/GeoLite2-City-Blocks-IPv6.csv");
    try {
        DatabaseReader reader = new DatabaseReader.Builder(db).build();
        InetAddress ip = InetAddress.getByName(ipaddress);

        CityResponse response = reader.city(ip);
        Location loc = response.getLocation();

        Double lat = loc.getLatitude();
        Double lng = loc.getLongitude();
        Integer acc = loc.getAccuracyRadius();

        System.out.println("lat=" + lat + "\nlng=" + lng + "\nacc=" + acc);

    } catch (GeoIp2Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

注意:要运行上面的代码,您需要在Maven依赖项部分添加一个插件。

 <dependency>
    <groupId>com.maxmind.geoip2</groupId>
    <artifactId>geoip2</artifactId>
    <version>2.1.0</version>
</dependency>

我没有在SO上遇到这个确切错误。我已经完成了调试器,错误发生在这一行:

DatabaseReader reader = new DatabaseReader.Builder(db).build();

这让我相信我从网站上下载的数据库出了问题。

本页底部的例外标题(http://maxmind.github.io/GeoIP2-java/)提供了一个死链接'GeoIP2 Precision Web服务文档',因此没有帮助。

在他们的Java api的例外部分中似乎没有任何与此异常相关的内容:http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/

更新

根据这里的规范中的DatabaseReader.Builder()。build()描述:http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/我认为问题来自于阅读数据库。我不确定如何解决这个问题。

更新

我能够使用二进制db文件(GeoLite2-City.mmdb)使其工作。我打开这个问题,以防任何人有关于CSV db文件为什么不适合我的更多信息。

1 个答案:

答案 0 :(得分:3)

您正在使用的库用于读取GeoIP2的二进制数据库文件。 CSV文件供人们加载到数据库中(或者他们想要使用CSV的任何其他方式)。