栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ElasticSearch如何与Mysql集成

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ElasticSearch如何与Mysql集成

终于我找到了答案。分享我的发现。

要将ElasticSearch与Mysql一起使用,您将需要Java数据库连接( JDBC
)导入程序。使用JDBC驱动程序,您可以将mysql数据同步到elasticsearch中。

我正在使用ubuntu 14.04 LTS,您将需要安装Java8才能运行Elasticsearch,因为它是用Java编写的

以下是安装 ElasticSearch 2.2.0和ElasticSearch-jdbc 2.2.0的步骤 ,请注意,
两个版本必须相同

在安装Java8 .....之后,按如下所示安装elasticsearch 2.2.0

# cd /opt# wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.2.0/elasticsearch-2.2.0.deb# sudo dpkg -i elasticsearch-2.2.0.deb

此安装过程会将Elasticsearch安装在/ usr / share / elasticsearch /中,其配置文件将放置在/ etc /
elasticsearch中。

现在让我们在配置文件中进行一些基本配置。/etc/elasticsearch/elasticsearch.yml是我们的配置文件,您可以打开文件进行更改

nano /etc/elasticsearch/elasticsearch.yml

并更改集群名称和节点名称

例如 :

# ---------------------------------- Cluster -----------------------------------## Use a descriptive name for your cluster:# cluster.name: servercluster## ------------------------------------ Node ------------------------------------## Use a descriptive name for the node:# node.name: vps.server.com## Add custom attributes to the node:## node.rack: r1

现在保存文件并开始elasticsearch

 /etc/init.d/elasticsearch start

测试已安装或未运行的ES

 curl -XGET 'http://localhost:9200/?pretty'

如果您得到关注,那么您的elasticsearch现在已安装:)

{  "name" : "vps.server.com",  "cluster_name" : "servercluster",  "version" : {    "number" : "2.2.0",    "build_hash" : "8ff36d139e16f8720f2947ef62c8167a888992fe",    "build_timestamp" : "2016-01-27T13:32:39Z",    "build_snapshot" : false,    "lucene_version" : "5.4.1"  },  "tagline" : "You Know, for Search"}

现在让我们安装 elasticsearch-JDBC

http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.3.1/elasticsearch-jdbc-2.3.3.1-dist.zip
从中下载它,并将其解压缩到/ etc / elasticsearch /中,并在那里也创建“ logs”文件夹(日志路径应为/ etc /
elasticsearch / logs)

我在mysql中创建了一个名为“ ElasticSearchDatabase ”的数据库,并在该 表中名为“ test”的表,
其中包含字段ID,名称和电子邮件

cd /etc/elasticsearch

然后运行

echo '{"type":"jdbc","jdbc":{"url":"jdbc:mysql://localhost:3306/ElasticSearchDatabase","user":"root","password":"","sql":"SELECt id as _id, id, name,email FROM test","index":"users","type":"users","autocommit":"true","metrics": { "enabled" : true        },        "elasticsearch" : {  "cluster" : "servercluster",  "host" : "localhost",  "port" : 9300         } }}' | java -cp "/etc/elasticsearch/elasticsearch-jdbc-2.2.0.0/lib/*" -"Dlog4j.configurationFile=file:////etc/elasticsearch/elasticsearch-jdbc-2.2.0.0/bin/log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCimporter"

现在检查是否在ES中导入了mysql数据

curl -XGET http://localhost:9200/users/_search/?pretty

如果一切顺利,您将能够以json格式查看所有mysql数据,如果有任何错误,您将可以在/etc/elasticsearch/logs/jdbc.log文件中查看它们

注意事项:

在旧版本的ES中,使用的插件 Elasticsearch-river-jdbc 在最新版本中已完全弃用,因此请不要使用它。

我希望我可以节省您的时间:)

任何进一步的想法表示赞赏

参考网址:https :
//github.com/jprante/elasticsearch-
jdbc



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405252.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号