Quantcast
Channel: Apache Timeline
Viewing all articles
Browse latest Browse all 5648

how to parse freebase triples?

$
0
0
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();

Viewing all articles
Browse latest Browse all 5648

Trending Articles