AGLoadNtriples.java

package com.franz.ag.examples;

import com.franz.ag.*;

import org.openrdf.model.URI;

public class AGLoadNtriples {

    /**
     * Demonstrates how to load a triple store from N-Triples files.
     * 
     * @param args unused
     * @throws AllegroGraphException
     */
    public static void main(String[] args) throws AllegroGraphException {
        // Connect to server, which must already be running.
        AllegroGraphConnection ags = new AllegroGraphConnection();
        try {
            ags.enable();
        } catch (Exception e) {
            throw new AllegroGraphException("Server connection problem", e);
        }

        // Create fresh triple-store.
        AllegroGraph ts = ags.renew("testnt", AGPaths.TRIPLE_STORES);
        
        // Load a file in N-Triples format
        String ntripleFile = AGPaths.dataSources("test.nt");        
        System.out.println("Loading N-Triples " + ntripleFile);
        ts.loadNTriples(ntripleFile);
        System.out.println("Loaded " + ts.numberOfTriples() + " triples.");
        
        // Get all triples from the default graph and show them.
        Cursor cc = ts.getStatements(null, null, null);
        AGUtils.showTriples(cc);
        
        // Load N-Triples data into a named graph
        URI g = ts.createURI("http://example.org/kennedy");
        long n = ts.loadNTriples(AGPaths.dataSources("kennedy.ntriples"), g);
        System.out.println("Loaded " + n + " triples.");
        
        // Get some triples from a given graph g and show them.
        cc = ts.getStatements(null, null, ts.createLiteral("Arnold"), g);
        AGUtils.showTriples(cc);
        
        // If you have a number of files to load, it is more efficient to
        // provide an array of filenames rather than loading them separately.
        String[] files = { 
                AGPaths.dataSources("sna.nt"),
                AGPaths.dataSources("temporal.nt"),
                AGPaths.dataSources("wilburwine.ntriples")
        };
        System.out.println("Loading multiple files ...");
        n = ts.loadNTriples(files, "source");
        System.out.println("Loaded " + n + " triples.");
        
        // Close the store and disconnect from the server.
        ts.closeTripleStore();
        ags.disable();
    }

    public static void loadNTriplesWithTiming(AllegroGraph ts, String ntriplesFile) throws AllegroGraphException {
        AGLoadNtriples.loadNTriplesWithTiming(ts, ntriplesFile, "");
    }

    public static void loadNTriplesWithTiming(AllegroGraph ts, String ntriplesFile, String graph) throws AllegroGraphException {
        System.out.println("Loading N-Triples from " + ntriplesFile);
        long start = System.currentTimeMillis();
        long n = ts.loadNTriples(ntriplesFile,graph,false,null,null);
        System.out.println("Loaded " + n + " triples in " + AGUtils.elapsedTime(start));
    }

}

Up | Next