-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KAN-79] MYSQL -> ES 동기화 배치 작업 (#18)
* [KAN-79] MYSQL -> ES 동기화 배치 작업 * [KAN-79] MYSQL -> ES 동기화 배치 작업 * [KAN-79] MYSQL -> ES 동기화 배치 작업 * [KAN-79] MYSQL -> ES 동기화 배치 작업 * [KAN-79] MYSQL -> ES 동기화 배치 작업
- Loading branch information
1 parent
48b74d8
commit 5e0971f
Showing
6 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Mysql To ElasticSearch Batch Job | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- main | ||
schedule: | ||
- cron: '0 2 * * *' | ||
|
||
jobs: | ||
batch-job: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Docker Build And Push | ||
run: | | ||
docker login -u ${{ secrets.USERNAME }} -p ${{ secrets.PASSWORD }} | ||
docker build -f mysql-to-es.Dockerfile -t skku-mysql-to-es-batch . | ||
docker tag skku-mysql-to-es-batch sinkyoungdeok/skku-mysql-to-es-batch | ||
docker push sinkyoungdeok/skku-mysql-to-es-batch | ||
- name: Deploy Prod | ||
uses: appleboy/ssh-action@v0.1.4 | ||
with: | ||
key: ${{ secrets.SSH_KEY }} | ||
host: ${{ secrets.HOST_NAME }} | ||
username: ubuntu | ||
port: 22 | ||
script: | | ||
docker login -u ${{ secrets.USERNAME }} -p ${{ secrets.PASSWORD }} | ||
docker pull sinkyoungdeok/skku-mysql-to-es-batch | ||
docker run --net ubuntu_default sinkyoungdeok/skku-mysql-to-es-batch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mysql-connector-python | ||
elasticsearch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM python:3.8-slim | ||
|
||
COPY mysql-to-es-requirements.txt mysql-to-es-requirements.txt | ||
COPY mysql-to-es.py mysql-to-es.py | ||
|
||
RUN pip install -r mysql-to-es-requirements.txt | ||
|
||
ENTRYPOINT ["python3", "mysql-to-es.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import mysql.connector | ||
from elasticsearch import Elasticsearch, helpers | ||
|
||
db_config = { | ||
'user': 'skku-user', | ||
'password': 'skku-pw', | ||
'host': 'skku-db', | ||
'database': 'skku', | ||
'port': 3306 | ||
} | ||
|
||
es = Elasticsearch(['http://es-singlenode:9200']) | ||
|
||
|
||
def fetch_restaurant_data(): | ||
connection = mysql.connector.connect(**db_config) | ||
cursor = connection.cursor(dictionary=True) | ||
|
||
query = """ | ||
SELECT id, name, rating_avg, review_count, like_count | ||
FROM restaurants | ||
""" | ||
|
||
cursor.execute(query) | ||
data = cursor.fetchall() | ||
|
||
cursor.close() | ||
connection.close() | ||
|
||
return data | ||
|
||
|
||
def update_elasticsearch(data): | ||
actions = [ | ||
{ | ||
"_op_type": "update", | ||
"_index": "restaurant", | ||
"_id": restaurant['id'], | ||
"doc": { | ||
"rating_avg": restaurant['rating_avg'], | ||
"review_count": restaurant['review_count'], | ||
"like_count": restaurant['like_count'] | ||
}, | ||
"doc_as_upsert": True | ||
} | ||
for restaurant in data | ||
] | ||
|
||
helpers.bulk(es, actions) | ||
|
||
|
||
restaurant_data = fetch_restaurant_data() | ||
update_elasticsearch(restaurant_data) | ||
print("Elasticsearch update complete") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters