本次实例软件相关版本
Groovy: 3.0.6
ElasticSearch: 7.9.3
kibana: 7.9.3
第三方地图Api: 腾讯地图 (第三方地图对比)
索引字段约定
- 请求日志索引
PUT /request_log-web
{
"mappings":{
"properties":{
"location":{
"type":"object",
"properties":{
"gps":{
"type":"geo_point",
"ignore_malformed":true
},
"desc":{
"type":"keyword",
"fields":{
"wildcard":{
"type":"wildcard"
}
}
},
"ad_info":{
"type":"object",
"properties":{
"nation":{
"type":"keyword"
},
"province":{
"type":"keyword"
},
"city":{
"type":"keyword"
},
"district":{
"type":"keyword"
},
"adcode":{
"type":"keyword"
}
}
}
}
},
"req_ip":{
"type":"ip"
}
}
}
}
- 存储ip地址索引
PUT /ip_address
{
"mappings": {
"properties": {
"ip": {
"type": "ip"
},
"createTime": {
"type": "date"
},
"location": {
"properties": {
"ad_info": {
"properties": {
"adcode": {
"type": "keyword"
},
"city": {
"type": "keyword"
},
"district": {
"type": "keyword"
},
"nation": {
"type": "keyword"
},
"province": {
"type": "keyword"
}
}
},
"gps": {
"type": "geo_point",
"ignore_malformed": true
}
}
},
"message": {
"type": "keyword"
},
"status": {
"type": "keyword"
}
}
}
}
groovy脚本
import groovy.json.JsonOutput
import groovy.json.JsonSlurperClassic
import groovy.transform.Field;
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection
class UpdateGps{
/**
*
* @param bulk_count_aggrs 单次聚合ip查询数量单个查询数量
* @param bulk_count_ip_inser 批量插入ip数量
* @param count_for_max 最大循环次数
* @param es_addr es地址
* @param logIndex 日志的索引
* @param ipIndex 存储ip的索引
* @param api_keys 腾讯地址api key配置
*/
public UpdateGps(int bulk_count_aggrs=1000,int bulk_count_ip_insert=1000,int count_for_max=10,
String es_addr="http://192.168.0.202:9200",
String logIndex="request_log-*",
String ipIndex="ip_address",
List<String> api_keys
){
this.bulk_count_aggrs=bulk_count_aggrs;
this.bulk_count_ip_insert=bulk_count_ip_insert;
this.count_for_max=count_for_max;
this.es_addr=es_addr;
this.ipIndex=ipIndex;
this.logIndex=logIndex;
this.api_keys=api_keys;
}
//腾讯地址api key配置
List<String> api_keys;
//工具配置
static JsonSlurperClassic jsonSlurper = new JsonSlurperClassic()
static JsonOutput jsonOutput = new JsonOutput()
static String rowDateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
static boolean debugInfo=true; //info日志开关
static boolean debugNet=false; //网络日志开关
//elasticsearch 配置
static es_addr="http://192.168.0.202:9200"
static String ipIndex="ip_address"; //ip信息
static String logIndex="request_log-*"; //日志索引
int bulk_count_aggrs=1000; //单次聚合ip查询数量单个查询数量
int bulk_count_ip_insert=1000; //批量插入ip数量
int count_for_max=1; //最大循环次数
int unknow_ip_size=0;
int count_for_cur=1;
String startTime=new Date().format("yyyy-MM-dd HH:mm:ss.SSS");
int total_ip_insert=0; //总计新增ip个数
int total_ip_update=0; //总计更新ip个数
int total_log_update=0; //总计更新行数
int total_unknow_ip=0; //总计未知ip
/**
* 入口方法
* @return
*/
int unknow_ip_size_temp