gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:22:28 25 4
gpt4 key购买 nike

我是 ELK 的新手,目前正在做的是:

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

如何使用logstash获取数据库的所有表并将它们索引到ElatsticSeach 一次性 其中每个表的索引与MySQL中的表名具有相同的名称数据库(Windows)。我可以运行查询作为显示表、获取列表并使用 for 循环并为每个表定义 .conf 并将它们保存为 .conf 文件吗?但我该如何修改 .yml 文件呢?因为文件是 .conf 和 .yml 而不是 .py?

Logstash configuration file image

最佳答案

官方文档说“每个查询都必须有单独的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

import sys
import os.path


class Generate:
@staticmethod
def save_files(tables):

# save config files
save_path = "D:\folder_name"
for table in tables:
init_f = save_path + "\initial_logstash.conf"
conf_f_name = table + ".conf"
save_file = os.path.join(save_path, conf_f_name)
with open(init_f, "r") as original: # read only
data = original.read()
data = data.replace("table_name", table)
with open(save_file, "w+") as conf: # overwrite if exist
conf.write(data)

# save yml file
yml_f_name = "init_logstash_pipelines.yml"
save_file = os.path.join(save_path, yml_f_name)
with open(save_file, "r") as original:
data = original.read()
with open(save_file, "a+") as yml: # append
for table in tables:
data = data.replace("table", table)
yml.write(data)
sys.exit()

示例配置和 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}"
}
}

请随意更改初始conf、yml 文件和代码。根据您的需求

关于python - 使用logstash将整个数据库保存到elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51185664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com