在我的一个项目中,我计划将ElasticSearch与MySQL结合使用。我已经成功地安装了ElasticSearch。我能够分别管理ES中的索引。但我不知道如何在MySQL中实现相同的功能。
我读过几份文件,但我有点困惑,没有一个明确的概念。
发布于 2016-04-05 11:55:25
最后我找到了答案。分享我的发现。
要在Mysql中使用ElasticSearch,您将需要(JDBC)导入程序。使用JDBC驱动程序,您可以将mysql数据同步到elasticsearch。
我使用的是ubuntu14.04LTS,您需要安装Java8来运行elasticsearch,因为它是用Java编写的。
下面是安装ElasticSearch 2.2.0和ElasticSearch-JDBC2.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此安装过程将在/usr/share/ Elasticsearch /其配置文件放置在/etc/elasticsearch中安装elasticsearch。
现在让我们在配置文件中进行一些基本配置。在这里/etc/elasticsearch/弹性搜索. you是我们的配置文件,您可以打开文件修改
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/并创建“日志”文件夹(日志路径应为/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"现在检查mysql数据是否导入到ES中。
curl -XGET http://localhost:9200/users/_search/?pretty如果一切顺利,您将能够以json格式查看所有mysql数据,如果存在任何错误,则可以在/etc/elasticsearch/log/jdbc.log文件中看到它们。
警告:
在旧版本的ES中..。插件Elasticsearch-river-jdbc是使用的,这是完全不推荐在最新版本,所以不要使用它。
我希望能节省你的时间:)
如有任何进一步的想法,将不胜感激。
发布于 2016-11-22 17:07:33
从ES 5.x开始,他们就用原木存放物插件提供了这个特性。
这将定期从数据库导入数据并推送到ES服务器。
我们必须创建一个简单的导入文件,并使用logstash来运行脚本。Logstash支持按计划运行此脚本。
# file: contacts-index-logstash.conf
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/mydb"
jdbc_user => "user"
jdbc_password => "pswd"
schedule => "* * * * *"
jdbc_validate_connection => true
jdbc_driver_library => "/path/to/latest/mysql-connector-java-jar"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
statement => "SELECT * from contacts where updatedAt > :sql_last_value"
}
}
output {
elasticsearch {
protocol => http
index => "contacts"
document_type => "contact"
document_id => "%{id}"
host => "ES_NODE_HOST"
}
}
# "* * * * *" -> run every minute
# sql_last_value is a built in parameter whose value is set to Thursday, 1 January 1970,
# or 0 if use_column_value is true and tracking_column is set您可以从maven 这里下载mysql。
如果在执行此脚本时ES中不存在索引,则将自动创建索引。就像向elasticsearch发出普通的post电话一样
发布于 2018-02-20 11:07:30
logstash插件将完成以下工作:
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/testdb"
jdbc_user => "root"
jdbc_password => "factweavers"
# The path to our downloaded jdbc driver
jdbc_driver_library => "/home/comp/Downloads/mysql-connector-java-5.1.38.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
# our query
schedule => "* * * *"
statement => "SELECT" * FROM testtable where Date > :sql_last_value order by Date"
use_column_value => true
tracking_column => Date
}
output {
stdout { codec => json_lines }
elasticsearch {
"hosts" => "localhost:9200"
"index" => "test-migrate"
"document_type" => "data"
"document_id" => "%{personid}"
}
}https://stackoverflow.com/questions/36152152
复制相似问题