使用Groovy调用第三方地图api更新ElasticSearch日志的ip定位信息

使用Groovy调用腾讯地图api更新ElasticSearch日志的ip定位信息

本次实例软件相关版本
Groovy: 3.0.6
ElasticSearch: 7.9.3
kibana: 7.9.3
第三方地图Api: 腾讯地图 (第三方地图对比)

索引字段约定

  1. 请求日志索引
 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"
            }
        }
    }
}
  1. 存储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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值