CONSTRUCT and DESCRIBE
CONSTRUCT and DESCRIBE queries return RDF data in N-Triples format as raw bytes.
CONSTRUCT queries
Section titled “CONSTRUCT queries”CONSTRUCT queries create RDF triples based on a template:
from sparqlite import SPARQLClient
with SPARQLClient("https://opencitations.net/meta/sparql") as client: # Build triples linking authors to their articles result = client.construct(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX pro: <http://purl.org/spar/pro/> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT { ?author foaf:name ?name ; foaf:made ?article . ?article dcterms:title ?title . } WHERE { ?article a fabio:JournalArticle ; dcterms:title ?title ; pro:isDocumentContextFor ?role . ?role pro:withRole pro:author ; pro:isHeldBy ?author . ?author foaf:name ?name . } LIMIT 10 """)
# Result is raw N-Triples bytes print(result.decode())DESCRIBE queries
Section titled “DESCRIBE queries”DESCRIBE queries return all triples related to a resource:
with SPARQLClient("https://opencitations.net/meta/sparql") as client: result = client.describe(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/>
DESCRIBE ?journal WHERE { ?journal a fabio:Journal ; dcterms:title ?title . } LIMIT 1 """)
# Result is raw N-Triples bytes print(result.decode())Working with N-Triples
Section titled “Working with N-Triples”The raw bytes can be parsed or processed as needed:
with SPARQLClient("https://opencitations.net/meta/sparql") as client: result = client.construct(query)
# Decode to string ntriples_str = result.decode()
# Write to file with open("output.nt", "wb") as f: f.write(result)
# Parse lines for line in result.decode().strip().split("\n"): if line: print(line)