Graph databases and RDF triplestores: storage of graph data in python
Asked Answered
W

7

19

I need to develop a graph database in python (I would enjoy if anybody can join me in the development. I already have a bit of code, but I would gladly discuss about it).

I did my research on the internet. in Java, neo4j is a candidate, but I was not able to find anything about actual disk storage. In python, there are many graph data models (see this pre-PEP proposal, but none of them satisfy my need to store and retrieve from disk.

I do know about triplestores, however. triplestores are basically RDF databases, so a graph data model could be mapped in RDF and stored, but I am generally uneasy (mainly due to lack of experience) about this solution. One example is Sesame. Fact is that, in any case, you have to convert from in-memory graph representation to RDF representation and viceversa in any case, unless the client code wants to hack on the RDF document directly, which is mostly unlikely. It would be like handling DB tuples directly, instead of creating an object.

What is the state-of-the-art for storage and retrieval (a la DBMS) of graph data in python, at the moment? Would it make sense to start developing an implementation, hopefully with the help of someone interested in it, and in collaboration with the proposers for the Graph API PEP ? Please note that this is going to be part of my job for the next months, so my contribution to this eventual project is pretty damn serious ;)

Edit: Found also directededge, but it appears to be a commercial product

Westmorland answered 19/8, 2009 at 23:7 Comment(0)
D
5

I have used both Jena, which is a Java framework, and Allegrograph (Lisp, Java, Python bindings). Jena has sister projects for storing graph data and has been around a long, long time. Allegrograph is quite good and has a free edition, I think I would suggest this cause it is easy to install, free, fast and you could be up and going in no time. The power you would get from learning a little RDF and SPARQL may very well be worth your while. If you know SQL already then you are off to a great start. Being able to query your graph using SPARQL would yield some great benefits to you. Serializing to RDF triples would be easy, and some of the file formats are super easy ( NT for instance ). I'll give an example. Lets say you have the following graph node-edge-node ids:

1 <- 2 -> 3
3 <- 4 -> 5

these are already subject predicate object form so just slap some URI notation on it, load it in the triple store and query at-will via SPARQL. Here it is in NT format:

<http://mycompany.com#1> <http://mycompany.com#2> <http://mycompany.com#3> .
<http://mycompany.com#3> <http://mycompany.com#4> <http://mycompany.com#5> .

Now query for all nodes two hops from node 1:

SELECT ?node
WHERE {
    <http://mycompany.com#1> ?p1 ?o1 .
    ?o1 ?p2 ?node .
}

This would of course yield <http://mycompany.com#5>.

Another candidate would be Mulgara, written in pure Java. Since you seem more interested in Python though I think you should take a look at Allegrograph first.

Dunkirk answered 2/1, 2010 at 4:24 Comment(3)
I am indeed considering Allegro.Westmorland
can I easily apply some graph analysis with RDF as well as SPARQL? for example, the distance between two node, shortest-path, traversal?Strenuous
Not that I'm aware of... BTW this question is a good candidate for semanticoverflow.comDunkirk
C
3

I think the solution really depends on exactly what it is you want to do with the graph once you have managed to store it on disk/in database, and this is a little unclear in your question. However, a couple of things you might wish to consider are:

  • if you just want to persist the graph without using any of the features or properties you might expect from an rdbms solution (such as ACID), then how about just pickling the objects into a flat file? Very rudimentary, but like I say, depends on exactly what you want to achieve.
  • ZODB is an object database for Python (a spin off from the Zope project I think). I can't say I've had much experience of it in a high performance environment, but bar a few restrictions does allow you to store Python objects natively.
  • if you wish to pursue RDF, there is an RDF Alchemy project which might help to alleviate some of your concerns about converting from your graph to RDF structures and I think has Sesame as part of it's stack.

There are some other persistence tools detailed on the python site which may be of interest, however I spent quite a while looking into this area last year, and ultimately I found there wasn't a native Python solution that met my requirements.

The most success I had was using MySQL with a custom ORM and I posted a couple of relevant links in an answer to this question. Additionally, if you want to contribute to an RDBMS project, when I spoke to someone from Open Query about a Graph storage engine for MySQL them seemed interested in getting active participation in their project.

Sorry I can't give a more definitive answer, but I don't think there is one... If you do start developing your own implementation, I'd be interested to keep up-to-date with how you get on.

Crumpet answered 20/8, 2009 at 7:59 Comment(0)
O
2

Greetings from your Serius Cybernetics Intelligent Agent!

Some useful links...

Obsessive answered 13/2, 2011 at 20:23 Comment(0)
H
1

Hmm, maybe you should take a look at CubicWeb

Heuer answered 20/8, 2009 at 9:22 Comment(0)
E
1

Regarding Neo4j, did you notice the existing Python bindings? As for the disk storage, take a look at this thread on the mailing list.

For graphdbs in Python, the Hypergraph Database Management System project was recently started on SourceForge by Maurice Ling.

Eccrinology answered 3/9, 2009 at 9:39 Comment(0)
D
0

Redland (http://librdf.org) is probably the solution you're looking for. It has Python bindings too.

Diderot answered 17/7, 2011 at 19:56 Comment(0)
F
0

RDFLib is a python library that you can use. Using harschware's example:

Create a test.nt file like below:

<http://mycompany.com#1> <http://mycompany.com#2> <http://mycompany.com#3> .
<http://mycompany.com#3> <http://mycompany.com#4> <http://mycompany.com#5> .

To query for all nodes two hops from node 1 in RDFLib:

    from rdflib import Graph

    g = Graph()
    g.parse("test.nt", format="nt")

    qres = g.query(
        """SELECT ?node
        WHERE {
            <http://mycompany.com#1> ?p1 ?o1 .
            ?o1 ?p2 ?node .
        }"""
    )

    for row in qres:
        print(node)

Should return the answer <http://mycompany.com#5>.

Faso answered 16/7, 2018 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.