<docbook><section><title>VirtRDFDatasetDump</title><title> Producing RDF dumps from the Virtuoso Triple-/Quad-Store</title> Producing RDF dumps from the Virtuoso Triple-/Quad-Store
<bridgehead class="http://www.w3.org/1999/xhtml:h2">What?</bridgehead>
 How to export RDF model data from Virtuoso&#39;s Quad Store.<bridgehead class="http://www.w3.org/1999/xhtml:h2">Why?</bridgehead>
 Every DBMS needs to offer a mechanism for bulk export and import of data.<para>Virtuoso supports dumping and reloading graph model data (e.g., RDF), as well as relational data (e.g., SQL) (discussed elsewhere).</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> How?</bridgehead>
<para>We have created stored procedures for the task.
 The dump procedures leverage SPARQL to facilitate selective data dump(s) from one or more Named Graphs, each denoted by an IRI.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3"> Dump One Graph</bridgehead>
<para>The procedure <emphasis>dump_one_graph</emphasis> can be used to dump any single Named Graph.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h4"> Parameters</bridgehead>
<itemizedlist mark="bullet" spacing="compact"><listitem>IN <emphasis>srcgraph</emphasis> VARCHAR -- source graph </listitem>
<listitem>IN <emphasis>out_file</emphasis> VARCHAR -- output file </listitem>
<listitem>IN <emphasis>file_length_limit</emphasis> INTEGER -- maximum length of dump files</listitem>
</itemizedlist><bridgehead class="http://www.w3.org/1999/xhtml:h4"> Procedure source</bridgehead>
<programlisting>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 = &#39;uncommitted&#39;;
   max_ses_len  := 10000000;
   file_len     := 0;
   file_idx     := 1;
   file_name    := sprintf (&#39;%s%06d.ttl&#39;, out_file, file_idx);
   string_to_file ( file_name || &#39;.graph&#39;, 
                     srcgraph, 
                     -2
                   );
    string_to_file ( file_name, 
                     sprintf ( &#39;# Dump of graph &lt;%s&gt;, as of %s\n@base &lt;&gt; .\n&#39;, 
                               srcgraph, 
                               CAST (NOW() AS VARCHAR)
                             ), 
                     -2
                   );
   env := vector (dict_new (16000), 0, &#39;&#39;, &#39;&#39;, &#39;&#39;, 0, 0, 0, 0, 0);
   ses := string_output ();
   FOR (SELECT * FROM ( SPARQL DEFINE input:storage &quot;&quot; 
                         SELECT ?s ?p ?o { GRAPH `iri(?:srcgraph)` { ?s ?p ?o } } 
                       ) AS sub OPTION (LOOP)) DO
      {
        http_ttl_triple (env, &quot;s&quot;, &quot;p&quot;, &quot;o&quot;, ses);
        ses_len := length (ses);
        IF (ses_len &gt; max_ses_len)
          {
            file_len := file_len + ses_len;
            IF (file_len &gt; file_length_limit)
              {
                http (&#39; .\n&#39;, ses);
                string_to_file (file_name, ses, -1);
                gz_compress_file (file_name, file_name||&#39;.gz&#39;);
                file_delete (file_name);
                file_len := 0;
                file_idx := file_idx + 1;
                file_name := sprintf (&#39;%s%06d.ttl&#39;, out_file, file_idx);
                string_to_file ( file_name, 
                                 sprintf ( &#39;# Dump of graph &lt;%s&gt;, as of %s (part %d)\n@base &lt;&gt; .\n&#39;, 
                                           srcgraph, 
                                           CAST (NOW() AS VARCHAR), 
                                           file_idx), 
                                 -2
                               );
                 env := VECTOR (dict_new (16000), 0, &#39;&#39;, &#39;&#39;, &#39;&#39;, 0, 0, 0, 0, 0);
              }
            ELSE
              string_to_file (file_name, ses, -1);
            ses := string_output ();
          }
      }
    IF (LENGTH (ses))
      {
        http (&#39; .\n&#39;, ses);
        string_to_file (file_name, ses, -1);
        gz_compress_file (file_name, file_name||&#39;.gz&#39;);
        file_delete (file_name);
      }
  }
;
</programlisting><para> To load the procedure into Virtuoso, it can simply be copied and pasted into the Virtuoso &quot;isql&quot; command line tool or Interactive SQL UI of the Conductor and execute.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h4"> Example</bridgehead>
<orderedlist spacing="compact"><listitem>Call the <span style="color: red">
      UNKNOWN tag:
      http://www.w3.org/1999/xhtml:nowikidump_one_graph</span> procedure with appropriate arguments: <programlisting>$ pwd 
/Applications/OpenLink Virtuoso/Virtuoso 6.1/database

$ grep DirsAllowed virtuoso.ini
DirsAllowed              = ., ../vad,

$ /opt/virtuoso/bin/isql 1111
Connected to OpenLink Virtuoso
Driver: 06.01.3127 OpenLink Virtuoso ODBC Driver
OpenLink Interactive SQL (Virtuoso), version 0.9849b.
Type HELP; for help and EXIT; to exit.
SQL&gt; dump_one_graph (&#39;http://daas.openlinksw.com/data#&#39;, &#39;./data_&#39;, 1000000000); 
Done. -- 1438 msec.
</programlisting></listitem>
<listitem>As a result, a dump of the graph &lt;<ulink url="http://daas.openlinksw.com/data#">http://daas.openlinksw.com/data#</ulink>&gt; will be found in the files data_XX (located in your Virtuoso db folder): <programlisting>$ ls
data_000001.ttl
data_000002.ttl
....
data_000001.ttl.graph
</programlisting> </listitem>
</orderedlist><bridgehead class="http://www.w3.org/1999/xhtml:h3"> Dump Multiple Graphs</bridgehead>
<para>See the following documentation on <ulink url="VirtRDFDumpNQuad">dumping Virtuoso RDF Quad store hosted data into NQuad datasets</ulink>.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Related</bridgehead>
<itemizedlist mark="bullet" spacing="compact"><listitem><ulink url="VirtTipsAndTricksGuide">Virtuoso Tips and Tricks Collection</ulink> </listitem>
<listitem><ulink url="VirtRDFDumpNQuad">RDF dumps from Virtuoso Quad store hosted data into NQuad dumps</ulink> </listitem>
<listitem><ulink url="http://docs.openlinksw.com/virtuoso/rdfperformancetuning.html#rdfperfdumpandreloadgraphs">Virtuoso Documentation</ulink> </listitem>
</itemizedlist></section></docbook>