I am using Jena to read an RDF file, but I want to get the information just specific statements. Below is my sample code to read sample.rdf
.
sample.rdf
:
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/Instance/143">
<c:length>4</c:length>
<c:offset>6588</c:offset>
<c:suffix> network, specific mechanisms for implementing</c:suffix>
<c:exact>VoIP</c:exact>
<c:prefix>applications. Topics include imple-menting a
</c:prefix>
<c:detection>[applications. Topics include imple-
menting a ]VoIP[ network, specific mechanisms for implementing]</c:detection>
<c:subject rdf:resource="http://d.opencalais.com/genericHasher-1/1bc26b65-5ef5-306d-9203-fd0f8aa3ba18"/>
<c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
<rdf:type rdf:resource="http://s.opencalais.com/1/type/sys/InstanceInfo"/>
</rdf:Description>
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
<c:originalValue>Cisco IOS</c:originalValue>
<c:importance>2</c:importance>
<c:name>Cisco IOS</c:name>
<c:socialtag rdf:resource="http://d.opencalais.com/genericHasher-1/8ed51994-de69-3307-acf7-be18cc0d06e2"/>
<c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
<rdf:type rdf:resource="http://s.opencalais.com/1/type/tag/SocialTag"/>
</rdf:Description>
Java code that loads sample.rdf
into a Jena Model:
public class FirstRDFReader extends Object {
public static void main (String args[]) {
String inputFile="C://Sample.rdf";
Model model = ModelFactory.createDefaultModel();
try{
InputStream in =new FileInputStream(inputFile);
if (in == null) {
System.out.println("File not found");
}
model.read(in," ");
model.write(System.out);
}catch(Exception e){}
}
}
Desired output:
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
<c:importance>2</c:importance>
<c:name>Cisco IOS</c:name>
</rdf:Description>
thanks
Working Data
Here's some data that we can actually work with. It's based on your data, with the
rdf
prefix defined, and some additional prefixes for makingctag:SocialTag
andcsys:InstanceInfo
work. Those are optional, but a prefix definition forc
was required, since it was used in your data. I just usedhttp://example.org/c#
for convenience, but you've probably got something else defined.Once you've got that data somewhere, there are two easy ways to get it out using the Jena. The first is to use Jena's Model API which provides methods for retrieving statements. The second is to use a SPARQL query. You can run the SPARQL query using Jena's command line tools, but also from a Java program.
Using the Model API
Here's Java code that creates a
results
model to store the desired output, retrieves SocialTags and the statements defining their name and importance frominput
, and copies the statement intoresults
.Using SPARQL
The following SPARQL
construct
query also retrieve SocialTags and then constructs the desired graph.Here's Java code that executes that query on the
input
model.All together now
The listings above are just snippets from a working example that defines
input
and reads in the data. Here's the entire listing:Here's the output: