This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
galaxy-tsg-olap-storm-log-s…/src/main/java/com/zdjizhi/utils/general/TransFunction.java

213 lines
6.2 KiB
Java
Raw Normal View History

2021-03-16 14:48:07 +08:00
package com.zdjizhi.utils.general;
2020-12-25 17:32:54 +08:00
2021-03-25 14:27:41 +08:00
import cn.hutool.core.codec.Base64;
2021-03-16 14:48:07 +08:00
import com.zdjizhi.common.FlowWriteConfig;
import com.zdjizhi.utils.hbase.HBaseUtils;
import com.zdjizhi.utils.json.JsonParseUtil;
2021-03-16 10:39:23 +08:00
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
2020-12-25 17:32:54 +08:00
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import com.zdjizhi.utils.Encodes;
import com.zdjizhi.utils.FormatUtils;
import com.zdjizhi.utils.IpLookup;
import com.zdjizhi.utils.StringUtil;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-12-25 17:32:54 +08:00
/**
* @author qidaijie
*/
class TransFunction {
2021-03-16 10:39:23 +08:00
2021-03-16 14:48:07 +08:00
private static final Log logger = LogFactory.get();
2021-03-16 10:39:23 +08:00
2021-03-16 14:48:07 +08:00
private static final Pattern PATTERN = Pattern.compile("[0-9]*");
2020-12-25 17:32:54 +08:00
/**
* 生成当前时间戳的操作
*/
static long getCurrentTime() {
return System.currentTimeMillis() / 1000;
}
/**
* 根据clientIp获取location信息
*
* @param ip client IP
* @return ip地址详细信息
*/
static String getGeoIpDetail(IpLookup ipLookup, String ip) {
return ipLookup.cityLookupDetail(ip);
}
/**
* 根据ip获取asn信息
*
* @param ip client/server IP
* @return ASN
*/
static String getGeoAsn(IpLookup ipLookup, String ip) {
return ipLookup.asnLookup(ip);
}
/**
* 根据ip获取country信息
*
* @param ip server IP
* @return 国家
*/
static String getGeoIpCountry(IpLookup ipLookup, String ip) {
return ipLookup.countryLookup(ip);
}
/**
* radius借助HBase补齐
*
* @param ip client IP
* @return account
*/
static String radiusMatch(String ip) {
String account = HBaseUtils.getAccount(ip.trim());
if (StringUtil.isBlank(account)) {
2021-03-25 14:27:41 +08:00
logger.warn("HashMap get account is null, Ip is :{}", ip);
2020-12-25 17:32:54 +08:00
}
return account;
}
/**
* 解析顶级域名
*
* @param domain 初始域名
* @return 顶级域名
*/
static String getTopDomain(String domain) {
try {
return FormatUtils.getTopPrivateDomain(domain);
} catch (StringIndexOutOfBoundsException outException) {
2021-03-25 14:27:41 +08:00
logger.error("解析顶级域名异常,异常域名:{}" + domain);
2020-12-25 17:32:54 +08:00
return "";
}
}
/**
* 根据编码解码base64
*
* @param message base64
* @param charset 编码
* @return 解码字符串
*/
static String decodeBase64(String message, String charset) {
String result = "";
try {
if (StringUtil.isNotBlank(message)) {
if (StringUtil.isNotBlank(charset)) {
2021-03-25 14:27:41 +08:00
result = Base64.decodeStr(message, charset);
2020-12-25 17:32:54 +08:00
} else {
2021-03-25 14:27:41 +08:00
result = Base64.decodeStr(message, FlowWriteConfig.MAIL_DEFAULT_CHARSET);
2020-12-25 17:32:54 +08:00
}
}
2021-03-25 14:27:41 +08:00
} catch (RuntimeException rune) {
logger.error("解析 Base64 异常,异常信息:" + rune);
2020-12-25 17:32:54 +08:00
}
return result;
}
/**
* 根据表达式解析json
*
* @param message json
* @param expr 解析表达式
* @return 解析结果
*/
static String flattenSpec(String message, String expr) {
String flattenResult = "";
try {
if (StringUtil.isNotBlank(expr)) {
ArrayList<String> read = JsonPath.parse(message).read(expr);
flattenResult = read.get(0);
}
} catch (ClassCastException | InvalidPathException e) {
2021-03-25 14:27:41 +08:00
logger.error("设备标签解析异常,[ " + expr + " ]解析表达式错误" + e);
2020-12-25 17:32:54 +08:00
}
return flattenResult;
}
/**
* 判断是否为日志字段,是则返回对应value否则返回原始字符串
*
* @param object 内存实体类
* @param param 字段名/普通字符串
* @return JSON.Value or String
*/
static String isJsonValue(Object object, String param) {
if (param.contains(FlowWriteConfig.IS_JSON_KEY_TAG)) {
Object value = JsonParseUtil.getValue(object, param.substring(2));
if (value != null) {
return value.toString();
} else {
return "";
}
} else {
return param;
}
}
/**
* IF函数实现解析日志构建三目运算;包含判断是否为数字若为数字则转换为long类型返回结果
2020-12-25 17:32:54 +08:00
*
* @param object 内存实体类
* @param ifParam 字段名/普通字符串
* @return resultA or resultB or ""
*/
static Object condition(Object object, String ifParam) {
2020-12-25 17:32:54 +08:00
try {
String[] split = ifParam.split(FlowWriteConfig.FORMAT_SPLITTER);
String[] norms = split[0].split(FlowWriteConfig.IF_CONDITION_SPLITTER);
String direction = isJsonValue(object, norms[0]);
if (StringUtil.isNotBlank(direction)) {
if (split.length == FlowWriteConfig.IF_PARAM_LENGTH) {
String resultA = isJsonValue(object, split[1]);
String resultB = isJsonValue(object, split[2]);
String result = (Integer.parseInt(direction) == Integer.parseInt(norms[1])) ? resultA : resultB;
2021-03-16 14:48:07 +08:00
Matcher isNum = PATTERN.matcher(result);
if (isNum.matches()) {
return Long.parseLong(result);
} else {
return result;
}
2020-12-25 17:32:54 +08:00
}
}
2021-03-25 14:27:41 +08:00
} catch (RuntimeException e) {
logger.error("IF 函数执行异常,异常信息:" + e);
2020-12-25 17:32:54 +08:00
}
return null;
2020-12-25 17:32:54 +08:00
}
2021-02-01 11:05:02 +08:00
/**
* 设置固定值函数 若为数字则转为long返回
2021-03-16 10:39:23 +08:00
*
2021-02-01 11:05:02 +08:00
* @param param 默认值
* @return 返回数字或字符串
*/
static Object setValue(String param) {
try {
2021-03-16 14:48:07 +08:00
Matcher isNum = PATTERN.matcher(param);
2021-02-01 11:05:02 +08:00
if (isNum.matches()) {
return Long.parseLong(param);
} else {
return param;
}
2021-03-25 14:27:41 +08:00
} catch (RuntimeException e) {
logger.error("SetValue 函数异常,异常信息:" + e);
2021-02-01 11:05:02 +08:00
}
return null;
}
2020-12-25 17:32:54 +08:00
}