개발공부/Elasticsearch

[ELK] logstash와 mysql 다중 테이블 가져오기

차나니 2024. 4. 25. 13:15

logstash를 통해 mysql에 있는 데이터를 elasticsearch로 불러오는 방법은 여기에서 보실 수 있습니다 !

다중 테이블 가져오기

이전 포스팅에서 logstash.conf 파일 input에 JDBC를 통해 Mysql에서 데이터를 가져올 수 있도록 코드를 작성해봤는데요.

쿼리를 통해 조회된 데이터를 output에 등록해 놓은 index로 데이터를 전달해줄 수 있도록 하였습니다.

그런데 ! 과연 쿼리를 1개만 작성할 수 있는지 궁금해서 multi pipeline 관련된 내용을 찾아봤습니다.

conf 파일을 따로 생성하여 알맞은 port에 접속했을 때 특정 conf 파일이 실행되는 형태였습니다.

하지만 저는 그냥 단순히 쿼리를 하나 더 써서 다른 index에 값을 가져오고 싶던 찰나 아래와 같은 방법을 찾았습니다 !

input {
   jdbc {
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_driver_library => "/Users/ichanhan/Desktop/coding/tools/logstash-7.17.3/logstash-core/lib/jars/mysql-connector-j-8.2.0.jar"
    jdbc_connection_string => "jdbc:mysql://localhost:3306/TheJapen?serverTimezone=Asia/Seoul"
    jdbc_user => "DBUSER"
    jdbc_password => "DBPASSWORD"
    statement => "SELECT * FROM word ORDER BY word_num"
    use_column_value => true
    record_last_run => true
    tracking_column => "word_num"
    clean_run => true
    schedule => "*/3 * * * *"
    jdbc_fetch_size => 1000
    tags => ["word"]
  }
  
  jdbc {
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_driver_library => "/Users/ichanhan/Desktop/coding/tools/logstash-7.17.3/logstash-core/lib/jars/mysql-connector-j-8.2.0.jar"
    jdbc_connection_string => "jdbc:mysql://localhost:3306/TheJapen?serverTimezone=Asia/Seoul"
    jdbc_user => "TheJapen"
    jdbc_password => "1q2w3e4r1!"
    statement => "SELECT * FROM notice ORDER BY notice_num"
    use_column_value => true
    record_last_run => true
    tracking_column => "notice_num"
    clean_run => true
    schedule => "*/3 * * * *"
    jdbc_fetch_size => 1000
    tags => ["notice"]
  }
}
output {
  if "word" in [tags] {
    elasticsearch {
      hosts => ["http://localhost:9200"]
      index => "word"
      document_id => "%{word_num}"
    }
  }
  if "notice" in [tags] {
    elasticsearch {
      hosts => ["http://localhost:9200"]
      index => "notice"
      document_id => "%{notice_num}"
    }
  }
}