2

New Question:

I firstly start the Fuseki Server to create a new dataset called 'address_act':

fuseki-server --update --mem /address_act

here are the code to get data of each address and then add it to a Triplestore (database) in Fuseki Server:

import requests
import pandas as pd
import numpy as np
import re
from rdflib import Graph, Literal, URIRef
from rdflib.plugins.stores import sparqlstore

query_endpoint = 'http://localhost:3030/address_act/query'
update_endpoint = 'http://localhost:3030/address_act/update'
store = sparqlstore.SPARQLUpdateStore()
store.open((query_endpoint, update_endpoint))
g = Graph(identifier = URIRef('http://www.example.com'))

for i in range(1,3):
    results = []
    url = 'http://gnafld.net/address/?per_page=10&page=' + str(i)
    page = requests.get(url)
    response = requests.get(url)
    response.raise_for_status()
    results = re.findall('\"Address ID: (GAACT[0-9]+)\"', response.text)

    for ad in results:
        ad_url = 'http://gnafld.net/address/' + ad
        ad_info = requests.get(ad_url).content

        g.parse(data=ad_info, format='turtle')
        store.add_graph(g)

It seems that the code works but when I browse http://localhost:3030/dataset.html?tab=info&ds=/address_act, It always shows that there is 0 triples in the graph.no triples in the graph I wonder whether it inserts the triples into the dataset successfully. If yes, then where can I find those triples? If not, how can I add the triples into the default graph? Any help is highly appreciated.

SpongeBob
  • 41
  • 5

2 Answers2

2

If you start fuseki like this:

$ fuseki-server --update --mem /ds

Then you can access it locally using:

from rdflib import Graph, Literal, URIRef
from rdflib.plugins.stores import sparqlstore

query_endpoint = 'http://localhost:3030/ds/query'
update_endpoint = 'http://localhost:3030/ds/update'
store = sparqlstore.SPARQLUpdateStore()
store.open((query_endpoint, update_endpoint))

...use store...
store.add_graph(graph)
store.remove_graph(graph)
store.query(...)

(ds is name of the fuseki dataset)

It looks like you have a turtle format file so:

g = Graph(identifier = URIRef('http://www.example.com/'))
g.parse(data=r, format='turtle')

store.add_graph(g)
user205512
  • 8,798
  • 29
  • 28
  • Thanks for your attention and time. I just run the code but got the error above. I have already edited the problem description. Could you plz help me with that? Can you run it without errors? – SpongeBob May 09 '18 at 05:58
  • Yes, I tried. Then the error is 'SPARQLUpdateStore' object has no attribute 'add_Graph'. Then I change to store.add(g) and it said 'too many values to unpack (expected 3)' as i re-edit above. – SpongeBob May 09 '18 at 09:31
  • `add_graph` - lowercase 'g' – user205512 May 09 '18 at 10:45
  • update: forgot that rdflib 'graph' is a bit odd and includes an 'identifier'. No idea how to just use the default graph, but updated code works. – user205512 May 09 '18 at 10:59
  • It seems that the code works. But in the Fuseki Server, it always shows that 0 triples in the default graph. Then which graph should the inserted triples should be in? – SpongeBob May 11 '18 at 01:01
  • http://www.example.com - that's what the graph identifier does. Apologies, couldn't work out how to get it in the default graph (although you could set fuseki to use default union graph). – user205512 May 11 '18 at 09:59
  • I am really confused whether I insert the triples successfully though the code works because there is 0 triple in the endpoint. And I cannot find any result from http://www.example.com.... – SpongeBob May 11 '18 at 22:00
  • Try `select (count(*) AS ?count) { graph { ?s ?p ?o } }`? – user205512 May 14 '18 at 13:19
  • When I print (len(g)), it shows 22 triples in the graph. But when I try the SPARQL query you mentioned above, it still shows 0 there. Really confused and have no idea why... – SpongeBob May 16 '18 at 00:23
  • I am sorry I did not get your idea. what do you mean by this? – SpongeBob May 17 '18 at 03:32
1

Summary

Took me a while to figure this out but finally and by help of this post, I found a solution:

To load your data into the default graph, you can use the magic url for the default graph provided by rdflib like so:

from rdflib.graph import DATASET_DEFAULT_GRAPH_ID as default

You then pass the default variable to the graph constructor, e.g.

graph = Graph(store, identifier=default)

Complete example

This assumes that apache-jena-fuseki is running on port 3030 on localhost (the default).

from rdflib import Namespace, Graph
from rdflib.namespace import RDF, FOAF
from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID as default

# Connect to fuseki triplestore.
store = SPARQLUpdateStore()
query_endpoint = 'http://localhost:3030/test/query'
update_endpoint = 'http://localhost:3030/test/update'
store.open((query_endpoint, update_endpoint))

# Define example namespace.
ex = Namespace("http://example.org/")

# Define a node.
node = (ex.me, RDF.type, FOAF.Person)

# Open a graph in the open store and set identifier to default graph ID.
graph = Graph(store, identifier=default)

# Add node to graph.
graph.add(node)

You should then see one (new) node in the fuseki dataset admin panel, see this screenshot of fuseki dataset info panel after inserting one node into default graph using rdflib.

c4r510
  • 11
  • 3