將ip轉換爲地理位置存入elasticsearch

有些需要將一些數據基於地址位置進行分析,比如哪些區域比較活躍,在什麼時間範圍內活躍,但是後端僅能獲取ip地址,因此需要將ip地址轉換爲地理位置,幸運的是我們有開源的工具可以使用,maxmind/GeoIP2-java使用GeoLite2-City.mmdb庫就可以由ip分析得到對應的經緯度,下面給出具體操作步驟:

1、從https://dev.maxmind.com/geoip/geoip2/geolite2/下載免費的GeoLite2-City庫,但是準確度不如收費的geoip2-city庫

2、創建elasticsearch索引

3、更新映射

 

4、將數據插入到elasticsearch,ip解析經緯度參考https://github.com/maxmind/GeoIP2-java

     //使用RestHighLevelClient BulkRequest批量插入數據
     @Test
	public void createSubBankIndex(){
    	List<LogDetail>  details = logDetailMapper.getLogDetails();
    	
    	//bulkRequest.timeout("2m");
		DatabaseReader reader = null;
		try {
			Resource resource = new ClassPathResource("GeoLite2-City.mmdb");
			InputStream file = resource.getInputStream();				
			reader = new DatabaseReader.Builder(file).build();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(null == reader)
			return;
    	for(int i=0,len=details.size();i<len;){
    		int sz = len - i > 2000?2000: len - i;
    		List<LogDetail> subBanks = details.subList(i, sz+i);
    		i += sz;
        	BulkRequest bulkRequest = new BulkRequest("log_detail1104", "log_detail");
        	bulkRequest.timeout(TimeValue.timeValueMinutes(2));   
        	IndexRequest indexRequest = new IndexRequest("log_detail1104", "log_detail");
        	GeoLocation loc = new GeoLocation();
    		for(LogDetail b:subBanks){
    			GeoPoint gp = ip2Geo(reader,b.getIpAddress());
    			loc.setLat(gp.getLat());
    			loc.setLon(gp.getLon());
    			b.setLocation(loc);
    			//indexRequest.timeout(TimeValue.timeValueMinutes(2)); 
    			indexRequest.source(JSON.toJSONString(b), XContentType.JSON);
    			bulkRequest.add(indexRequest);			
    		}
    		
    		try {
    			highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    			//highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}	    		
    	}	
	}


    //將ip轉換爲地理位置信息
	private GeoPoint ip2Geo(DatabaseReader reader,String ip){
		try {
			InetAddress ipAddress = InetAddress.getByName(ip);

			// Replace "city" with the appropriate method for your database, e.g.,
			// "country".
			CityResponse response = reader.city(ipAddress);		


			Country country = response.getCountry();
			//System.out.println(country.getIsoCode());            // 'US'
			//System.out.println(country.getName());               // 'United States'
			System.out.println(country.getNames().get("zh-CN")); // '美國'

			Subdivision subdivision = response.getMostSpecificSubdivision();
			System.out.println(subdivision.getName());    // 'Minnesota'
			//System.out.println(subdivision.getIsoCode()); // 'MN'

			City city = response.getCity();
			System.out.println(city.getName()); // 'Minneapolis'

			Postal postal = response.getPostal();
			//System.out.println(postal.getCode()); // '55455'

			Location location = response.getLocation();
			System.out.println(location.getLatitude());  // 44.9733
			System.out.println(location.getLongitude()); // -93.2323	
			
			GeoPoint gp = new GeoPoint(location.getLatitude(),location.getLongitude());
			return gp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

5、運行後效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章