Python處理JSON數(shù)據(jù)并導(dǎo)入Neo4j數(shù)據(jù)庫
引言
在數(shù)據(jù)處理和分析中,JSON是一種常見的數(shù)據(jù)格式。Neo4j是一個(gè)高性能的圖數(shù)據(jù)庫,能夠存儲(chǔ)和查詢復(fù)雜的網(wǎng)絡(luò)關(guān)系。本文將通過解析一段Python代碼,詳細(xì)介紹如何處理JSON數(shù)據(jù)并將其導(dǎo)入Neo4j數(shù)據(jù)庫。
代碼結(jié)構(gòu)概覽
首先,我們來看一下代碼的整體結(jié)構(gòu):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time :2022/9/13 10:03
# @File :handler_person_data.py
# @Description: 處理json數(shù)據(jù)
import json
import os
from common import constant
from common.conn_neo4j import ConnNeo4j
# 獲得數(shù)據(jù)文件的路徑
data_path = os.path.join(constant.DATA_DIR, "data-json.json")
# 讀取數(shù)據(jù)文件的內(nèi)容
data = json.load(open(data_path, 'r', encoding='utf-8'))
print("人物數(shù)目:", len(data))
# 連接Neo4j服務(wù)器
neo4j = ConnNeo4j()
# 遍歷數(shù)據(jù)
for item in data:
item['name'] = item['中文名']
# 畢業(yè)于
school = []
if '畢業(yè)于' in item.keys():
school = item['畢業(yè)于']
item.pop('畢業(yè)于')
# 作品
works = []
if '作品' in item.keys():
works = item['作品']
item.pop('作品')
# 相關(guān)人物
relate_persons = {}
if '相關(guān)人物' in item.keys():
relate_persons = item['相關(guān)人物']
item.pop('相關(guān)人物')
print(item)
# 創(chuàng)建人物節(jié)點(diǎn)
neo4j.create_node("人物", item)
# 創(chuàng)建學(xué)校節(jié)點(diǎn),人物與學(xué)校間的關(guān)系
neo4j.create_node_relations("人物", item, "學(xué)校", school, "畢業(yè)于", {'type': '畢業(yè)于'}, False)
# 創(chuàng)建作品節(jié)點(diǎn),人物與作品間的關(guān)系
neo4j.create_node_relations("人物", item, "作品", works, "創(chuàng)作", {'type': '創(chuàng)作'}, False)
# 創(chuàng)建相關(guān)人物,人物社會(huì)關(guān)系
for key in relate_persons.keys():
tmp_value = relate_persons[key]
tmp_rel_type = key
if key in ['兒子', '女兒', '父親', '母親']:
neo4j.create_node_relations("人物", item, "人物", tmp_value, tmp_rel_type, {'type': '親子'}, False)
elif key in ['孫子', '孫女', '爺爺', '奶奶']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '祖孫'}, False)
elif key in ['哥哥', '妹妹', '弟弟', '姐姐']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '兄弟姐妹'}, False)
elif key in ['丈夫', '妻子']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '夫妻'}, False)
elif key in ['女婿', '兒媳']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '婿媳'}, False)
elif key in ['學(xué)生', '老師']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '師生'}, False)
else:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '其他'}, False)
代碼詳解
1. 導(dǎo)入必要的庫
import json import os from common import constant from common.conn_neo4j import ConnNeo4j
json:用于處理JSON格式的數(shù)據(jù)。
os:用于處理文件路徑。
constant:從common模塊中導(dǎo)入的常量,可能包含數(shù)據(jù)目錄等信息。
ConnNeo4j:從common.conn_neo4j模塊中導(dǎo)入的Neo4j連接類。
2. 定義數(shù)據(jù)文件路徑
# 獲得數(shù)據(jù)文件的路徑 data_path = os.path.join(constant.DATA_DIR, "data-json.json")
data_path:指向包含數(shù)據(jù)的JSON文件路徑。
3. 讀取JSON文件內(nèi)容
# 讀取數(shù)據(jù)文件的內(nèi)容
data = json.load(open(data_path, 'r', encoding='utf-8'))
print("人物數(shù)目:", len(data))
使用json.load()函數(shù)讀取JSON文件的內(nèi)容,并將其存儲(chǔ)在data變量中。
打印出數(shù)據(jù)中的人物數(shù)目。
4. 連接Neo4j服務(wù)器
# 連接Neo4j服務(wù)器 neo4j = ConnNeo4j()
創(chuàng)建一個(gè)ConnNeo4j對(duì)象,用于連接Neo4j數(shù)據(jù)庫。
5. 遍歷數(shù)據(jù)并處理
# 遍歷數(shù)據(jù)
for item in data:
item['name'] = item['中文名']
# 畢業(yè)于
school = []
if '畢業(yè)于' in item.keys():
school = item['畢業(yè)于']
item.pop('畢業(yè)于')
# 作品
works = []
if '作品' in item.keys():
works = item['作品']
item.pop('作品')
# 相關(guān)人物
relate_persons = {}
if '相關(guān)人物' in item.keys():
relate_persons = item['相關(guān)人物']
item.pop('相關(guān)人物')
print(item)
# 創(chuàng)建人物節(jié)點(diǎn)
neo4j.create_node("人物", item)
# 創(chuàng)建學(xué)校節(jié)點(diǎn),人物與學(xué)校間的關(guān)系
neo4j.create_node_relations("人物", item, "學(xué)校", school, "畢業(yè)于", {'type': '畢業(yè)于'}, False)
# 創(chuàng)建作品節(jié)點(diǎn),人物與作品間的關(guān)系
neo4j.create_node_relations("人物", item, "作品", works, "創(chuàng)作", {'type': '創(chuàng)作'}, False)
# 創(chuàng)建相關(guān)人物,人物社會(huì)關(guān)系
for key in relate_persons.keys():
tmp_value = relate_persons[key]
tmp_rel_type = key
if key in ['兒子', '女兒', '父親', '母親']:
neo4j.create_node_relations("人物", item, "人物", tmp_value, tmp_rel_type, {'type': '親子'}, False)
elif key in ['孫子', '孫女', '爺爺', '奶奶']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '祖孫'}, False)
elif key in ['哥哥', '妹妹', '弟弟', '姐姐']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '兄弟姐妹'}, False)
elif key in ['丈夫', '妻子']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '夫妻'}, False)
elif key in ['女婿', '兒媳']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '婿媳'}, False)
elif key in ['學(xué)生', '老師']:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '師生'}, False)
else:
neo4j.create_node_relations('人物', item, '人物', tmp_value, tmp_rel_type, {'type': '其他'}, False)
遍歷data中的每個(gè)JSON對(duì)象。
將中文名字段重命名為name。
處理畢業(yè)于、作品和相關(guān)人物字段,并將其從JSON對(duì)象中移除。
打印處理后的JSON對(duì)象。
調(diào)用neo4j.create_node()方法創(chuàng)建人物節(jié)點(diǎn)。
調(diào)用neo4j.create_node_relations()方法創(chuàng)建學(xué)校、作品和相關(guān)人物節(jié)點(diǎn),并建立相應(yīng)的關(guān)系。
總結(jié)
通過這段代碼,我們學(xué)會(huì)了如何從JSON文件中提取數(shù)據(jù),并將其導(dǎo)入Neo4j數(shù)據(jù)庫。這個(gè)過程包括讀取JSON文件、處理數(shù)據(jù)、創(chuàng)建節(jié)點(diǎn)和關(guān)系。希望這篇文章對(duì)你理解如何處理JSON數(shù)據(jù)并導(dǎo)入Neo4j數(shù)據(jù)庫有所幫助。
到此這篇關(guān)于Python處理JSON數(shù)據(jù)并導(dǎo)入Neo4j數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Python處理JSON數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)(六)支持向量機(jī)
這篇文章主要介紹了python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)第六篇,支持向量機(jī)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
python元組和字典的內(nèi)建函數(shù)實(shí)例詳解
這篇文章主要介紹了python元組和字典的內(nèi)建函數(shù),結(jié)合實(shí)例形式詳細(xì)分析了Python元組和字典的各種常見內(nèi)建函數(shù)功能與相關(guān)使用技巧,需要的朋友可以參考下2019-10-10
python中列表和元組的用法以及區(qū)別超詳細(xì)講解
這篇文章主要介紹了Python中的列表和元組,包括它們的定義、特點(diǎn)、常見操作以及與列表的區(qū)別,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
Python實(shí)現(xiàn)決策樹并且使用Graphviz可視化的例子
今天小編就為大家分享一篇Python實(shí)現(xiàn)決策樹并且使用Graphviz可視化的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python selenium 三種等待方式詳解(必會(huì))
這篇文章主要介紹了Python selenium 三種等待方式詳解(必會(huì))的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

