Instructions on Federated Queries using pystardog


I'm trying to query several databases from pystardog, e.g. DB_a & DB_b are databases in my Stardog Cloud. It's hard to find instructions how to implement it with pystardog.

I see pystardog can connect databases individually, and I was able to run SPARQL queries.

conn = stardog.Connection(DATABASE_NAME, **CONNECTION_DETAILS)
query_test = """
?s ?p ?o .
csv_results =, content_type='text/csv')

How can I implement the same idea as in the following blog?

Run a query that can retrieve results from both DB_a and DB_b? Is there an option that I have to specify in pystardog to access all the databases rather than a single database?


Hi Gloria,

All you need to do is write your query differently; your use of Pystardog is irrelevant in this case. If you change your query to:

      SERVICE <db://DB_a> {
         ?s ?p ?o
      SERVICE <db://DB_b> {
         ?a ?b ?c

you should get the results you want. You can see more at our documentation for federated queries.


Thanks Steve, it works!