{ address: "CN|吉林|長(zhǎng)春|None|CERNET|1|None", content: { address: "吉林省長(zhǎng)春市", address_detail: { city: "長(zhǎng)春市", city_code: 53, district: "", province: "吉林省", street: "", street_number: "" }, point: { x: "125.31364243", y: "43.89833761" }
}, status: 0 }
https://api.map.baidu.com/highacciploc/v1?qcip=220.181.38.113&qterm=pc&ak=請(qǐng)輸入您的AK&coord=bd09ll
返回值:
{ content: { location: { lat: 40.047726,#緯度 lng: 116.313304 #經(jīng)度 }, locid: "8b1bf81d208bc2ce657fb6e6c270de66",#定位結(jié)果唯一ID radius: 30, #定位結(jié)果半徑 confidence: 1 #定位結(jié)果可信度 }, result: { error: 161,#定位結(jié)果狀態(tài)碼 loc_time: "2016-08-23 15:14:12"#定位時(shí)間 }
}
這個(gè)API也不是隨便問的,首先就需要注冊(cè);每個(gè)時(shí)間段的訪問量還有限...因此不適合做數(shù)據(jù)分析使用。因?yàn)閿?shù)據(jù)分析往往是大批量的數(shù)據(jù)同時(shí)去進(jìn)行經(jīng)緯度的轉(zhuǎn)換。
Logstash進(jìn)行轉(zhuǎn)換
Logstash本身提供了IP地址轉(zhuǎn)換成經(jīng)緯度的功能:
input{
file{
path => "D:\access.json" start_position => "beginning" }
}
filter{
json{
source => "message" }
date{
match => ["time","yyyy-MM-dd HH:mm:ss"]
timezone => "Asia/Shanghai" }
geoip {
source => "ip" target => "geoip" }
}
output{
stdout{
codec => rubydebug
}
}
MaxMind提供的GeoIp服務(wù)
東拼西湊山寨方案
這個(gè)山寨的方案靈感來源于Logstash,Logstash本身提供了IP轉(zhuǎn)換經(jīng)緯度的功能。原理就是它自己有一個(gè)IP數(shù)據(jù)庫(kù),可以通過它執(zhí)行查詢。其實(shí)這個(gè)數(shù)據(jù)庫(kù)時(shí)老版的MaxMind提供的數(shù)據(jù)文件,湊合用吧!新的需要花錢呀!
廢話不多說,在Java中想要使用這個(gè)數(shù)據(jù)文件需要下載相應(yīng)的Jar包和dat文件:
把dat文件放在自己的本地目錄,然后項(xiàng)目中導(dǎo)入geoip.jar即可:
import com.maxmind.geoip.Location; import com.maxmind.geoip.LookupService; import java.io.IOException; public class TestMain { public static void main(String[] args) { try { LookupService cl = new LookupService("D:/lib/geoip/GeoLiteCity-2013-01-18.dat", LookupService.GEOIP_MEMORY_CACHE); Location l2 = cl.getLocation("144.0.9.29"); System.out.println( "countryCode: " + l2.countryCode +"\n"+ "countryName: " + l2.countryName +"\n"+ "region: " + l2.region +"\n"+ "city: " + l2.city +"\n"+ "latitude: " + l2.latitude +"\n"+ "longitude: " + l2.longitude);
} catch (IOException e) {
e.printStackTrace();
}
}
}
輸出內(nèi)容:
countryCode: CN countryName: China region: 25 city: Jinan latitude: 36.668304 longitude: 116.99719
最后曬一個(gè)圖,興奮一下