Merge branch 'feature-181031' into develop
This commit is contained in:
@@ -1409,7 +1409,7 @@
|
||||
<result column="found_Time" jdbcType="TIMESTAMP" property="foundTime" />
|
||||
<result column="recv_Time" jdbcType="TIMESTAMP" property="recvTime" />
|
||||
<result column="cap_ip" jdbcType="VARCHAR" property="capIp" />
|
||||
<result column="voip_rotocol" jdbcType="VARCHAR" property="voipProtocol" />
|
||||
<result column="voip_protocol" jdbcType="VARCHAR" property="voipProtocol" />
|
||||
<result column="rtp_d_ip" jdbcType="VARCHAR" property="rtpDIp" />
|
||||
<result column="rtp_s_ip" jdbcType="VARCHAR" property="rtpSIp" />
|
||||
<result column="rtp_d_port" jdbcType="VARCHAR" property="rtpDPort" />
|
||||
|
||||
272
src/main/java/com/nis/web/dao/impl/LogJDBCByDruid.java
Normal file
272
src/main/java/com/nis/web/dao/impl/LogJDBCByDruid.java
Normal file
@@ -0,0 +1,272 @@
|
||||
package com.nis.web.dao.impl;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.ibatis.mapping.ResultMap;
|
||||
import org.apache.ibatis.mapping.ResultMapping;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.nis.domain.Page;
|
||||
import com.nis.util.Constants;
|
||||
import com.nis.web.service.SpringContextHolder;
|
||||
import com.zdjizhi.utils.StringUtil;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>
|
||||
* Title: LogJDBCByDruid
|
||||
* </p>
|
||||
* <p>
|
||||
* Description: 使用druid连接池对hive或clickhouse进行查询并解析结果
|
||||
* </p>
|
||||
* <p>
|
||||
* Company: IIE
|
||||
* </p>
|
||||
*
|
||||
* @author rkg
|
||||
* @date 2018年8月20日
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
public class LogJDBCByDruid {
|
||||
private final static Logger logger = LoggerFactory.getLogger(LogJDBCByDruid.class);
|
||||
static DruidDataSource datasource = null;
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
Statement st = null;
|
||||
|
||||
private static Connection getConnection() throws SQLException {
|
||||
if (datasource == null) {
|
||||
if (Constants.ISUSECLICKHOUSE) {
|
||||
datasource = (DruidDataSource) SpringContextHolder.getBean("ClickHouseDataSourceByDruid");
|
||||
} else {
|
||||
datasource = (DruidDataSource) SpringContextHolder.getBean("HiveDataSourceByDruid");
|
||||
}
|
||||
}
|
||||
return datasource.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据sql从数据中心中获取日志并set到list中
|
||||
*
|
||||
* @param page
|
||||
* @param sql
|
||||
* @param entityClass
|
||||
* @throws Exception
|
||||
*/
|
||||
public <T> void getTableData(Page<T> page, String sql, Class<?> entityClass) throws Exception {
|
||||
List<T> listObject = new ArrayList<T>();
|
||||
try {
|
||||
Map<String, String> filedAndColumnMap = getColumn2FiledMap(entityClass);
|
||||
// 不从Object... obj中获取需要date类型的字段了,调用的时候容易漏写,改为反射获取date类型的字段
|
||||
List<String> columnList = getDateColumn(entityClass);
|
||||
conn = getConnection();
|
||||
logger.info("连接数据中心日志库成功--------------------------");
|
||||
st = conn.createStatement();
|
||||
logger.info("开始执行日志查询语句sql={}", sql);
|
||||
rs = st.executeQuery(sql);
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
while (rs.next()) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
Object value = rs.getObject(i);
|
||||
String filedName = filedAndColumnMap.get(metaData.getColumnName(i).toString().toLowerCase());
|
||||
if (!StringUtil.isEmpty(value)) {
|
||||
// 如果是日期类型的属性需要把时间戳转换成日期,如果时间戳为0直接把值设置为null
|
||||
if (null != columnList && columnList.contains(filedName.toLowerCase())) {
|
||||
long time = 0L;
|
||||
time = Long.parseLong(value.toString());
|
||||
map.put(filedName, time == 0L ? null : new Date(time * 1000));
|
||||
} else {
|
||||
map.put(filedName, value);
|
||||
}
|
||||
} else {
|
||||
map.put(filedName, null);
|
||||
}
|
||||
}
|
||||
listObject.add((T) map2Obj(map, entityClass));
|
||||
}
|
||||
if (null == listObject || listObject.size() == 0) {
|
||||
page.setList(new ArrayList());
|
||||
} else {
|
||||
page.setList(listObject);
|
||||
}
|
||||
logger.info("执行日志查询语句成功,sql={}", sql);
|
||||
} finally {
|
||||
closeConn();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭数据库连接
|
||||
*/
|
||||
private void closeConn() {
|
||||
try {
|
||||
if (rs != null) {
|
||||
rs.close();
|
||||
rs = null;
|
||||
}
|
||||
if (st != null) {
|
||||
st.close();
|
||||
st = null;
|
||||
}
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
conn = null;
|
||||
}
|
||||
logger.info("关闭数据中心连接成功");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.error("关闭数据中心连接失败,失败原因" + e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 反射获取类中date类型的字段名称
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static List<String> getDateColumn(Class<?> type) throws Exception {
|
||||
List<String> columnList = new ArrayList<String>();
|
||||
BeanInfo beanInfo = Introspector.getBeanInfo(type);
|
||||
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
||||
for (int i = 0; i < propertyDescriptors.length; i++) {
|
||||
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
|
||||
String name = propertyDescriptor.getName();
|
||||
String fieldTypeName = propertyDescriptor.getPropertyType().getName();
|
||||
if (fieldTypeName.equals("java.util.Date")) {
|
||||
columnList.add(name.toLowerCase());
|
||||
}
|
||||
}
|
||||
return columnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将map中的数据利用反射set到Class中,并返回设置好值的对象
|
||||
*
|
||||
* @param map
|
||||
* @param beanClass
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static Object map2Obj(Map<String, Object> map, Class<?> beanClass) throws Exception {
|
||||
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
|
||||
Object obj = beanClass.newInstance();
|
||||
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
||||
for (int i = 0; i < propertyDescriptors.length; i++) {
|
||||
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
|
||||
String name = propertyDescriptor.getName();
|
||||
String fieldTypeName = propertyDescriptor.getPropertyType().getName();
|
||||
if (map.containsKey(name)) {
|
||||
Object value = map.get(name);
|
||||
if (!StringUtil.isEmpty(value)) {
|
||||
if (fieldTypeName.equals("java.lang.String")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, value.toString());
|
||||
} else if (fieldTypeName.equals("java.lang.Integer")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, Integer.parseInt(value.toString()));
|
||||
} else if (fieldTypeName.equals("java.lang.Long")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, Long.parseLong(value.toString()));
|
||||
} else if (fieldTypeName.equals("java.lang.Boolean")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, Boolean.parseBoolean(value.toString()));
|
||||
} else if (fieldTypeName.equals("java.lang.Character")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, value.toString().toCharArray());
|
||||
} else if (fieldTypeName.equals("java.lang.Byte")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, value.toString().getBytes());
|
||||
} else if (fieldTypeName.equals("java.lang.Short")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, Short.parseShort(value.toString()));
|
||||
} else if (fieldTypeName.equals("java.lang.Float")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, Float.parseFloat(value.toString()));
|
||||
} else if (fieldTypeName.equals("java.lang.Double")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, Double.parseDouble(value.toString()));
|
||||
} else if (fieldTypeName.equals("java.math.BigDecimal")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj,
|
||||
BigDecimal.valueOf(Long.parseLong(value.toString())));
|
||||
} else if (fieldTypeName.equals("java.util.Date")) {
|
||||
propertyDescriptor.getWriteMethod().invoke(obj, (Date) value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据class从DfLogSearchDao.xml中获取对应的resultMap里column与property的关系,key是column
|
||||
*
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
private static Map<String, String> getColumn2FiledMap(Class<?> clazz) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
SqlSessionFactory sqlSessionFactory = SpringContextHolder.getBean(SqlSessionFactory.class);
|
||||
ResultMap resultMap = sqlSessionFactory.getConfiguration().getResultMap(clazz.getSimpleName() + "Map");
|
||||
List<ResultMapping> mapping = resultMap.getResultMappings();
|
||||
for (ResultMapping mapp : mapping) {
|
||||
map.put(mapp.getColumn().toLowerCase(), mapp.getProperty());
|
||||
}
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行count的sql并将结果和计算的last的值set到page对象中
|
||||
*
|
||||
* @param page
|
||||
* @param sql
|
||||
* @throws Exception
|
||||
*/
|
||||
public <T> void getCount(Page<T> page, String sql) throws Exception {
|
||||
try {
|
||||
conn = getConnection();
|
||||
logger.info("连接数据中心日志库成功--------------------------");
|
||||
st = conn.createStatement();
|
||||
logger.info("开始执行查询日志总条数sql={}", sql);
|
||||
rs = st.executeQuery(sql);
|
||||
String countStr = null;
|
||||
while (rs.next()) {
|
||||
countStr = rs.getObject(1).toString();
|
||||
break;
|
||||
}
|
||||
if (countStr == null || countStr.trim().equals("")) {
|
||||
logger.info("获取数据中心日志总条数成功,总共0条日志,sql={}", sql);
|
||||
page.setCount(0l);
|
||||
page.setLast(1);
|
||||
} else {
|
||||
Long count = Long.valueOf(countStr);
|
||||
logger.info("获取数据中心日志总条数成功,总共{}条日志,sql={}", count, sql);
|
||||
page.setCount(count);
|
||||
page.setLast(getLastPageNum(count.intValue(), page.getPageSize()));
|
||||
}
|
||||
} finally {
|
||||
closeConn();
|
||||
}
|
||||
}
|
||||
|
||||
private int getLastPageNum(int totalCount, int pageSize) {
|
||||
int pageNum = totalCount / pageSize;
|
||||
if (totalCount % pageSize > 0) {
|
||||
pageNum++;
|
||||
}
|
||||
return pageNum;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,9 +2,6 @@ package com.nis.web.service;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -14,13 +11,13 @@ import java.util.Map;
|
||||
import org.apache.ibatis.mapping.ResultMap;
|
||||
import org.apache.ibatis.mapping.ResultMapping;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.nis.domain.Page;
|
||||
import com.nis.util.Configurations;
|
||||
import com.nis.util.Constants;
|
||||
import com.nis.util.LogJDBCByDruid;
|
||||
import com.nis.web.dao.impl.LogJDBCByDruid;
|
||||
import com.zdjizhi.utils.StringUtil;
|
||||
|
||||
/**
|
||||
@@ -33,10 +30,9 @@ import com.zdjizhi.utils.StringUtil;
|
||||
public class LogDataService {
|
||||
// private final static Logger logger =
|
||||
// LoggerFactory.getLogger(LogDataService.class);
|
||||
static DruidDataSource datasource = null;
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
Statement st = null;
|
||||
|
||||
@Autowired
|
||||
private LogJDBCByDruid logJDBCByDruid;
|
||||
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
|
||||
private static Map<String, Map<String, String>> col2col = new HashMap<String, Map<String, String>>();
|
||||
@@ -189,7 +185,7 @@ public class LogDataService {
|
||||
}
|
||||
Integer startNum = (page.getPageNo() - 1) * page.getPageSize();
|
||||
StringBuffer foundTimeSql = new StringBuffer();
|
||||
foundTimeSql.append("select distinct found_time from " + tableName + " where ");
|
||||
foundTimeSql.append("select found_time from " + tableName + " where ");
|
||||
if (whereSB.length() == 0) {// 没有其他查询条件只有默认的found_time条件
|
||||
if (whereFoundTime.length() > 0) {
|
||||
int indexOf = whereFoundTime.indexOf("and") + "and".length();
|
||||
@@ -365,10 +361,11 @@ public class LogDataService {
|
||||
*/
|
||||
private <T> void searchFromDataCenter(Page<T> page, Object bean, StringBuffer selSql, StringBuffer countSql)
|
||||
throws Exception {
|
||||
new LogJDBCByDruid().getTableData(page, selSql.toString(), bean.getClass());
|
||||
// new LogJDBCByDruid().getTableData(page, selSql.toString(), bean.getClass());
|
||||
logJDBCByDruid.getTableData(page, selSql.toString(), bean.getClass());
|
||||
if (Constants.ISOPENLOGCOUNTANDLAST) {
|
||||
if (page.getList() != null && page.getList().size() > 0) {
|
||||
new LogJDBCByDruid().getCount(page, countSql.toString().toLowerCase());
|
||||
logJDBCByDruid.getCount(page, countSql.toString().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ import com.nis.domain.restful.ConfigPzIdSource;
|
||||
import com.zdjizhi.utils.StringUtil;
|
||||
import com.nis.web.dao.ConfigPzIdDao;
|
||||
import com.nis.web.service.BaseLogService;
|
||||
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.Static;
|
||||
|
||||
/**
|
||||
* @ClassName:ConfigPzIdService
|
||||
|
||||
Reference in New Issue
Block a user