How to prevent triples from getting mixed up while uploading to Dydra programmatically?

2024/10/12 14:21:31

I am trying to upload some data to Dydra from a Sesame triplestore I have on my computer. While the download from Sesame works fine, the triples get mixed up (the s-p-o relationships change as the object of one becomes object of another). Can someone please explain why this is happening and how it can be resolved? The code is below:

#Querying the triplestore to retrieve all results
sesameSparqlEndpoint = 'http://my.ip.ad.here:8080/openrdf-sesame/repositories/rep_name'
sparql = SPARQLWrapper(sesameSparqlEndpoint)
queryStringDownload = 'SELECT * WHERE {?s ?p ?o}'
dataGraph = Graph()sparql.setQuery(queryStringDownload)
sparql.method = 'GET'
sparql.setReturnFormat(JSON)
output = sparql.query().convert()
print outputfor i in range(len(output['results']['bindings'])):#The encoding is necessary to parse non-English charactersoutput['results']['bindings'][i]['s']['value'].encode('utf-8')try:subject_extract = output['results']['bindings'][i]['s']['value']if 'http' in subject_extract:subject = "<" + subject_extract + ">"subject_url = URIRef(subject)print subject_urlpredicate_extract = output['results']['bindings'][i]['p']['value']if 'http' in predicate_extract:predicate = "<" + predicate_extract + ">"predicate_url = URIRef(predicate)print predicate_urlobjec_extract = output['results']['bindings'][i]['o']['value']if 'http' in objec_extract:objec = "<" + objec_extract + ">"objec_url = URIRef(objec)print objec_urlelse:objec = objec_extractobjec_wip = '"' + objec + '"'objec_url = URIRef(objec_wip)# Loading the data on a graph       dataGraph.add((subject_url,predicate_url,objec_url))except UnicodeError as error: print error#Print all statements in dataGraph      
for stmt in dataGraph:pprint.pprint(stmt)# Upload to Dydra
URL = 'http://dydra.com/login'
key = 'my_key'with requests.Session() as s:resp = s.get(URL)soup = BeautifulSoup(resp.text,"html5lib")csrfToken = soup.find('meta',{'name':'csrf-token'}).get('content')# print csrf_tokenpayload = {'account[login]':key,'account[password]':'','csrfmiddlewaretoken':csrfToken,'next':'/'}# print payloadp = s.post(URL,data=payload, headers=dict(Referer=URL))# print p.textr = s.get('http://dydra.com/username/rep_name/sparql')# print r.textdydraSparqlEndpoint = 'http://dydra.com/username/rep_name/sparql'for stmt in dataGraph:queryStringUpload = 'INSERT DATA {%s %s %s}' % stmtsparql = SPARQLWrapper(dydraSparqlEndpoint)sparql.setCredentials(key,key)sparql.setQuery(queryStringUpload)sparql.method = 'POST'sparql.query()
Answer

A far simpler way to copy your data over (apart from using a CONSTRUCT query instead of a SELECT, like I mentioned in the comment) is simply to have Dydra itself directly access your Sesame endpoint, for example via a SERVICE-clause.

Execute the following on your Dydra database, and (after some time, depending on how large your Sesame database is), everything will be copied over:

   INSERT { ?s ?p ?o }WHERE { SERVICE <http://my.ip.ad.here:8080/openrdf-sesame/repositories/rep_name> { ?s ?p ?o }}

If the above doesn't work on Dydra, you can alternatively just directly access the RDF statements from your Sesame store by using the URI http://my.ip.ad.here:8080/openrdf-sesame/repositories/rep_name/statements. Assuming Dydra has an upload-feature where you can provide the URL of an RDF document, you can simply provide it the above URI and it should be able to load it.

https://en.xdnf.cn/q/118188.html

Related Q&A

Adding a new row to a dataframe in pandas for every iteration

Adding a new row to a dataframe with correct mapping in pandasSomething similar to the above question.carrier_plan_identifier ... hios_issuer_identifier 1 AU…

Twisted client protocol - attaching an interface frontend

I am following the tutorial on writing a client/server pair in Twisted located here:http://twistedmatrix.com/documents/current/core/howto/clients.htmlI have everything working for the communication of …

Get query string as function parameters on flask

Is there a way to get query string as function parameters on flask? For example, the request will be like this.http://localhost:5000/user?age=15&gender=MaleAnd hope the code similar to this.@app.…

cython.parallel cannot see the difference in speed

I tried to use cython.parallel prange. I can only see two cores 50% being used. How can I make use of all the cores. i.e. send the loops to the cores simultaneously sharing the arrays, volume and mc_vo…

Is it possible (how) to add a spot color to pdf from matplotlib?

I am creating a chart which has to use (multiple) spot colors. This color could be one that is neither accessible from RGB nor CMYK. Is there a possibility to specify a spot color for a line in matplot…

Separate keywords and @ mentions from dataset

I have a huge set of data which has several columns and about 10k rows in more than 100 csv files, for now I am concerned about only one column with message format and from them I want to extract two p…

Kivy class in .py and .kv interaction 2

Follow up from Kivy class in .py and .kv interaction , but more complex. Here is the full code of what Im writing: The data/screens/learnkanji_want.kv has how I want the code to be, but I dont fully un…

How to centre an image in pygame? [duplicate]

This question already has an answer here:How to center an image in the middle of the window in pygame?(1 answer)Closed 1 year ago.I am using python 2.7, I would like to know if there is a way to centr…

widget in sub-window update with real-time data in tkinter python

Ive tried using the after/time.sleep to update the treeview, but it is not working with the mainloop. My questions are: How can I update the treeview widget with real-time data? And is there a way th…

Changing for loop to while loop

Wondering how would the following for loop be changed to while loop. While having the same output.for i in range(0,20, 4):print(i)