Reputation: 41
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.
Upvotes: 2
Views: 2149
Reputation: 11
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)
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.
Upvotes: 1
Reputation: 8898
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)
Upvotes: 3