ElasticSearch IK分词器的MySQL热部署字典(Docker)

1.下载插件源码

找到自己对应ES版本的下载

Releases · infinilabs/analysis-ik · GitHub

2.添加mysql驱动依赖

XML 复制代码
        <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加载一次

bash 复制代码
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.编写热更新线程类

java 复制代码
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行)

java 复制代码
//启动热词字典加载线程
new Thread(new HotDictReloadThread()).start();

7.387行添加

java 复制代码
loadMySQLExtDict()
java 复制代码
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.编写加载停用字段代码

java 复制代码
//方法最后添加
this.loadMySQLStopwordDict();
java 复制代码
/**
	 * 从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容器,进入容器

java 复制代码
docker exec -it es bash

cd config

新建socketPolicy.policy

touch socketPolicy.policy

java 复制代码
cat >> socketPolicy.policy <<"EOF"
grant {
   permission java.net.SocketPermission "*:*","accept,connect,resolve";
   permission java.lang.RuntimePermission "setContextClassLoader";
};
EOF

修改jvm.options,注意路径要从/usr/share开始写

java 复制代码
cat >> jvm.options <<"EOF"
-Dfile.encoding=GBK
-Djava.security.policy=/usr/share/elasticsearch/config/socketPolicy.policy
EOF

13.把IK插件再重新加入到plugins中

14.测试

相关推荐
vvw&3 分钟前
Docker Build 命令详解:在 Ubuntu 上构建 Docker 镜像教程
linux·运维·服务器·ubuntu·docker·容器·开源
dessler1 小时前
Docker-Dockerfile讲解(二)
linux·运维·docker
SelectDB2 小时前
飞轮科技荣获中国电信星海大数据最佳合作伙伴奖!
大数据·数据库·数据分析
小刘鸭!3 小时前
Hbase的特点、特性
大数据·数据库·hbase
神奇侠20243 小时前
解决集群Elasticsearch 未授权访问漏洞
elasticsearch
Elastic 中国社区官方博客3 小时前
如何通过 Kafka 将数据导入 Elasticsearch
大数据·数据库·分布式·elasticsearch·搜索引擎·kafka·全文检索
神奇侠20243 小时前
解决单台Elasticsearch 未授权访问漏洞
elasticsearch
nece0013 小时前
elasticsearch 杂记
大数据·elasticsearch·搜索引擎
开心最重要(*^▽^*)3 小时前
Es搭建——单节点——Linux
大数据·elasticsearch
学计算机的睿智大学生5 小时前
Hadoop的生态系统所包含的组件
大数据·hadoop·分布式