一、IP转化工具介绍
1、将数据集ip2region.db导入到工程下的dataset目录
2、导入依赖
org.lionsoul
ip2region
1.7.2
2.2.2、使用
@Test
def ip2region(): Unit = {
val searcher = new DbSearcher(new DbConfig(), "dataset/ip2region.db")
val region = searcher.btreeSearch("61.152.197.181").getRegion
println(region)
// 国家|区域|省份|城市|ISP_
}
1.3、GeoLite使用
1.3.1、依赖包
com.maxmind.geoip2
geoip2
2.13.1
1.3.2、使用
@Test
/**
* IP -> longtitude,latitude
*/
def ip2Location(): Unit = {
import com.maxmind.geoip2.DatabaseReader
import com.maxmind.geoip2.model.CityResponse
import com.maxmind.geoip2.record.City
import com.maxmind.geoip2.record.Postal
import com.maxmind.geoip2.record.Subdivision
import java.net.InetAddress
val database = new File("dataset/GeoLite2-City.mmdb")
val reader: DatabaseReader = new DatabaseReader.Builder(database).build
val ipAddress = InetAddress.getByName("128.101.101.101")
val response = reader.city(ipAddress)
val country = response.getCountry
System.out.println(country.getIsoCode)
System.out.println(country.getName)
System.out.println(country.getNames.get("zh-CN"))
val subdivision = response.getMostSpecificSubdivision
System.out.println(subdivision.getName)
System.out.println(subdivision.getIsoCode)
val city = response.getCity
System.out.println(city.getName)
val postal = response.getPostal
System.out.println(postal.getCode)
val location = response.getLocation
System.out.println(location.getLatitude)
System.out.println(location.getLongitude)
}