Skip to content

Instantly share code, notes, and snippets.

@mommi84
Created October 31, 2018 14:23
Show Gist options
  • Save mommi84/25375fb3e97d83840ea5f8a0c83fd399 to your computer and use it in GitHub Desktop.
Save mommi84/25375fb3e97d83840ea5f8a0c83fd399 to your computer and use it in GitHub Desktop.
SPARQL Query Execution in Python 2.7
import urllib2, urllib, httplib, json
import sys
MAX_RESULTS = 10000
graph = ""
endpoint = ""
# Execute a SPARQL query.
def sparql_query(query):
param = dict()
param["default-graph-uri"] = graph
param["query"] = query
param["format"] = "JSON"
param["CXML_redir_for_subjs"] = "121"
param["CXML_redir_for_hrefs"] = ""
param["timeout"] = "600000" # ten minutes - works with Virtuoso endpoints
param["debug"] = "on"
try:
resp = urllib2.urlopen(endpoint + "?" + urllib.urlencode(param))
j = resp.read()
resp.close()
except (urllib2.HTTPError, httplib.BadStatusLine):
print("*** Query error. Empty result set. ***")
return []
return json.loads(j)["results"]["bindings"]
# Execute a SPARQL query and iterates over the offsets to get all results.
def sparql_query_all(query):
results = []
for i in xrange(sys.maxint):
offset = i * MAX_RESULTS
part = sparql_query("{} OFFSET {} LIMIT 10000".format(query, offset))
results += part
if len(part) < MAX_RESULTS:
break
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment