zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch详解程序员

2023-06-13 09:19:59 时间

按照之前的四篇文章,走到现在,接下来就是需要安装jdbc了。这里是用的是jdbc的2.3.4.0版本,虽然按照官方给出的对应版本对应不上,https://github.com/jprante/elasticsearch-jdbc,但是实际测试,是可以支持的。

下载解压并移动到/usr/local/jdbc目录

#wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.4.0/elasticsearch-jdbc-2.3.4.0-dist.zip //wget下载地址

# unzip elasticsearch-jdbc-2.3.4.0-dist.zip 

# mv elasticsearch-jdbc-2.3.4.0-dist.zip /usr/local/jdbc/

配置环境变量

# vim /etc/profile //把下面两行代码放入底部 

export JDBC_HOME=/usr/local/jdbc 

export ES_HOME=/usr/local/elasticsearch 

# source /etc/profile //生效

接下来创建导入脚本

# cd /usr/local/ 

# mkdir shell shell/logs 

# vim mysql-elasticsearch.sh

"elasticsearch.autodiscover":true, "elasticsearch.cluster":"my-application", #簇名,对应好配置 "url":"jdbc:mysql://192.168.9.155:3306/hb", #mysql地址/端口/库名 "user":"root", #Mysql账号 "password":"wt000000", #Mysql密码 "sql":"select product_name,id as _id from hp_product where id 10 limit 10 ", #SQL语句 .........注意的是,这里只是测试,在实际配置中需要对应好要存入文档的字段,同样表的主见id对应文档id "elasticsearch" : {
"cluster":"my-application", "host" : "192.168.9.155", "port" : 9300 "index" : "myindex", #新的index,这里测试的是商品表,用的index是myindex,其实可以规范命名~ "type" : "mytype" #新的type,类型也可以规范命名 }| java / -cp "${lib}/*" / -Dlog4j.configurationFile=${bin}/log4j2.xml / org.xbib.tools.Runner / org.xbib.tools.JDBCImporter
# chmod a+x mysql-elasticsearch.sh //赋予权限 

# ./mysql-elasticsearch.sh //执行脚本

ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch详解程序员

执行完成之后,查看一下日志文件,[INFO]成功信息~

cat /usr/local/shell/logs/jdbc.log

ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch详解程序员

# curl -XGET http://192.168.9.155:9200/myindex/mytype/_search?pretty  

ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch详解程序员

 OK~

 

 

 

 

附加 -

#!/bin/sh 

bin=$JDBC_HOME/bin 

lib=$JDBC_HOME/lib 

echo { 

 "type" : "jdbc", 

 "jdbc" : { 

 "url" : "jdbc:mysql://172.16.10.XXX:3306/db_car", 

 "user" : "write", 

 "password" : "write", 

 "sql" : "select id,title,fnStripTags(content) as content,click_num,keywords,UNIX_TIMESTAMP(updated) as update_time from t_knowledgenew", 

 "elasticsearch" : { 

 "cluster": "my-application", 

 "host" : "172.16.170.XXX", 

 "port" : 9300 

 "index" : "knowledge_index", 

 "type" : "knowledge" 

} | java / 

 -cp "${lib}/*" / 

 -Dlog4j.configurationFile=${bin}/log4j2.xml / 

 org.xbib.tools.Runner / 

 org.xbib.tools.JDBCImporter

通过命令查看es中数据

curl -XGET http://XXX.XXX.XXX.XXX:9200/knowledge_index/knowledge/_search?pretty 

ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch详解程序员

 删除指定索引

# curl -XDELETE http://172.16.170.122:9200/index_name

 

 

 

 

 

实时同步。

https://github.com/m358807551/mysqlsmom

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/2343.html

服务器部署程序员系统优化网站设置运维