2

I'm quite new to linked data and rdflib and I'm quite lost. I am trying to use rdflib to make a persistence store with 'Sleepycat' to load the DBLP database rdf file and then start querying it.This is what I've done:

import rdflib

graph = rdflib.Graph("Sleepycat")
graph.open("C:\Users\Maral\Desktop\Springer-DBLP\Mydblp", create=True)
graph.parse("C:\Users\Maral\Desktop\dblp.rdf", format = 'xml')

It took almost 2 hours but now it seems that dblp.rdf is loaded, parsed and stored in Mydblp. But len(graph) returns 0, and I don't know how to access the data and query it.

Am I missing any steps? Is the data correctly loaded? All the examples are about adding triples to graphs but I just want to query what is already there.

Thank you.

DMD
  • 103
  • 1
  • 8

2 Answers2

5

here is a working example,

from rdflib import ConjunctiveGraph, Namespace, Literal
from rdflib.store import NO_STORE, VALID_STORE

graph = ConjunctiveGraph('Sleepycat')

rt = graph.open("C:\Users\Maral\Desktop\Springer-DBLP\Mydblp", create=False)

if rt == NO_STORE:
    # There is no underlying Sleepycat infrastructure, create it
    graph.open(path, create=True)
else:
    assert rt == VALID_STORE, 'The underlying store is corrupt'

print('Triples in graph before add: ', len(graph))

ontologies = rdflib.Graph()
ontologies.parse('C:\Users\Maral\Desktop\dblp.rdf',format='xml')
for onto in ontologies:
    graph.add(onto)
print ('Triples in graph after add: ', len(graph))

print (graph.serialize(format='xml'))

# close when done, otherwise sleepycat will leak lock entries. 
graph.close()
Slimane amiar
  • 934
  • 12
  • 27
3

I was going through the exact same problem few days ago. After quite a bit of experimenting I was able to run SPARQL query over my local triplestore. Though this question was asked year ago, I hope my ans will help other people.

Here is what I did (Skipping the part about adding the triples to the triple store.):

from rdflib import ConjunctiveGraph, Namespace, Literal
import rdflib
from rdflib import plugin
path = './mytriplestore'
graph = ConjunctiveGraph('Sleepycat')
graph.open(path, create = False)

query = """SELECT *
   WHERE {
     ?s ?p ?o.
   }
   Limit 10"""

qres = graph.query(query)
print qres
for row in qres:
    print row
RDangol
  • 179
  • 9