-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_neo4j_data.py
172 lines (162 loc) · 7.56 KB
/
load_neo4j_data.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# load_neo4j_data.py
import json
from neo4j import GraphDatabase
import os
def load_data_to_neo4j(file_path):
# Neo4j 데이터베이스 연결 설정
uri = "bolt://localhost:7687"
user = "neo4j"
password = "nextpage"
# 파일에서 데이터 로드 -> 안돼서 그냥 데이터 집어넣기로함
with open(file_path, 'r') as file:
#data = json.load(file)
data = [
# # 사용자 데이터
# {
# "model": "user.User",
# "fields": {
# "user_id": "unique-user-id-001",
# "nickname": "Kanguk",
# "created_at": "2024-01-08T06:39:55.329190",
# "updated_at": "2024-01-08T06:39:55.329261",
# "is_deleted": False
# }
# },
# 스토리 데이터
{
"model": "story.Story",
"fields": {
"story_id": "unique-story-id-001",
"content": "이것은 최초의 스토리 내용입니다.",
"created_at": "2024-01-08T07:00:00.000000",
"updated_at": "2024-01-08T07:00:00.000000",
"is_deleted": "최초",
"image_url": "http://example.com/image1.jpg",
"parent_story": None,
"child_stories": ["unique-story-id-002", "unique-story-id-003"]
}
},
{
"model": "story.Story",
"fields": {
"story_id": "unique-story-id-002",
"content": "이것은 첫 번째 분기 스토리 내용입니다.",
"created_at": "2024-01-08T08:00:00.000000",
"updated_at": "2024-01-08T08:00:00.000000",
"is_deleted": "첫번째",
"image_url": "http://example.com/image2.jpg",
"parent_story": "unique-story-id-001",
"child_stories": ["unique-story-id-004"]
}
},
{
"model": "story.Story",
"fields": {
"story_id": "unique-story-id-003",
"content": "이것은 두 번째 분기 스토리 내용입니다.",
"created_at": "2024-01-08T09:00:00.000000",
"updated_at": "2024-01-08T09:00:00.000000",
"is_deleted": "두번째",
"image_url": "http://example.com/image3.jpg",
"parent_story": "unique-story-id-001",
"child_stories": ["unique-story-id-005"]
}
},
{
"model": "story.Story",
"fields": {
"story_id": "unique-story-id-004",
"content": "이것은 첫 번째의 첫번째 분기 스토리 내용입니다.",
"created_at": "2024-01-08T09:00:00.000000",
"updated_at": "2024-01-08T09:00:00.000000",
"is_deleted": "첫첫번째",
"image_url": "http://example.com/image3.jpg",
"parent_story": "unique-story-id-002",
"child_stories": ["unique-story-id-006","unique-story-id-007"]
}
},
{
"model": "story.Story",
"fields": {
"story_id": "unique-story-id-006",
"content": "이것은 첫 번째의 첫 번째의 첫 번째 분기 스토리 내용입니다.",
"created_at": "2024-01-08T09:00:00.000000",
"updated_at": "2024-01-08T09:00:00.000000",
"is_deleted": "첫첫첫번째",
"image_url": "http://example.com/image3.jpg",
"parent_story": "unique-story-id-004",
"child_stories": []
}
},
{
"model": "story.Story",
"fields": {
"story_id": "unique-story-id-007",
"content": "이것은 첫 번째의 첫 번째의 두 번째 분기 스토리 내용입니다.",
"created_at": "2024-01-08T09:00:00.000000",
"updated_at": "2024-01-08T09:00:00.000000",
"is_deleted": "첫첫두번째",
"image_url": "http://example.com/image3.jpg",
"parent_story": "unique-story-id-004",
"child_stories": []
}
},
{
"model": "story.Story",
"fields": {
"story_id": "unique-story-id-005",
"content": "이것은 두 번째의 첫 번째 분기 스토리 내용입니다.",
"created_at": "2024-01-08T09:00:00.000000",
"updated_at": "2024-01-08T09:00:00.000000",
"is_deleted": "두첫번째",
"image_url": "http://example.com/image3.jpg",
"parent_story": "unique-story-id-003",
"child_stories": []
}
}
]
# Neo4j에 데이터 삽입
driver = GraphDatabase.driver(uri, auth=(user, password))
with driver.session() as session:
for item in data:
fields = item['fields']
if item['model'] == 'user.User':
session.run(
"CREATE (u:User {user_id: $user_id, nickname: $nickname, created_at: datetime($created_at), updated_at: datetime($updated_at), is_deleted: $is_deleted})",
user_id=fields['user_id'],
nickname=fields['nickname'],
created_at=fields['created_at'],
updated_at=fields['updated_at'],
is_deleted=fields['is_deleted']
)
elif item['model'] == 'story.Story':
session.run(
"CREATE (s:Story {story_id: $story_id, content: $content, created_at: datetime($created_at), updated_at: datetime($updated_at), is_deleted: $is_deleted, image_url: $image_url})",
story_id=fields['story_id'],
content=fields['content'],
created_at=fields['created_at'],
updated_at=fields['updated_at'],
is_deleted=fields['is_deleted'],
image_url=fields['image_url']
)
# 관계 생성
for item in data:
fields = item['fields']
if item['model'] == 'story.Story':
# 부모 스토리와의 관계 생성 자식 -> 부모
# if fields['parent_story']:
# session.run(
# "MATCH (parent:Story {story_id: $parent_story_id}), (child:Story {story_id: $child_story_id}) CREATE (child)-[:ParentStory]->(parent)",
# parent_story_id=fields['parent_story'],
# child_story_id=fields['story_id']
# )
# 자식 스토리들과의 관계 생성 부모 -> 자식
for child_id in fields['child_stories']:
session.run(
"MATCH (parent:Story {story_id: $parent_story_id}), (child:Story {story_id: $child_story_id}) CREATE (parent)-[:ChildStory]->(child)",
parent_story_id=fields['story_id'],
child_story_id=child_id
)
driver.close()
if __name__ == "__main__":
load_data_to_neo4j("init_data.json")