使用py2neo从CSV文件创建Neo4j图形数据库 [英] Creating neo4j graph database from csv file using py2neo

查看:1012
本文介绍了使用py2neo从CSV文件创建Neo4j图形数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究博士学位,并且对Py2neo感兴趣,因此我正在使用它来使用社交图进行一些实验.但是我遇到了新手麻烦.请问这些简单的问题.

I am currently working in a doctoral program and i am interested about Py2neo, so I am using it in order to perform some experiments using social graphs. However I got into newbie troubles. Excuse me for asking these simple questions.

我得到了一个xml数据集,其中包含有关新闻出版物的数据,我将其转换为一个csv表,大约有700条记录,每条记录由四个字段组成:日期,标题,关键字,作者.因此,我的第一个问题是如何以编程方式从该表创建图形.我考虑过编写一个python脚本,该脚本循环csv表,读取每个行和列字段,然后写入节点. +++++++++++++++++++++++++++++++++++++++++++++代码++++ +++++++++++++++++++++++++++++++++++++++++

I got a xml dataset containing data about publications of a jornal, I have converted it into a csv table, there are about 700 records and each record is composed by four fiels: date, title, keywords, author. So my first question is how to create a graph from this table programatically. I considered writing a python script which loops the csv table, reads for each row and columns fields and writes into nodes. +++++++++++++++++++++++++++++++++++++++++++++ Code +++++++++++++++++++++++++++++++++++++++++++

   #!/usr/bin/env python
   #
   import csv
   from py2neo import neo4j, cypher
   from py2neo import node,  rel

   # calls database service of Neo4j
   #
   graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
   #
   # Create nodes and relationships from a csv table
   # since it's a csv table, a reader must be invoked


   ifile  = open('testeout5_cp.csv', "rb")
   reader = csv.reader(ifile)

   # clear database
   graph_db.clear()

   rownum = 0
   for row in reader:
        colnum = 0
        for col in row:
            titulo, autor, rel = graph_db.create(
            {"titulo": col[1]}, {"autor": col[3]}, (1, "eh_autor_de", 0)
            )
            print(titulo,  autor)  
   rownum += 1

   ifile.close()

================= 我得到以下输出(片段): Python 2.7.5(默认,2013年8月22日,09:31:58) [aires2,Standard]上的[G​​CC 4.8.1 20130603(Red Hat 4.8.1-1)]

================ I got this output (Fragment): Python 2.7.5 (default, Aug 22 2013, 09:31:58) [GCC 4.8.1 20130603 (Red Hat 4.8.1-1)] on aires2, Standard

    (Node('http://localhost:7474/db/data/node/10392'), Node('http://localhost:7474/db/data /node/10393'))
    (Node('http://localhost:7474/db/data/node/10394'), Node('http://localhost:7474/db/data/node/10395'))
    (Node('http://localhost:7474/db/data/node/10396'), Node('http://localhost:7474/db/data/node/10397'))
    (Node('http://localhost:7474/db/data/node/10398'), Node('http://localhost:7474/db/data/node/10399'))
    (Node('http://localhost:7474/db/data/node/10400'), Node('http://localhost:7474/db/data/node/10401'))
    (Node('http://localhost:7474/db/data/node/10402'), Node('http://localhost:7474/db/data/node/10403'))
    (Node('http://localhost:7474/db/data/node/10404'), Node('http://localhost:7474/db/data/node/10405'))

========== 怎么了?

========= What is wrong?

推荐答案

我认为没有错,您的代码看起来不错.

I think there is nothing wrong, your code looks good.

您打印节点并获得正确的py2neo节点实例.尝试print(titulo, autor, rel)查看是否也创建了您的关系.

You print the nodes and get proper py2neo node instances. Try print(titulo, autor, rel) to see if your relationship is also created.

只需使用http://localhost:7474/webadmin/上的Web界面检查数据是否存在.由于没有太多节点,因此可以尝试使用简单的密码查询来获取所有节点,并检查是否一切正常.

Just check with the webinterface at http://localhost:7474/webadmin/ if your data is there. Since you don't have too many nodes, you could try a simple cypher query to get all nodes and check if everything is ok.

START n=node(*) RETURN n;

这篇关于使用py2neo从CSV文件创建Neo4j图形数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆