Importing results from query

I would like to use a script to export part of the data from a database and then import it in another database.

I can use stardog query to export the desired partition of my data. However, the result formats (such as CSV, TEXT, SPARQL, etc) do not seem to be accepteb by stardog data add.

How can I achieve this?

You can use a CONSTRUCT query, then your results will be in RDF so you can add those to a database.

Or you can get fancy and use an INSERT ... WHERE ... query against the target database while fetching data from your source database using SERVICE.

Cheers,
Pavel

Thanks, I have not thought of CONSTRUCT, it might work, I will give it a try.

The second option is not valid since I need to move it to a place where no direct network connection is possible.

CONSTRUCT seems to work, although it is getting more than desired. Is it possible to restrict the results only to the default graph? (ie. not querying any named graphs) I have the option "query all graphs" activated in the database and should not remove this even temporarily. I just need for this specific query to be able to say "only query the default graph, not any named graph".

I cannot find this in the manual.

Sure, you can use FROM <tag:stardog:api:context:default> in your query to limit the results to the default graph only.

Cheers,
Pavel

1 Like

Great, many thanks!

I suppose there is no "standard" way to do it in SPARQL. Before, I tried to get the name of the default graph (select distinct ?g where { graph ?g {?s ?p ?o} }) and then use it in the FROM clause (ie. FROM <def_name_url>) but it did not work. It only showed my preffixes.

Yeah, managing query datasets is a bit odd part of the SPARQL spec. The spec intentionally does not define the query dataset in case the query doesn't use either FROM or FROM NAMED. In that case the dataset is determined by the vendor, and Stardog uses either the default graph or all graphs depending on query.all.graphs for patterns outside of graph g { ... }.

If you don't use query.all.graphs you can still run SPARQL queries without any Stardog-specific URIs but you'd need to manually UNION scans in the default and named scopes, e.g. { ?s ?p ?o } UNION { graph ?g { ?s ?p ?o } } to get all data. But if you do use query.all.graphs=true, then you need FROM <tag:stardog:api:context:default> to access the default graph because it really does not have any name in the SPARQL spec.

Cheers,
Pavel

1 Like

Great answer. Many thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.