The error comes from the fact that the data you have ARE n-triples!

Try this code :

<pre>

public static main (String[] argv){
       final String filepath = "path_to_the_file_containing_your_triples";
       final Model model = ModelFactory.createDefaultModel();
       RDFDataMgr.read(model, new FileInputStream(filepath), Lang.NTRIPLES);

       final StmtIterator listStatements = model.listStatements();
       while (listStatements.hasNext()) 
{System.out.println(listStatements.next());}
}
</pre>


I saved the example of triples you attached in the mail and got this print :

<pre>

[http://rdf.freebase.com/ns/american_football.football_player.footballdb_id, 
http://rdf.freebase.com/ns/type.object.name, "footballdb ID"@en]
[http://rdf.freebase.com/ns/american_football.football_player.footballdb_id, 
http://rdf.freebase.com/ns/type.object.type, 
http://rdf.freebase.com/ns/type.property]
</pre>


Hope this help.

Arthur Vaisse-Lesteven



________________________________
 De : Li Li <[email protected]>
À : [email protected] 
Envoyé le : Vendredi 8 août 2014 10h03
Objet : how to parse freebase triples?
 

I want to play with freebase data. on the dump webpage, it says it's
n-triples format.
but in this so question:
http://stackoverflow.com/questions/21274368/jena-parsing-issue-for-freebase-rdf-dump-jan-2014
it seems turtle format.
I want to parse a few lines of this file using jena but it throws exception

Exception in thread "main" com.hp.hpl.jena.shared.JenaException:
java.net.MalformedURLException: no protocol:
<http://rdf.freebase.com/ns/american_football.football_player.footballdb_id>
<http://rdf.freebase.com/ns/type.object.type>
<http://rdf.freebase.com/ns/type.property> .

<http://rdf.freebase.com/ns/american_football.football_player.footballdb_id>
<http://rdf.freebase.com/ns/type.object.name> "footballdb ID"@en .


my codes

FileInputStream is = new FileInputStream("freebase-rdf-2014-08-03-00-00.gz");

GZIPInputStream gzis=new GZIPInputStream(is);

BufferedReader br=new BufferedReader(new InputStreamReader(gzis,"UTF8"));

int lineNum=0;

String line;

StringBuilder sb=new StringBuilder();

while((line=br.readLine())!=null){

sb.append(line).append("\n");

lineNum++;

if(lineNum%100==0){

break;

}

}

Model model = ModelFactory.createDefaultModel();

String s=sb.toString();

model.read(s, "TURTLE");

// list the statements in the Model

StmtIterator iter = model.listStatements();


// print out the predicate, subject and object of each statement

while (iter.hasNext()) {

    Statement stmt      = iter.nextStatement();  // get next statement

    Resource  subject   = stmt.getSubject();     // get the subject

    Property  predicate = stmt.getPredicate();   // get the predicate

    RDFNode   object    = stmt.getObject();      // get the object


    System.out.print(subject.toString());

    System.out.print(" " + predicate.toString() + " ");

    if (object instanceof Resource) {

      System.out.print(object.toString());

    } else {

        // object is a literal

        System.out.print(" \"" + object.toString() + "\"");

    }


    System.out.println(" .");

}

br.close();

Reply via email to