I usualy use this code to get literal datatype :
final Model model = ModelFactory.createDefaultModel();
RDFDataMgr.read(model, new FileInputStream(filepath), Lang.NTRIPLES);
final StmtIterator listStatements = model.listStatements();
while (listStatements.hasNext()) {
final Statement next = listStatements.next();
final RDFNode object =
next.getObject();
if (object.isLiteral()) {
final Literal literal = object.asLiteral();
final String datatype = literal.getDatatypeURI();
}
Jena automatically parse and link litteral to their datatype.
However, for your excerpt, the datatype obtained is equal to null.
In fact, as no datatype is defined in the dataset it is logical.
It may exist a way to automatically give a certain datatype (xsd:String for
example) to certain literal, but if it is the ca se I don't now how to to it.
Arthur.
________________________________
De : Li Li <[email protected]>
À : [email protected]; Arthur Vaïsse-Lesteven <[email protected]>
Envoyé le : Vendredi 8 août 2014 10h54
Objet : Re: how to parse freebase triples?
thanks, it works.
btw, for literals, how can I know it's data type and convert it to
corresponding java type? I
want to save it to my own database
On Fri, Aug 8, 2014 at 4:37 PM, Arthur Vaïsse-Lesteven
<[email protected]> wrote:
> 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();