package com.kuaifa.web.es;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ElasticSearchDruidDataSourceFactory;
import com.kuaifa.web.profile.PathUtil;
import com.kuaifa.web.profile.PropertiesSrv;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
public class ESSqlConnPool {
private static final Logger logger = LogManager.getLogger(ESSqlConnPool.class);
private static final String elasticSearchPropertyFile = "es.properties";
private static DruidDataSource dataSource = null;
public ESSqlConnPool() {
init();
}
public void init() {
try {
String filePathAndName = PathUtil.getFilePathFromConf(elasticSearchPropertyFile);
Properties properties = PropertiesSrv.getPropertyFile(filePathAndName);
System.setProperty("es.set.netty.runtime.available.processors", "false");
dataSource = (DruidDataSource) ElasticSearchDruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
logger.error("", e);
}
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void close(Connection conn, PreparedStatement ps) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
es.properties 配置文件信息如下:
url=jdbc:elasticsearch://xxxxxxx:9300/
maxActive = 300 initialSize = 2 maxWait = 8000
elasticsearch-sql-6.2.4.0.jar 下载地址
链接: https://pan.baidu.com/s/1ga-LC0Cd5rMSZUiXgxM2uA 密码: 32bd