-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_data_separated.py
67 lines (48 loc) · 1.93 KB
/
graph_data_separated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pymysql
import json
def save_graph_to_json(graph, filename):
# 리스트로 변환
graph_as_list = {str(key): list(value) for key, value in graph.items()}
with open(filename, 'w') as f:
json.dump(graph_as_list, f, indent=4)
def fetch_graph_from_database(config):
# MySQL 데이터베이스 연결
conn = pymysql.connect(
host=config['host'],
user=config['user'],
password=config['password'],
database=config['database'],
port=config['port']
)
cursor = conn.cursor()
# peopleActor 테이블에서 데이터 가져오기
cursor.execute("SELECT peopleId, movieId FROM kmovieActor")
rows = cursor.fetchall()
# 그래프 딕셔너리 초기화
people_to_movie = {}
movie_to_people = {}
# 데이터를 딕셔너리로 변환
for row in rows:
people_id, movie_id = row
# peopleId를 키로 하는 딕셔너리
if people_id not in people_to_movie:
people_to_movie[people_id] = set()
people_to_movie[people_id].add(movie_id)
# movieId를 키로 하는 딕셔너리
if movie_id not in movie_to_people:
movie_to_people[movie_id] = set()
movie_to_people[movie_id].add(people_id)
# 데이터베이스 연결 종료
conn.close()
return people_to_movie, movie_to_people
# JSON 파일에서 연결 정보 읽기
with open('config.json', 'r') as f:
config = json.load(f)
# 그래프 데이터 가져오기
people_to_movie, movie_to_people = fetch_graph_from_database(config)
# 그래프 데이터를 JSON 파일로 저장
people_to_movie_json_filename = 'people_to_movie.json'
movie_to_people_json_filename = 'movie_to_people.json'
save_graph_to_json(people_to_movie, people_to_movie_json_filename)
save_graph_to_json(movie_to_people, movie_to_people_json_filename)
print(f"Graph data saved to {people_to_movie_json_filename} and {movie_to_people_json_filename}")