1.下载插件源码
找到自己对应ES版本的下载
Releases · infinilabs/analysis-ik · GitHub
2.添加mysql驱动依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
3.修改ES版本为当前自己的版本
我这里是7.12.1
4.编写jdbc-reload.properties
每10s加载一次
jdbc.url=jdbc:mysql://192.168.88.129:3306/moni?serverTimezone=GMT
jdbc.user=root
jdbc.password=123456
jdbc.reload.sql=select word from hotwords
jdbc.reload.stopword.sql=select stopword as word from stopwords
jdbc.reload.interval=10000
5.编写热更新线程类
package org.wltea.analyzer.dic;
import org.apache.logging.log4j.Logger;
import org.wltea.analyzer.help.ESPluginLoggerFactory;
public class HotDictReloadThread implements Runnable {
private static final Logger LOGGER = ESPluginLoggerFactory.getLogger(HotDictReloadThread.class.getName());
@Override
public void run() {
while(true) {
LOGGER.info("[==========]从MYSQL中读取字典......");
Dictionary.getSingleton().reLoadMainDict();
}
}
}
6.修改Dictionary类(约157行)
//启动热词字典加载线程
new Thread(new HotDictReloadThread()).start();
7.387行添加
loadMySQLExtDict()
private static Properties prop = new Properties();
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
logger.error("error", e);
}
}
/**
* 从mysql加载热更新词典
*/
private void loadMySQLExtDict() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Path file = PathUtils.get(getDictRoot(), "jdbc-reload.properties");
prop.load(new FileInputStream(file.toFile()));
logger.info("[==========]jdbc-reload.properties");
for(Object key : prop.keySet()) {
logger.info("[==========]" + key + "=" + prop.getProperty(String.valueOf(key)));
}
logger.info("[==========]query hot dict from mysql, " + prop.getProperty("jdbc.reload.sql") + "......");
conn = DriverManager.getConnection(
prop.getProperty("jdbc.url"),
prop.getProperty("jdbc.user"),
prop.getProperty("jdbc.password"));
stmt = conn.createStatement();
rs = stmt.executeQuery(prop.getProperty("jdbc.reload.sql"));
while(rs.next()) {
String theWord = rs.getString("word");
logger.info("[==========]hot word from mysql: " + theWord);
_MainDict.fillSegment(theWord.trim().toCharArray());
}
Thread.sleep(Integer.valueOf(String.valueOf(prop.get("jdbc.reload.interval"))));
} catch (Exception e) {
logger.error("erorr", e);
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
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);
}
}
}
}
8.编写加载停用字段代码
//方法最后添加
this.loadMySQLStopwordDict();
/**
* 从mysql加载停用词
*/
private void loadMySQLStopwordDict() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Path file = PathUtils.get(getDictRoot(), "jdbc-reload.properties");
prop.load(new FileInputStream(file.toFile()));
logger.info("[==========]jdbc-reload.properties");
for(Object key : prop.keySet()) {
logger.info("[==========]" + key + "=" + prop.getProperty(String.valueOf(key)));
}
logger.info("[==========]query hot stopword dict from mysql, " + prop.getProperty("jdbc.reload.stopword.sql") + "......");
conn = DriverManager.getConnection(
prop.getProperty("jdbc.url"),
prop.getProperty("jdbc.user"),
prop.getProperty("jdbc.password"));
stmt = conn.createStatement();
rs = stmt.executeQuery(prop.getProperty("jdbc.reload.stopword.sql"));
while(rs.next()) {
String theWord = rs.getString("word");
logger.info("[==========]hot stopword from mysql: " + theWord);
_StopWords.fillSegment(theWord.trim().toCharArray());
}
Thread.sleep(Integer.valueOf(String.valueOf(prop.get("jdbc.reload.interval"))));
} catch (Exception e) {
logger.error("erorr", e);
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
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);
}
}
}
}
9.maven打包
10.上传至docker
先解压再上传至plugins文件内(会报错,可以先看下一步)
11.重新启动报错
12.解决报错
删除ik分词器,启动ES容器,进入容器
docker exec -it es bash
cd config
新建socketPolicy.policy
touch socketPolicy.policy
cat >> socketPolicy.policy <<"EOF"
grant {
permission java.net.SocketPermission "*:*","accept,connect,resolve";
permission java.lang.RuntimePermission "setContextClassLoader";
};
EOF
修改jvm.options,注意路径要从/usr/share开始写
cat >> jvm.options <<"EOF"
-Dfile.encoding=GBK
-Djava.security.policy=/usr/share/elasticsearch/config/socketPolicy.policy
EOF