Skip to content

Instantly share code, notes, and snippets.

@jindrichmynarz
Created February 25, 2016 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jindrichmynarz/da9b8fe13329ff7d2985 to your computer and use it in GitHub Desktop.
Save jindrichmynarz/da9b8fe13329ff7d2985 to your computer and use it in GitHub Desktop.
OpenLink Virtuoso procedure for dumping a graph to N-Triples
CREATE PROCEDURE dump_one_graph_nt
( 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.nt', out_file, file_idx)
; string_to_file ( file_name || '.graph',
srcgraph,
-2
);
; env := vector (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_nt_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.nt', out_file, file_idx);
env := vector (0, 0, 0);
}
ELSE
string_to_file (file_name, ses, -1);
ses := string_output ();
}
}
IF (LENGTH (ses))
{
string_to_file (file_name, ses, -1);
gz_compress_file (file_name, file_name||'.gz');
file_delete (file_name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment