【问题标题】:Mysql to Elasticsearch - won't create index and export dataMysql 到 Elasticsearch - 不会创建索引和导出数据
【发布时间】:2016-06-23 21:47:19
【问题描述】:

我正在尝试将 MySQL 表导入 Elasticsearch。它是一个包含 10 个不同列的表,其中一个 8 位 VARCHAR 设置为主键。 MySQL 数据库位于远程主机上。

为了将数据从 MySQL 传输到 Elasticsearch,我决定使用 Logstash 和 jdbc MySQL 驱动程序。

我假设 Logstash 会为我创建索引,如果它不存在的话。

这是我的 logstash.conf 脚本:

input{ 

jdbc {      
jdbc_driver_library => "/home/user/logstash/mysql-connector-java-5.1.17-bin.jar"

jdbc_driver_class => "com.mysql.jdbc.Driver"

jdbc_connection_string => "jdbc:mysql://[remotehostipnumber]/databasename"

jdbc_validate_connection => true

jdbc_user => "username"

jdbc_password => "password"

schedule => "* * * * *"

statement => "select * from table" 

} 

}

output  

{
  elasticsearch 
{

index => "tables"
document_type => "table"
document_id => "%{table_id}"
hosts => "localhost:9200"
}stdout { codec => json_lines }
}

在运行 logstash config test 时,它会输出“Configration OK”消息:

sudo /opt/logstash/bin/logstash --configtest -f /home/user/logstash/logstash.conf

同样在执行 logstash.conf 脚本时,Elasticsearch 会输出:

Settings: Default filter workers: 1
Logstash startup completed

但是当我去检查索引是否已经创建并且数据也已经添加时:

curl -XGET 'localhost:9200/tables/table/_search?pretty=true'

我明白了:

{
"error" : {
"root_cause" : [ {
  "type" : "index_not_found_exception",
  "reason" : "no such index",
  "resource.type" : "index_or_alias",
  "resource.id" : "tables",
  "index" : "table"
} ],
"type" : "index_not_found_exception",
"reason" : "no such index",
"resource.type" : "index_or_alias",
"resource.id" : "tables",
"index" : "tables"
 },
 "status" : 404
}

数据未被编入索引的潜在原因是什么?

附言。我让 Elasticsearch 服务器在单独的终端窗口中运行,以确保 Logstash 可以连接并与之交互。

【问题讨论】:

  • 你能用--debug 运行logstash,这样你就可以看到更多的输出了吗?你能ping你的数据库主机remotehostipnumber吗?
  • 由于您使用的是 stdout 输出,您应该能够检查 Logstash 是否接收到数据,并知道问题是否在于 Logstash 检索日志或将它们发送到 Elasticsearch
  • 查看 --debug 输出,它看起来像已连接,我还可以 ping 数据库主机。真的卡在这里

标签: mysql database jdbc elasticsearch logstash


【解决方案1】:

对于那些最终在这里寻找类似问题的答案的人。

我的数据库有 400 万行,logstash/elasticsearch/jdbc 驱动程序在一个命令中处理的数量肯定太多了。

在我将初始传输分成 4 个单独的工作块后,脚本运行并将所需的表添加到 elasticsearch NoSQL 数据库中。

【讨论】:

    【解决方案2】:

    使用以下代码从mysql表中导出数据并在弹性搜索中创建索引

    echo '{
    "type":"jdbc",
    "jdbc":{
    
    "url":"jdbc:mysql://localhost:3306/your_database_name",
    "user":"your_database_username",
    "password":"your_database_password",
    "useSSL":"false",
    "sql":"SELECT * FROM table1",
    "index":"Index_name",
    "type":"Index_type",
    "poll" : "6s",
    "autocommit":"true",
    "metrics": {
                "enabled" : true
            },
            "elasticsearch" : {
                 "cluster" : "clustername",
                 "host" : "localhost",
                 "port" : 9300 
            } 
    }
    }' | java -cp "/etc/elasticsearch/elasticsearch-jdbc-2.3.4.0/lib/*" -"Dlog4j.configurationFile=file:////etc/elasticsearch/elasticsearch-jdbc-2.3.4.0/bin/log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCImporter"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-21
      • 2013-06-23
      • 2012-01-26
      • 1970-01-01
      • 2019-01-30
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多