mardi 3 mars 2015

MaxMind Get Lat/Lng from IP address, InvalidDatabaseException Error



My end goal is to obtain the latitude, longitude and country associated with a provided ip address. I am using java and the MaxMind GeoIP2 Java API. My preference is not to use their webapi as I need quick turn around time.


Can anyone tell me why I am getting this error when I call my function that ties in the MaxMind api call (code shown after the error)?


I am not sure what a MaxMind DB metadata marker is. It is complaining about the file that I grabbed off of the MaxMind site. I would assume it's a valid MaxMind DB file.



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)


I followed the instructions and sample code on the MaxMind site here for the Data Usage section: http://ift.tt/1oL2DDv


I have chosen to use a GeoLite2 csv dbs obtained from here: http://ift.tt/18lOww8


My code:



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();
}

}


Note: in order to run the code above you need to add a plugin to your Maven Dependencies section.



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


I didn't come across much for this exact error on SO. I have stepped through the debugger and the error happens on this line:



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


This leads me to believe something is wrong with the db that I downloaded off the site.


The Exceptions heading at the bottom of this page (http://ift.tt/1oL2DDv) provides a dead link 'GeoIP2 Precision web service documentation' so that's no help.


There doesn't seem to be anything related to this exception in the Exceptions portion of their Java api: http://ift.tt/1AE0iLn


UPDATE


According to the DatabaseReader.Builder().build() description in the specs here: http://ift.tt/1AE0iLn I think the issue comes from reading the db. I am not sure yet how to solve this issue.


UPDATE I was able to get it working using the binary db files. I am leaving this question open in case anyone has any more information about why the CSV db files did not work for me.




Aucun commentaire:

Enregistrer un commentaire