-
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.
- Loading branch information
1 parent
48b74d8
commit 9bbc969
Showing
5 changed files
with
77 additions
and
13 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
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,56 @@ | ||
import mysql.connector | ||
from elasticsearch import Elasticsearch, helpers | ||
|
||
# MySQL database connection parameters | ||
db_config = { | ||
'user': 'skku-user', | ||
'password': 'skku-pw', | ||
'host': 'skku-db', | ||
'database': 'skku', | ||
'port': 3306 | ||
} | ||
|
||
# Elasticsearch connection parameters | ||
es = Elasticsearch(['http://localhost: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