mysql과 elasticsearch 실시간 데이터 연동 + 노리까지!

BaeBae·2023년 4월 28일
0

검색엔진

목록 보기
6/7
post-thumbnail

지난번에 mysql을 연동해보았다!
이어서 mysql의 데이터가 변경 될 때마다 logstash가 watch하여 데이터를 가져와보려고 한다!

1. *.conf 파일 수정

  • 아래의 1-1과 1-2중 하나로만 수행하면 된다

1-0. *.conf 파일 내용

input {
    jdbc {
      clean_run => true
        jdbc_driver_library => "/usr/share/java/mysql-connector-java-8.0.23.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_connection_string => "jdbc:mysql:DB 주소"
        jdbc_user => "DB 사용자 이름"
        jdbc_password => "DB 사용자 비밀번호"
      jdbc_paging_enabled => true
      tracking_column => "unix_ts_in_secs"
      use_column_value => true
      tracking_column_type => "numeric"
      statement => "SELECT *, UNIX_TIMESTAMP(modified_time) AS unix_ts_in_secs FROM member WHERE (UNIX_TIMESTAMP(modified_time) > :sql_last_value AND modified_time < NOW()) ORDER BY modified_time ASC"
      schedule => "/5 * * * * *"
      last_run_metadata_path => "/usr/share/logstash/.logstash_jdbc_last_run"
    }
}

filter {
  mutate {
    copy => {"member_id" => "[@metadata][member_id]"}
    remove_field => ["member_id", "@version", "unix_ts_in_secs"]
  }
}

output {

stdout {}
elasticsearch {
    hosts => "http://localhost:9200"
    index => "churest"
    document_id => "%{[@metadata][member_id]}"
  }
}

1-1. 터미널에서 수정하는 방법

# usr/share/logstash로 이동
cd usr/share/logstash

# conf 파일 수정
vi churest.conf

# 1-0의 내용을 복사해서 자신에 맞게 편집한 다음 붙여넣기!

1-2. 파일을 열어 직접 수정하는 방법

# usr/share/logstash로 이동
# conf 파일 열기
# 1-0의 내용을 복사해서 자신에 맞게 편집한 다음 붙여넣기!

파일 수정이 안 된다면? 파일을 열었는데 읽기 전용이라면?

$ sudo su
### 파일에 모든 권한 부여
$ chmod 777 {파일 이름}.conf

2. 한글 검색을 위한 nori 설치

# /usr/share/elasticsearch 에서
# nori 설치
sudo bin/elasticsearch-plugin install analysis-nori

3. index에 analyzer 추가해서 생성하기

3-1. 새로운 index 만들기

curl -XPUT http://localhost:9200/churest2

3-2. index의 settings 설정을 위해 index close

curl -XPOST http://localhost:9200/churest2/_close

3-3. settings 설정

PUT http://localhost:9200/churest2/_settings

# body 내용
{
  "settings": {
    "analysis": {
      "analyzer": {
        "churest_nori_none": {
          "tokenizer": "nori_none",
          "type": "custom"
        },
        "churest_nori_discard": {
          "tokenizer": "nori_discard",
          "type": "custom"
        },
        "churest_nori_mixed": {
          "tokenizer": "nori_mixed",
          "type": "custom"
        }
      },
      "tokenizer": {
        "nori_none": {
          "type": "nori_tokenizer",
          "decompound_mode": "none"
        },
        "nori_discard": {
          "type": "nori_tokenizer",
          "decompound_mode": "discard"
        },
        "nori_mixed": {
          "type": "nori_tokenizer",
          "decompound_mode": "mixed"
        }
      }
    }
  }
}

3-4. index open

curl -XPOST http://localhost:9200/churest2/_open

3-5. mappings 설정

  • 기존의 mapping 정보 복사
GET http://localhost:9200/churest/_mappings
  • 검색하고 싶은 곳에 원하는 nori로 analyze 추가
파라미터명파라미터 값설명예제
decompound_modenone복합명사로 분리하지 않는다- 잠실역
- 사회보장제도
discard복합명사로 분리하고 원본 데이터는 삭제한다- 잠실역 => [잠실, 역]
- 사회보장제도 => [사회, 보장, 제도]
mixed복합명사로 분리하고 원본 데이터도 유지한다- 잠실역 => [잠실, 역, 잠실역]
- 사회보장제도 => [사회, 보장, 제도, 사회보장제도]
PUT http://localhost:9200/churest2/_mappings

{
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "avatar_id": {
          "type": "long"
        },
        "coin": {
          "type": "long"
        },
        "email": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "modified_time": {
          "type": "date"
        },
        "nickname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
        "analyzer": "churest_nori_mixed"
        },
        "token": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
}

4. 실행

# elastic search 실행
sudo systemctl start elasticsearch.service

# logstash 실행
sudo systemctl start logstash.service

# 데이터 변화 감지하기 위해 실행!
sudo bin/logstash -f churest.conf 

실행 전에 *.conf 파일에 새로 만든 인덱스 이름으로 바꿔야 한다!

5. 검색

http://localhost:9200/churest2/_search?q=검색할 단어

끝! ㅎㅎ
다음에는 EC2 Docker 환경에서 진행해봐야겠다! ㅎㅎ


참고 자료
profile
Data가 좋은 Web 개발자

0개의 댓글