A.Dumas
A.Dumas

Reputation: 81

How to get UUID() from INSERT Sparql Request?

I would like to know if there is a possibility to retrieve UUID() urn: when using INSERT Statement in SPARQL query ? My problem is simple but I don't know how to solve it using SPARQL : I would like to store a lot of timestamp values. Same timestamp can appear multiple times, so I guess I can use UUID() to generate random URI. I need urn: from UUID() function to relate my new triples. I'm right ? Or UUID() is not the solution ?

Thanks for your help.

EDIT :

Ok, so I have to say I would like to retrieve data in my python code. I am using SPARQLWrapper to run my requests.

If I create one INSERT request like that :

INSERT { 
    ?uuid1 rdf:type capt:ECHANTILLON .
    ?uuid1 capt:Nom ?uuid1 .
    ?uuid1 capt:Description '2019-08-07 16:07:34.027636' .
    ?uuid1 capt:Valeur '1565182189' .
} WHERE { 
    FILTER NOT EXISTS { ?uuid1 rdf:type capt:ECHANTILLON} .
    BIND (UUID() AS ?uuid1) . 
};

Then an other INSERT request using ?uuid1 from the first :

INSERT { 
    ?uuid2 rdf:type capt:VALEUR .
    ?uuid2 capt:Nom ?uuid2 .
    ?uuid2 capt:Description '2019-08-07 16:07:34.027659' .
    ?uuid2 capt:Valeur '27.0' .
    **?uuid1 capt:A_Pour_Valeur ?uuid2 .**  <===
} WHERE { 
    FILTER NOT EXISTS { ?uuid2 rdf:type capt:VALEUR} .
    BIND (UUID() AS ?uuid2) . 
};

What I want is :

uuid1 = endpoint.setQuery(request_1)
request_2.addSubject(uuid1)
uuid2 = endpoint.setQuery(request_2)

Something like that.

How can I retrieve ?uuid1 from the first request if INSERT does not return this value ? I would like to make two requests if possible. Have I to regroup two requests in one request, or have I to run a SELECT ?uuid1 request before running second ?

Upvotes: 1

Views: 1021

Answers (1)

Jeen Broekstra
Jeen Broekstra

Reputation: 22052

You can't get the value of the UUID directly from the SPARQL update - if you want to retrieve it via SPARQL somehow, you'll have to do a query after you've inserted it - or, of course, you could adapt your second SPARQL update to do the selection for you by querying for the 'correct' UUID in its WHERE clause.

However, in this case, that looks difficult to do, and I think the easiest solution for you is that you don't create the UUID in the SPARQL operation itself, but instead create it in code and then use that in your query string, e.g. by adding a VALUES clause, something like this:

  import uuid

  uuid1 = uuid.uuid1()
  update = """INSERT {  .... }
              WHERE { VALUES ?uuid1 { <urn:uuid:""" + uuid1 + """> }
                      FILTER NOT EXISTS .... (etc) }"""

Upvotes: 2

Related Questions