Skip to content

Instantly share code, notes, and snippets.

@shrkw
Created August 27, 2014 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrkw/465455224fcbfc92d8d0 to your computer and use it in GitHub Desktop.
Save shrkw/465455224fcbfc92d8d0 to your computer and use it in GitHub Desktop.
MaxMindが提供しているGeoLite2をScalaから利用してみる ref: http://qiita.com/shrkw/items/3d98dd7e266932feab21
import java.io.File
import java.net.InetAddress
import com.maxmind.geoip2.DatabaseReader.Builder
import com.maxmind.geoip2.exception.AddressNotFoundException
object GeoipSample {
def main(args: Array[String]): Unit ={
val database = new File("/tmp/GeoLite2-Country.mmdb")
// This creates the DatabaseReader object, which should be reused across lookups.
val reader = new Builder(database).build()
val ipAddresses = Seq("128.101.101.101", "192.168.100.1", "183.79.135.206", "212.58.244.18")
ipAddresses foreach { ip =>
try {
println(reader.country(InetAddress.getByName(ip)))
} catch {
case e: AddressNotFoundException => println(e)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment