Skip to content

How to export a RDF GRAPH from Virtuoso?

Junjun Cao edited this page Jul 28, 2024 · 3 revisions

Sometimes, it's necessary to export a graph in RDF turtle format, because RDF, being a "text document database", allows for checking and editing directly rather than updating using SPARQL from an endpoint.

1. CREATE PROCEDURE dump_one_graph

Navigate to the ISQL input interface and input following code:

CREATE PROCEDURE dump_one_graph 
  ( IN  srcgraph           VARCHAR
  , IN  out_file           VARCHAR
  , IN  file_length_limit  INTEGER  := 1000000000
  )
  {
    DECLARE  file_name     VARCHAR
  ; DECLARE  env
          ,  ses           ANY
  ; DECLARE  ses_len
          ,  max_ses_len
          ,  file_len
          ,  file_idx      INTEGER
  ; SET ISOLATION = 'uncommitted'
  ; max_ses_len  := 10000000
  ; file_len     := 0
  ; file_idx     := 1
  ; file_name    := sprintf ('%s%06d.ttl', out_file, file_idx)
  ; string_to_file ( file_name || '.graph', 
                     srcgraph, 
                     -2
                   );
    string_to_file ( file_name, 
                     sprintf ( '# Dump of graph <%s>, as of %s\n@base <> .\n', 
                               srcgraph, 
                               CAST (NOW() AS VARCHAR)
                             ), 
                     -2
                   )
  ; env := vector (dict_new (16000), 0, '', '', '', 0, 0, 0, 0, 0)
  ; ses := string_output ()
  ; FOR (SELECT * FROM ( SPARQL DEFINE input:storage "" 
                         SELECT ?s ?p ?o { GRAPH `iri(?:srcgraph)` { ?s ?p ?o } } 
                       ) AS sub OPTION (LOOP)) DO
      {
        http_ttl_triple (env, "s", "p", "o", ses);
        ses_len := length (ses);
        IF (ses_len > max_ses_len)
          {
            file_len := file_len + ses_len;
            IF (file_len > file_length_limit)
              {
                http (' .\n', ses);
                string_to_file (file_name, ses, -1);
		gz_compress_file (file_name, file_name||'.gz');
		file_delete (file_name);
                file_len := 0;
                file_idx := file_idx + 1;
                file_name := sprintf ('%s%06d.ttl', out_file, file_idx);
                string_to_file ( file_name, 
                                 sprintf ( '# Dump of graph <%s>, as of %s (part %d)\n@base <> .\n', 
                                           srcgraph, 
                                           CAST (NOW() AS VARCHAR), 
                                           file_idx), 
                                 -2
                               );
                 env := VECTOR (dict_new (16000), 0, '', '', '', 0, 0, 0, 0, 0);
              }
            ELSE
              string_to_file (file_name, ses, -1);
            ses := string_output ();
          }
      }
    IF (LENGTH (ses))
      {
        http (' .\n', ses);
        string_to_file (file_name, ses, -1);
	gz_compress_file (file_name, file_name||'.gz');
	file_delete (file_name);
      }
  }
;

2. Then dump a Graph from ISQL

Execute the code to export a graph:
dump_one_graph ('the IRI of the GRPAH in Virtuoso', './theNewNameOfTheDumpedGraph_', 1000000000);

--in the function there are 3 parameters:

(1)In the second parameter, "./" indicates the dumped directory is right within the local Virtuoso working directory. And don't change the final "_" which is crucial for dividing the data when the graph is exceptionally large.
(2)The third parameter indicates the chunk size for the dump process.