概述
在一個(gè)項(xiàng)目需求中,我們需要實(shí)現(xiàn)輸入一個(gè)地區(qū)的火車站哮翘、汽車站或者飛機(jī)場的名字,就能返回其的所在省市信息毛秘,要實(shí)現(xiàn)這個(gè)需求我們首先就需要知道全國火車站饭寺、客運(yùn)站及飛機(jī)場的字典表,但是在網(wǎng)上找了一圈叫挟,發(fā)現(xiàn)能滿足這樣的需求還是挺少的艰匙,于是自己利用百度API的地理位置API實(shí)現(xiàn)了一下,過程如下:
過程
- 注冊(cè)成為百度API開發(fā)者
注冊(cè)地址:http://lbsyun.baidu.com/ 按照注冊(cè)流程即可抹恳,注冊(cè)完成之后會(huì)有屬于自己的ak,這個(gè)ak是我們?cè)L問API的唯一標(biāo)識(shí)
- 使用placeV2 API
開發(fā)文檔:
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-placeapi
使用示例:
http://api.map.baidu.com/place/v2/search?query=機(jī)場®ion=西安市&output=json&ak=XXXXXXXXXXXXX
Java批量獲取
@Service
public class LoadResourceDataService {
private static Logger logger = Logger.getLogger(LoadResourceDataService.class);
@Autowired
LoadResourceDataUtil loadResourceDataUtil;
@Autowired
EsSearchService service;
//API
private final String baidu_uri_prefix = "http://api.map.baidu.com/place/v2/search";
//format json
private final String baidu_uri_format = "&output=json";
//你自己的ak
private final String baidu_uri_ak = "&ak=XXXXXXXXXXXXXXXXXXX";
private static Map<String, String> baiduCityCodeMap;
private static CloseableHttpClient httpClient;
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
httpClient = HttpClients.custom().setConnectionManager(cm).build();
}
@PostConstruct
private void init() {
baiduCityCodeMap = loadResourceDataUtil.getBaiduCityCodeMap();
}
private Map<String, Object> baiduHttpRequest(String query, String region) {
String url = baidu_uri_prefix + "?query=" + query + "®ion=" + region + baidu_uri_format + baidu_uri_ak;
Map<String, Object> map = new HashMap<>();
try {
HttpGet get = new HttpGet(url);
HttpResponse response = httpClient.execute(get);
Thread.sleep(500);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
JSONObject object = JSON.parseObject(result);
List<String> list = new ArrayList<>();
JSONArray jsonArray = object.getJSONArray("results");
if (jsonArray == null) {
return map;
}
for (Object o : jsonArray) {
JSONObject jsonObject = (JSONObject) o;
if (jsonObject.getString("uid") == null) {
continue;
}
String name = jsonObject.getString("name");
//過濾一些臟數(shù)據(jù)
if (!query.equals(name) && name.length() <= 8 && !name.contains("(")) {
list.add(name);
}
}
if (list.size() > 0) {
map.put(region, list);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return map;
}
public void startTask() {
//獲取火車站的信息
fixedThreadPool.submit(() -> queryAndSave("railway_station", "火車站"));
//獲取客運(yùn)站的信息
fixedThreadPool.submit(() -> queryAndSave("transport_station", "客運(yùn)站"));
//獲取飛機(jī)場的信息
fixedThreadPool.submit(() -> queryAndSave("airport_station", "飛機(jī)場"));
}
private void queryAndSave(String index, String query) {
int i = 1;
//迭代 city map 獲取每個(gè)省市自治區(qū)的數(shù)據(jù)
for (Map.Entry<String, String> entry : baiduCityCodeMap.entrySet()) {
Map<String, Object> data = baiduHttpRequest(query, entry.getValue());
logger.info(i + ". data:" + data);
if (data != null && !data.isEmpty()) {
//這個(gè)方法用于將查詢到的數(shù)據(jù)寫入elasticsearch
service.insert(index, entry.getValue(), entry.getKey(), data);
saveLocalFile(index + ".txt", JSON.toJSONString(data));
}
i++;
}
}
//用來將數(shù)據(jù)寫入本地文件
private void saveLocalFile(String fileName, String content) {
FileWriter fw = null;
try {
String path = LoadResourceDataService.class.getClassLoader().getResource("").getPath();
File file = new File(path + "\\" + fileName);
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file, true);
fw.write(content + "\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
}
}
}
}
Util類员凝,用來初始化百度cityCodeMap(在開發(fā)文檔的資源下載可下載該文檔)
@Component
public class LoadResourceDataUtil extends Constaus {
private Logger logger = Logger.getLogger(LoadResourceDataUtil.class);
private Map<String, String> baiduCityCodeMap;
public Map<String, String> getBaiduCityCodeMap() {
return baiduCityCodeMap;
}
@PostConstruct
public void initRegionData() {
try {
baiduCityCodeMap = loadCityCode("BaiduMap_cityCode.txt");
logger.info("init baidu city code success!");
} catch (IOException e) {
logger.error("init region error,", e);
}
}
private Map<String, String> loadCityCode(String fileName) throws IOException {
File file = new File(this.getClass().getClassLoader().getResource(fileName).getPath());
BufferedReader reader = null;
Map<String, String> map = new HashMap<>();
try {
reader = new BufferedReader(new FileReader(file));
String temp;
while ((temp = reader.readLine()) != null) {
map.put(temp.split(",")[0], temp.split(",")[1]);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return map;
}
}
BaiduMap_cityCode.txt內(nèi)容大概如下(一部分):
33,嘉峪關(guān)市
34,金昌市
35,白銀市
36,蘭州市
37,酒泉市
38,大興安嶺地區(qū)
39,黑河市
40,伊春市
41,齊齊哈爾市
42,佳木斯市
...
Controller層代碼如下:
@RequestMapping(value = "/test2", method = RequestMethod.GET)
public void test2() throws Exception {
dataService.startTask();
}
獲取火車站的數(shù)據(jù)railway_station.txt如下(部分)
{"保亭黎族苗族自治縣":["保亭車站","保亭縣幸福超市","海汽快遞","七仙美食園","紅運(yùn)來川菜館","金洲度假休閑賓館"]}
{"萬寧市":["萬寧站","神州站","和樂站","萬寧車站","萬寧興隆車站","車站永興商行","萬寧車站-西門","萬寧車站-西北門","萬寧站-進(jìn)站口"]}
{"儋州市":["儋州火車站","儋州車站","白馬井車站","銀灘站","海頭車站","車站快餐","儋州車站售票大廳","老車站停車場","恒興石材店"]}
...
獲取客運(yùn)站的數(shù)據(jù)transport_station.txt如下(部分)
{"保亭黎族苗族自治縣":["保亭汽車站"]}
{"萬寧市":["長豐汽車站","和樂客運(yùn)站","萬寧興隆車站","萬寧車站"]}
{"儋州市":["洋浦客運(yùn)站","儋州汽車站","儋州西站","海頭車站"]}
{"定安縣":["定安汽車站","客運(yùn)車上落站","定安黃竹車站"]}
{"白沙黎族自治縣":["白沙車站","海汽查苗車站"]}
{"瓊海市":["瓊海汽車站"]}
{"阿拉爾市":["阿拉爾中心客運(yùn)站"]}
{"濟(jì)源市":["邵原汽車站","北勛汽車站","承留汽車站","濟(jì)源市公交客運(yùn)站","濟(jì)源汽車客運(yùn)總站","濟(jì)源客運(yùn)西站"]}
...
獲取飛機(jī)場的數(shù)據(jù)airport_station.txt如下(部分)
{"儋州市":["西慶通用航空機(jī)場","西慶機(jī)場大門"]}
{"西沙群島":["三沙市氣象局"]}
{"瓊海市":["瓊海博鰲機(jī)場","博鰲機(jī)場進(jìn)出場路","三亞鳳凰機(jī)場貨運(yùn)"]}
{"臨高縣":["加來機(jī)場派出所","臨高星華賓館","菀茹商務(wù)酒店","臨高東盛商務(wù)賓館"]}
{"石河子市":["石河子機(jī)場"]}
{"福州市":["義序機(jī)場","機(jī)場巴士售票處","福州竹岐直升機(jī)場","機(jī)場貨運(yùn)站停車場"]}
{"惠州市":["惠州平潭機(jī)場","惠州機(jī)場","白云機(jī)場候機(jī)樓","白云機(jī)場候機(jī)樓","白云機(jī)場候機(jī)樓"]}
{"江門市":["大方航空貨運(yùn)","址山候機(jī)樓","江門候機(jī)樓"]}
{"汕頭市":["汕頭機(jī)場公司","迎賓機(jī)場路口"]}
...
百度API返回的有些數(shù)據(jù)可能不是特別精確需要自己添加過濾條件進(jìn)一步清洗。
以上奋献。
完結(jié)健霹。