赞
踩
打开cmd切换到neo4j安装目录的bin下,输入以下命令
neo4j console
def write2csv2(entities, relations): out_path = r'D:\Neo4j\XXX\import' entity_types = {"装备": "equipment", "组织": "organization"} relation_types = {'配备': 'Equip'} for et in entity_types: with open(os.path.join(out_path, entity_types[et] + '.csv'), 'w', newline='', encoding='utf-8') as file: writer = csv.writer(file) # 写入数据 writer.writerow([entity_types[et]]) for e in entities: if e['label'] == et: writer.writerow([e['text']]) for rt in relation_types: with open(os.path.join(out_path, relation_types[rt] + '.csv'), 'w', newline='', encoding='utf-8') as file: writer = csv.writer(file) # 写入数据 writer.writerow(["from_entity", "to_entity"]) for r in relations: if r['type'] == rt: # print(r['from_entity']['label']) writer.writerow( [r['from_entity']['text'], r['to_entity']['text']]) # 关闭CSV文件 file.close()
load csv with headers
from 'file:///entity.csv' as line
fieldterminator ','
create (
p:entity_name{
entity_name: line.entity_name
}
)
load csv with headers from "file:///relation.csv"
as row
merge (f1:from_entity_name{name:row.from_entity})
merge (f2:to_entity_name{name:row.to_entity})
merge (f1)-[r:relation_name]->(f2)
MATCH (n) RETURN n
MATCH (n) RETURN n LIMIT 25
MATCH p=()-->() RETURN p
MATCH p=()-[r:relation_name]->() RETURN p LIMIT 25
match (n) detach delete n
如果想读取不在import directory中的CSV,则:
(1)先改变neo4j默认设置,即:删除dbms.directories.import=import或者在该语句前加“#”;
(2)使用 “LOAD CSV FROM file:///C:/XXX/name.csv”(即:file:///+绝对路径), 导入本地CSV文件 。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。