Bläddra i källkod

修改源码支持mysql查询

liwh 8 månader sedan
förälder
incheckning
83ee42b3bf

+ 4 - 0
config/jdbc-reload.properties

@@ -0,0 +1,4 @@
+jdbc.url=jdbc:mysql://10.168.1.47:18080/smart-search?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
+jdbc.user=root
+jdbc.password=Taiji@2023#data
+jdbc.driver=com.mysql.cj.jdbc.Driver

+ 0 - 14
config/jdbc.yml

@@ -1,14 +0,0 @@
-## 新词mysql连接地址
-jdbc:
-  driver: com.mysql.cj.jdbc.Driver
-  url: jdbc:mysql://10.168.1.47:18080/smart-search?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
-  username: root
-  password: Taiji@2023#data
-
-
-## redis 连接地址
-redis:
-  host: 10.168.1.47
-  port: 18084
-  timeout: 5000
-  password: Taiji@2024#data

+ 0 - 6
pom.xml

@@ -90,12 +90,6 @@
         </dependency>
 
         <dependency>
-            <groupId>org.redisson</groupId>
-            <artifactId>redisson</artifactId>
-            <version>3.11.5</version>
-        </dependency>
-
-        <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
             <version>4.5.2</version>

+ 0 - 8
src/main/assemblies/plugin.xml

@@ -49,13 +49,5 @@
                 <include>mysql:mysql-connector-java</include>
             </includes>
         </dependencySet>
-        <dependencySet>
-            <outputDirectory/>
-            <useProjectArtifact>true</useProjectArtifact>
-            <useTransitiveFiltering>true</useTransitiveFiltering>
-            <includes>
-                <include>org.redisson:redisson</include>
-            </includes>
-        </dependencySet>
     </dependencySets>
 </assembly>

+ 24 - 141
src/main/java/org/wltea/analyzer/dic/DatabaseMonitor.java

@@ -2,12 +2,7 @@ package org.wltea.analyzer.dic;
 
 import org.apache.logging.log4j.Logger;
 import org.elasticsearch.SpecialPermission;
-import org.redisson.api.RBucket;
-import org.redisson.api.RedissonClient;
 import org.wltea.analyzer.help.ESPluginLoggerFactory;
-import org.wltea.analyzer.util.RedisUtils;
-
-import java.net.InetAddress;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.sql.*;
@@ -22,41 +17,25 @@ public class DatabaseMonitor implements Runnable {
 
     private static final Logger logger = ESPluginLoggerFactory.getLogger(DatabaseMonitor.class.getName());
 
-    public static final String PATH_JDBC_YML = "jdbc.yml";
-
     private static final String JDBC_URL = "jdbc.url";
     private static final String JDBC_USERNAME = "jdbc.username";
     private static final String JDBC_PASSWORD = "jdbc.password";
     private static final String JDBC_DRIVER = "jdbc.driver";
 
-    private static final String REDIS_HOST = "redis.host";
-    private static final String REDIS_PORT = "redis.port";
-    private static final String REDIS_TIMEOUT = "redis.timeout";
-    private static final String REDIS_PASSWORD = "redis.password";
+    private Connection connection = null;
 
-    private Properties propsRedis = new Properties();
+    private Statement statement = null;
 
+    private Properties dbProps = null;
 
-    public String getUrl() {
-        return Dictionary.getSingleton().getProperty(JDBC_URL);
-    }
-
-    public String getUsername() {
-        return Dictionary.getSingleton().getProperty(JDBC_USERNAME);
-    }
-
-    public String getPassword() {
-        return Dictionary.getSingleton().getProperty(JDBC_PASSWORD);
-    }
-
-    public String getDriver() {
-        return Dictionary.getSingleton().getProperty(JDBC_DRIVER);
-    }
+    private Long lastModified =0L;
 
     /**
      * 加载MySQL驱动
      */
-    public DatabaseMonitor() {
+    public DatabaseMonitor(Properties dbProps) {
+
+        this.dbProps = dbProps;
 
         SpecialPermission.check();
 
@@ -64,12 +43,7 @@ public class DatabaseMonitor implements Runnable {
 
             try {
 
-                Class.forName(getDriver());
-
-                propsRedis.put(REDIS_HOST, Dictionary.getSingleton().getProperty(REDIS_HOST));
-                propsRedis.put(REDIS_PORT, Dictionary.getSingleton().getProperty(REDIS_PORT));
-                propsRedis.put(REDIS_TIMEOUT, Dictionary.getSingleton().getProperty(REDIS_TIMEOUT));
-                propsRedis.put(REDIS_PASSWORD, Dictionary.getSingleton().getProperty(REDIS_PASSWORD));
+                Class.forName(dbProps.getProperty(JDBC_DRIVER));
 
             } catch (ClassNotFoundException e) {
 
@@ -100,21 +74,26 @@ public class DatabaseMonitor implements Runnable {
      */
     private void loadExtDictByMysql() {
 
-        Connection conn = null;
-        Statement stmt = null;
         ResultSet rs = null;
 
         try {
 
-            Long thisSynonymVersion = getRedisSynonymVersion();
+            if (connection == null || statement == null) {
 
-            conn = DriverManager.getConnection(getUrl(),getUsername(),getPassword());
+                connection = DriverManager.getConnection(
+                        dbProps.getProperty(JDBC_URL),
+                        dbProps.getProperty(JDBC_USERNAME),
+                        dbProps.getProperty(JDBC_PASSWORD)
+                );
 
-            stmt = conn.createStatement();
+                statement = connection.createStatement();
+            }
 
-            String sql = "select *,UNIX_TIMESTAMP(update_time) as version from sys_hot_word where UNIX_TIMESTAMP(update_time) >= "+thisSynonymVersion;
+            logger.info("获取新词库上一次修改的时间"+lastModified);
 
-            rs = stmt.executeQuery(sql);
+            String sql = "select *,UNIX_TIMESTAMP(update_time) as version from sys_hot_word where UNIX_TIMESTAMP(update_time) >= "+lastModified;
+
+            rs = statement.executeQuery(sql);
 
             List<String> stopWordList = new ArrayList<>();
             List<String> mainWordList = new ArrayList<>();
@@ -129,9 +108,9 @@ public class DatabaseMonitor implements Runnable {
                 String stopWord = rs.getString("stop_word");
                 Long version = rs.getLong("version");
 
-                if(version > thisSynonymVersion){
+                if(version > lastModified){
 
-                    thisSynonymVersion = version;
+                    lastModified = version;
 
                 }
 
@@ -158,7 +137,7 @@ public class DatabaseMonitor implements Runnable {
                 }
             }
 
-            setRedisSynonymVersion(thisSynonymVersion);
+            logger.info("获取新词库下一次修改的时间"+lastModified);
 
             if(stopWordList.size() > 0){
 
@@ -185,6 +164,7 @@ public class DatabaseMonitor implements Runnable {
             logger.error("从mysql加载热更新词典失败,错误信息为:", e);
 
         } finally {
+
             if (rs != null) {
                 try {
                     rs.close();
@@ -192,103 +172,6 @@ public class DatabaseMonitor implements Runnable {
                     logger.error("error", e);
                 }
             }
-            if (stmt != null) {
-                try {
-                    stmt.close();
-                } catch (SQLException e) {
-                    logger.error("error", e);
-                }
-            }
-            if (conn != null) {
-                try {
-                    conn.close();
-                } catch (SQLException e) {
-                    logger.error("error", e);
-                }
-            }
-        }
-    }
-
-    /**
-     * 获取redis中同义词版本号信息
-     * 用于判断同义词是否需要进行重新加载
-     * @return
-     */
-    public Long getRedisSynonymVersion() {
-
-        Long thisSynonymVersion = 0L;
-
-        RedissonClient redissonClient = RedisUtils.getRedissonClient(propsRedis);
-
-        try{
-
-            String ip = InetAddress.getLocalHost().getHostAddress();
-
-            RBucket<String> ipRBucket =  redissonClient.getBucket("es_version_"+ip);
-
-            if(ipRBucket.isExists()){
-
-                String version = ipRBucket.get();
-
-                if(version !=null && !"".equals(version)){
-
-                    thisSynonymVersion = Long.parseLong(version);
-
-                }
-
-
-
-            }
-
-        }catch (Exception exception){
-
-            exception.printStackTrace();
-
-            logger.error("获取redis中同义词版本号信息失败!"+exception);
-
-        }finally {
-
-            if(redissonClient !=null && !redissonClient.isShutdown()){
-
-                redissonClient.shutdown();
-            }
-        }
-
-        return thisSynonymVersion;
-
-    }
-
-    /**
-     * 设置redis中同义词版本号信息
-     * 用于判断同义词是否需要进行重新加载
-     * @return
-     */
-    public void setRedisSynonymVersion(Long setSynonymVersion) {
-
-
-        RedissonClient redissonClient = RedisUtils.getRedissonClient(propsRedis);
-
-        try{
-
-            String ip = InetAddress.getLocalHost().getHostAddress();
-
-            RBucket<String> ipRBucket =  redissonClient.getBucket("es_version_"+ip);
-
-            ipRBucket.set(String.valueOf(setSynonymVersion));
-
-        }catch (Exception exception){
-
-            exception.printStackTrace();
-
-            logger.error("设置redis中同义词版本号信息失败!"+exception);
-
-        }finally {
-
-            if(redissonClient !=null && !redissonClient.isShutdown()){
-
-                redissonClient.shutdown();
-            }
         }
-
     }
 }

+ 40 - 2
src/main/java/org/wltea/analyzer/dic/Dictionary.java

@@ -97,9 +97,13 @@ public class Dictionary {
 	private final static  String EXT_STOP = "ext_stopwords";
 	private final static  String REMOTE_EXT_STOP = "remote_ext_stopwords";
 
+	private final static String DB_PROPERTIES = "jdbc-reload.properties";
+
 	private Path conf_dir;
 	private Properties props;
 
+	private static Properties dbProps = new Properties();
+
 	private Dictionary(Configuration cfg) {
 		this.configuration = cfg;
 		this.props = new Properties();
@@ -128,9 +132,43 @@ public class Dictionary {
 				logger.error("ik-analyzer", e);
 			}
 		}
+
+		//加载数据库配置文件
+		Path dbConfigFile = conf_dir.resolve(DB_PROPERTIES);
+		loadDbProperties(dbConfigFile);
+	}
+
+	private void loadDbProperties(Path dbConfigFile) {
+
+		InputStream input = null;
+		try {
+			logger.info("try load db config from {}", dbConfigFile);
+			input = new FileInputStream(dbConfigFile.toFile());
+		} catch (FileNotFoundException e) {
+			conf_dir = configuration.getConfigInPluginDir();
+			dbConfigFile = conf_dir.resolve(DB_PROPERTIES);
+			try {
+				logger.info("try load db config from {}", dbConfigFile);
+				input = new FileInputStream(dbConfigFile.toFile());
+			} catch (FileNotFoundException ex) {
+				// We should report origin exception
+				logger.error("ik-analyzer -> load db config", e);
+			}
+		}
+
+		if (input != null) {
+			try {
+				dbProps.load(input);
+			} catch (IOException e) {
+				logger.error("ik-analyzer-> load db config", e);
+			}
+		}
+
+
+
 	}
 
-	public String getProperty(String key){
+	private String getProperty(String key){
 		if(props!=null){
 			return props.getProperty(key);
 		}
@@ -165,7 +203,7 @@ public class Dictionary {
 							pool.scheduleAtFixedRate(new Monitor(location), 10, 60, TimeUnit.SECONDS);
 						}
 						//建立数据库监控线程
-						pool.scheduleAtFixedRate(new DatabaseMonitor(),10,120,TimeUnit.SECONDS);
+						pool.scheduleAtFixedRate(new DatabaseMonitor(dbProps),10,120,TimeUnit.SECONDS);
 					}
 
 				}

+ 0 - 40
src/main/java/org/wltea/analyzer/util/RedisUtils.java

@@ -1,40 +0,0 @@
-package org.wltea.analyzer.util;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.redisson.Redisson;
-import org.redisson.api.RedissonClient;
-import org.redisson.config.Config;
-import org.wltea.analyzer.dic.DatabaseMonitor;
-import org.wltea.analyzer.help.ESPluginLoggerFactory;
-
-import java.util.Properties;
-import java.util.Set;
-
-public class RedisUtils {
-
-    private static final Logger logger = ESPluginLoggerFactory.getLogger(RedisUtils.class.getName());
-
-    private RedisUtils(){
-
-    }
-
-    /**
-     * 定义静态方法,返回RedissonClient对象
-     */
-    public static RedissonClient getRedissonClient(Properties props){
-
-        //1、创建redission的config对象并配置redis服务器
-        Config config = new Config();
-        config.useSingleServer().setAddress("redis://"+props.getProperty("redis.host")+":"+props.getProperty("redis.port"))
-        .setConnectTimeout(Integer.parseInt(props.getProperty("redis.timeout"))).setPassword(props.getProperty("redis.password"));
-
-        //2、创建Redisson客户端
-        RedissonClient redisson = Redisson.create(config);
-
-        return redisson;
-    }
-
-
-
-}