使用logstash将整个数据库保存到elasticsearch

2024-05-29 12:04:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚接触麋鹿,目前正在做的是:

  1. 使用configuration.conf文件在logstash中设置jdbc(input>;filter>;output)
  2. 对于logstash配置文件中具有单独的input{}的每个MySQL查询
  3. 或者使用pipilines.yml使单独的配置文件在单独的线程中运行(即每个MySQL查询都有(存储在)不同的配置文件中)
  4. 为管道运行命令logstash -f config.conf(Windows)或仅运行logstash

如何使用logstash获取数据库中的所有表并将它们索引到elaticseach,其中每个表的索引与MySQL数据库(Windows)中的表名同名。我可以以show tables的形式运行查询,get list并使用for循环,为每个表定义.conf并将它们保存为.conf文件吗?但我该如何修改.yml文件呢?因为文件是.conf和.yml而不是.py?在

Logstash configuration file image


Tags: 文件gt数据库inputoutputwindowsymlconf
1条回答
网友
1楼 · 发布于 2024-05-29 12:04:02

官方文档说“每个查询都必须有单独的jdbc”:Configuring multiple SQL statements

脚本如下:

getTableNames.py

import MySQLdb
# custom made class, Generate
from package_name.generate_conf_yml_logstash import Generate
connection = MySQLdb.connect(host="localhost:3306",
                              user="root/sa", password="password", db="database_name")
cursor = connection.cursor()
cursor.execute("show tables")
tables = cursor.fetchall()
application_name_tables = []
for table in tables:
    application_name_tables.append(table[0])
cursor.close()
connection.close()
Generate.save_files(application_name_tables)

generate_conf_yml_logstash.py

^{pr2}$

示例Config和YML文件是:

init_logstash_pipelines.yml

- pipeline.id: table
    path.config: "../config/table.conf"
    pipeline.workers: 1

initial_logstash.conf

input {
  jdbc {
    jdbc_driver_library => "../logstash-6.3.0/logstash-core/lib/jars/mysql-connector-java-5.1.46-bin.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://Mysql:3306/database_name"
    jdbc_user => "root"
    jdbc_password => "password"
    statement => "SELECT * from table_name"
    # schedule => "* * * * *"
  }
}
output {
    stdout { codec => json_lines }
    elasticsearch {
    hosts => ["localhost:9200"]
    index => "table_name"
    # as every table has diff. primary key, change this please
    document_id => "%{pk}"
    }
}

请随时根据您的需要更改初始配置、yml文件和代码

相关问题 更多 >

    热门问题