Bondolin
Bondolin

Reputation: 3121

How to use Jena SPARQL UUID?

Is there a way to generate UUID's in SPARQL? I'm using the Jena-Fuseki version. I cannot use b-nodes. Thanks!

Upvotes: 1

Views: 1430

Answers (3)

RobV
RobV

Reputation: 28655

Since SPARQL 1.1 UUID generation is now part of the standard and has been supported in Jena ARQ for some time now.

The UUID() function will return a UUID as a URI

The StrUUID() function will return a UUID as a String

E.g.

SELECT (UUID() AS ?uuid) (StrUUID() AS ?strUuid) WHERE { }

Upvotes: 3

Joshua Taylor
Joshua Taylor

Reputation: 85883

Jena's ARQ includes a number of built-in functions as SPARQL extensions, including a UUID generator. Many of these functions are listed on the Functions in ARQ page of the ARQ documentation, though uuid is not. Here's an example usage, with Jena's command line tools. The following query is in a file called query.sparql.

prefix afn: <http://jena.hpl.hp.com/ARQ/function#>
prefix ex: <http://example.org/>

construct { 
  ?uuid a ex:UUID
}
where {
  BIND( afn:uuid() as ?uuid ) 
}

The arq command line tool needs an data input, so I created a blank file called data.n3. The construct query returns an RDF graph that says the generated UUID is an ex:UUID.

$ arq --query query.sparql --data data.n3 
@prefix afn:     <http://jena.hpl.hp.com/ARQ/function#> .
@prefix ex:      <http://example.org/> .

<urn:uuid:295322a3-7eab-4e4c-b0a0-ca8ac77f3205>
      <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                    ex:UUID .

With a different serialization format:

$ arq --query query.sparql --data data.n3  --results RDF/XML
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:afn="http://jena.hpl.hp.com/ARQ/function#"
    xmlns:ex="http://example.org/">
  <ex:UUID rdf:about="urn:uuid:10c7c8f0-4154-4bbf-ab0c-c52a4196b804"/>
</rdf:RDF>

Upvotes: 2

Bondolin
Bondolin

Reputation: 3121

Actually, Jena seems to have provided a couple of functions at PREFIX afn: < http://jena.hpl.hp.com/ARQ/function#> -- afn:uuid() (returns an URN) and its counterpart afn:struuid().

Upvotes: 0

Related Questions