如何将ElasticSearch与MySQL集成?

64
在我的一个项目中,我计划将ElasticSearch与MySQL一起使用。 我已经成功安装了ElasticSearch。我能够独立管理ES中的索引,但是我不知道如何在MySQL中实现相同的功能。 我已经阅读了一些文档,但我还是有点困惑,没有清晰的想法。
4个回答

76

从 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调用一样。


4
这个答案应该是首选的。 - Lluis Martinez
1
“contacts-index-logstash.conf” 应该放在哪里? - Brian Leishman
嗨,@NikhilSahu,如何测试这个配置是否运行良好? - user2809386
1
从ES 7+开始,似乎应该使用logstash-integration-jdbc插件。 - Eric
有没有一种方法可以使用数组值填充文档字段? - LF00
显示剩余2条评论

46

最终我找到了答案,现在分享我的发现。

要使用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

现在保存文件并启动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、name和email字段。
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/logs/jdbc.log文件中查看它们。
注意:
在旧版本的ES中...插件Elasticsearch-river-jdbc已被完全弃用,在最新版本中不要使用它。
我希望我可以节省您的时间:)
欢迎提出进一步的想法。
参考网址: https://github.com/jprante/elasticsearch-jdbc

嗨,对我来说不起作用,出现了“type”:“index_not_found_exception”和状态404。无法连接到MySQL。 - Vipin Singh
注意:您需要安装Java 8 Oracle:sudo add-apt-repository ppa:webupd8team/java,然后sudo apt-get update,最后sudo apt-get install oracle-java8-installer。我之前安装了9个OpenJDB,导致访问控制错误。 - Tsangares
@Yaxita...你有没有机会找出如何处理从mysql读取并馈送到elasticsearch的行?除了更新时间戳之外,你如何处理mysql中的增量更改? - Prannoy Mittal
我已经安装了es-5.5.2,但是在http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/上没有相应的elasticsearch-jdbc 5.5.2。 - Sunil Garg
发现错误:{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"没有这个索引","resource.type":"index_or_alias","resource.id":"users","index":"users"}],"type":"index_not_found_exception","reason":"没有这个索引","resource.type":"index_or_alias","resource.id":"users","index":"users"},"status":404} 请帮我解决它。 - Prashant Shukla
"port" : 9300 - user2809386

6
logstash JDBC插件将能够完成此任务:
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}"
  }
}

2
为了让它更简单,我创建了一个PHP类来设置MySQL与Elasticsearch的连接。使用我的类,您可以将MySQL数据同步到elasticsearch中,并进行全文搜索。您只需要设置SQL查询,类就会为您完成其余工作。

很抱歉,您的帖子并没有回答问题。您提到了如何使用一种PHP API与elasticsearch配合,但并没有说明elasticsearch如何与数据库连接(与语言无关)。如果有人不打算使用PHP怎么办? 您的博客文章也是误导性的。我并不是想冒犯您,只是表达我的怀疑。 - Semo
我已经尝试过了,但是出现了错误:“Fatal error: Can only throw objects in /Connection.php on line 539”。你能帮忙吗? - Vidya L
@Ahmed,有没有办法提高处理速度?我的操作系统被重定向到https://stackoverflow.com/questions/71845773/how-to-process-large-data-from-database-using-php。 - nas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接