在我的一个项目中,我计划将ElasticSearch与MySQL一起使用。
我已经成功安装了ElasticSearch。我能够独立管理ES中的索引,但是我不知道如何在MySQL中实现相同的功能。
我已经阅读了一些文档,但我还是有点困惑,没有清晰的想法。
从 ES 5.x 开始,他们已经在 logstash 插件中默认提供了此功能。
这将定期从数据库导入数据并推送到 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的jar包。
如果在执行此脚本时ES中不存在索引,它们将自动创建。就像对Elasticsearch的普通POST调用一样。
最终我找到了答案,现在分享我的发现。
要使用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
这个安装过程将会在 /usr/share/elasticsearch/ 安装 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
/etc/init.d/elasticsearch start
要测试是否安装了ES,请运行以下命令
curl -XGET 'http://localhost:9200/?pretty'
{
"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"
}
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)。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"
curl -XGET http://localhost:9200/users/_search/?pretty
sudo add-apt-repository ppa:webupd8team/java
,然后sudo apt-get update
,最后sudo apt-get install oracle-java8-installer
。我之前安装了9个OpenJDB,导致访问控制错误。 - Tsangaresinput {
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}"
}
}
logstash-integration-jdbc
插件。 - Eric