其中, ModelFactory 类是一个Model 工厂,用于创建model 对象。我们可以使用 Model 的createResource 方法在model 中创建一个资源,并可以使用资源的 addProperty 方法添加属性。也可以直接使用createStatement方法在model中创建一个声明,并用model的add方法添加该声明。

Iterator有三种:NodeIterator,StmtIterator,ResIterator

练习:

RDF数据集:

1.

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.VCARD;  public class StatementDemo {  public static void main(String[] args){  //声明主语的URI并建立模型String personURI = "http://somewhere/JohnSmith"; Model model = ModelFactory.createDefaultModel();  //创立主谓宾关系,谓语是jena.vocabulary.VCARD自带的Resource johnSmith = model.createResource(personURI);  johnSmith.addProperty(VCARD.FN,"John Smith");  johnSmith.addProperty(VCARD.N,model.createResource().addProperty(VCARD.Given,"John").addProperty(VCARD.Family,"Smith"));  //建立声明迭代器StmtIterator iter = model.listStatements();  while(iter.hasNext()){//取出每一个声明,每一个声明的主语、谓语、宾语Statement stmt = iter.nextStatement();  Resource subject = stmt.getSubject();  Property predicate = stmt.getPredicate();  RDFNode object = stmt.getObject();  //用toString方法输出主谓宾System.out.print(subject.toString()+" -->");  System.out.print(" "+predicate.toString()+" -->");//判断宾语是资源还是文字(literal),如果是资源(的实例),则直接输出,如果是文字,加引号输出if(object instanceof Resource){  System.out.print(" "+object.toString());  }else{  System.out.print(" "+"\"" + object.toString() + "\"");  }  System.out.println(" .");  }  }
}
Result:
http://somewhere/JohnSmith --> http://www.w3.org/2001/vcard-rdf/3.0#N --> 203f5bb:15884c765d0:-7fff .
http://somewhere/JohnSmith--> http://www.w3.org/2001/vcard-rdf/3.0#FN--> "John Smith" .
203f5bb:15884c765d0:-7fff --> http://www.w3.org/2001/vcard-rdf/3.0#Family --> "Smith" .
203f5bb:15884c765d0:-7fff --> http://www.w3.org/2001/vcard-rdf/3.0#Given --> "John" .

Model 类的listStatements 将返回一个 Statement 的Iterator。Statement 有的主语、谓语、客体分别用 getSubject、getPredicate、getObject 来返回。其类型分别是 Resource、Property和RDFNode。其中客体 object 类型可以是Resource 或者文本(literal)

2.

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.VCARD;  public class RDFWriting {  public static void main(String[] args){  String personURI = "http://somewhere/JohnSmith"; Model model = ModelFactory.createDefaultModel();  Resource johnSmith = model.createResource(personURI);  johnSmith.addProperty(VCARD.FN,"John Smith");  johnSmith.addProperty(VCARD.N,model.createResource().addProperty(VCARD.Given,"John").addProperty(VCARD.Family,"Smith"));  //以不同格式输出RDFSystem.out.println("RDF/XML:");model.write(System.out,null);System.out.println("\n"+"RDF/XML-ABBREV:");model.write(System.out, "RDF/XML-ABBREV");  System.out.println("\n"+"N-TRIPLE:");model.write(System.out,"N-TRIPLE");System.out.println("\n"+"TURTLE:");model.write(System.out,"TURTLE");}
}
Result:
RDF/XML:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:nodeID="A0"><vcard:Family>Smith</vcard:Family><vcard:Given>John</vcard:Given></rdf:Description><rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:N rdf:nodeID="A0"/><vcard:FN>John Smith</vcard:FN></rdf:Description>
</rdf:RDF>RDF/XML-ABBREV:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#"><rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:N rdf:parseType="Resource"><vcard:Family>Smith</vcard:Family><vcard:Given>John</vcard:Given></vcard:N><vcard:FN>John Smith</vcard:FN></rdf:Description>
</rdf:RDF>N-TRIPLE:
<http://somewhere/JohnSmith> <http://www.w3.org/2001/vcard-rdf/3.0#N> _:BX2D335bbeeX3A15884deb340X3AX2D7fff .
<http://somewhere/JohnSmith> <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith" .
_:BX2D335bbeeX3A15884deb340X3AX2D7fff <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith" .
_:BX2D335bbeeX3A15884deb340X3AX2D7fff <http://www.w3.org/2001/vcard-rdf/3.0#Given> "John" .TURTLE:
<http://somewhere/JohnSmith><http://www.w3.org/2001/vcard-rdf/3.0#FN>"John Smith" ;<http://www.w3.org/2001/vcard-rdf/3.0#N>[ <http://www.w3.org/2001/vcard-rdf/3.0#Family>"Smith" ;<http://www.w3.org/2001/vcard-rdf/3.0#Given>"John"] .

用model.write(OutputStream out,String lang)方法以不同格式输出了RDF,供选的格式有RDF/XML, RDF/XML-ABBREV(RDF/XML的缩略形式), N-TRIPLE(三元组形式), TURTLE, N3(与TURTLE一样),第二个参数不写或写null(最新3.9版本jena中写null会有空指针异常,不识别这个关键字),代表用默认格式RDF/XML

3.

import java.io.InputStream;import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;public class RDFReading {public static void main(String[] args) {String inputFileName = "resource.rdf";Model model = ModelFactory.createDefaultModel();InputStream in = FileManager.get().open(inputFileName);if(in == null)throw new IllegalArgumentException("File: "+inputFileName+" not found");model.read(in,null);model.write(System.out);}
}
Result:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:about="http://somewhere/RebeccaSmith/"><vcard:N rdf:nodeID="A0"/><vcard:FN>Becky Smith</vcard:FN></rdf:Description><rdf:Description rdf:nodeID="A0"><vcard:Given>Rebecca</vcard:Given><vcard:Family>Smith</vcard:Family></rdf:Description><rdf:Description rdf:about="http://somewhere/MattJones/"><vcard:N rdf:nodeID="A1"/><vcard:FN>Matt Jones</vcard:FN></rdf:Description><rdf:Description rdf:about="http://somewhere/SarahJones/"><vcard:N rdf:nodeID="A2"/><vcard:FN>Sarah Jones</vcard:FN></rdf:Description><rdf:Description rdf:nodeID="A3"><vcard:Given>John</vcard:Given><vcard:Family>Smith</vcard:Family></rdf:Description><rdf:Description rdf:nodeID="A2"><vcard:Given>Sarah</vcard:Given><vcard:Family>Jones</vcard:Family></rdf:Description><rdf:Description rdf:about="http://somewhere/JohnSmith/"><vcard:N rdf:nodeID="A3"/><vcard:FN>John Smith</vcard:FN></rdf:Description><rdf:Description rdf:nodeID="A1"><vcard:Given>Matthew</vcard:Given><vcard:Family>Jones</vcard:Family></rdf:Description>
</rdf:RDF>

用model.read(InputStream in,String base)方法读入一个rdf文件,并用默认的RDF/XML格式输出。第二个参数是让相对地址(URI)转换为绝对地址,当项目里都是绝对地址时,设置为null。这里找寻文件的类用的是jena里的FileManager

4.

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;public class NSPrefix {public static void main(String[] args) {Model model = ModelFactory.createDefaultModel();String nsA = "http://somewhere/else#";String nsB = "http://nowhere/else#";//create Resources and PropertiesResource root = model.createResource(nsA + "root");Property P = model.createProperty( nsA + "P" );  Property Q = model.createProperty( nsB + "Q" );  Resource x = model.createResource( nsA + "x" );  Resource y = model.createResource( nsA + "y" );  Resource z = model.createResource( nsA + "z" ); model.add(root, P, x).add(root, P, y).add(y, Q, z);System.out.println( "# -- no special prefixes defined" );  model.write( System.out );  System.out.println( "\n# -- nsA and nsB defined" );  model.setNsPrefix("nsA",nsA );model.setNsPrefix("nsB",nsB );model.write( System.out );}
}
Result:
# -- no special prefixes defined
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:j.0="http://nowhere/else#"xmlns:j.1="http://somewhere/else#" > <rdf:Description rdf:about="http://somewhere/else#y"><j.0:Q rdf:resource="http://somewhere/else#z"/></rdf:Description><rdf:Description rdf:about="http://somewhere/else#root"><j.1:P rdf:resource="http://somewhere/else#y"/><j.1:P rdf:resource="http://somewhere/else#x"/></rdf:Description>
</rdf:RDF># -- nsA and nsB defined
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:nsB="http://nowhere/else#"xmlns:nsA="http://somewhere/else#" > <rdf:Description rdf:about="http://somewhere/else#y"><nsB:Q rdf:resource="http://somewhere/else#z"/></rdf:Description><rdf:Description rdf:about="http://somewhere/else#root"><nsA:P rdf:resource="http://somewhere/else#y"/><nsA:P rdf:resource="http://somewhere/else#x"/></rdf:Description>
</rdf:RDF>

用model.setNsPrefix设置名字空间的前缀,如果没有为RDF指定namespace前缀,jena会自动生成j.0和j.1的名字空间

5.

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.VCARD;public class ModelAccess {public static void main(String[] args) {String personURI = "http://somewhere/JohnSmith";  String givenName = "John";  String familyName = "Smith";  String fullName = givenName + " " + familyName;  Model model = ModelFactory.createDefaultModel();  Resource johnSmith = model.createResource(personURI);  johnSmith.addProperty(VCARD.FN, fullName);  johnSmith.addProperty(VCARD.N,   model.createResource()  .addProperty(VCARD.Given, givenName)  .addProperty(VCARD.Family, familyName)); /*------假设以上一切已是一个RDF文件中的声明,且我们未知,现在要对一个Model操作(一个Model是一个Description)--------------------------------------*/  //从Model中获得资源,假设我们已经知道这个Description主语的URIResource vcard = model.getResource(personURI);//如果知道该属性(VCARD.N)的值是属性,可以直接使用属性的getResource()方法//Resource name = vcard.getProperty(VCART.N).getResource();Resource name = (Resource) vcard.getProperty(VCARD.N).getObject();//如果知道该属性的值是literal,可以直接使用属性的getString()方法fullName = vcard.getProperty(VCARD.FN).getString();//可以为这个主语增加属性vcard.addProperty(VCARD.NICKNAME,"Smithy").addProperty(VCARD.NICKNAME, "Adman");//用迭代器查看该属性的值System.out.println("The nicknames of \""+fullName+"\" are:");StmtIterator itor = vcard.listProperties(VCARD.NICKNAME);while(itor.hasNext()){System.out.println("   "+itor.nextStatement().getObject().toString());}System.out.println("----------------------------------------");//查看加入新属性后的RDF/XMLmodel.write(System.out);}
}
Result:
The nicknames of "John Smith" are:AdmanSmithy
----------------------------------------
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:nodeID="A0"><vcard:Family>Smith</vcard:Family><vcard:Given>John</vcard:Given></rdf:Description><rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:NICKNAME>Adman</vcard:NICKNAME><vcard:NICKNAME>Smithy</vcard:NICKNAME><vcard:N rdf:nodeID="A0"/><vcard:FN>John Smith</vcard:FN></rdf:Description>
</rdf:RDF>

Model 的 getResource 方法:该方法根据参数返回一个资源对象。
Resource 的 getProperty 方法:根据参数返回一个属性对象。
Property 的 getObject 方法:返回属性值。使用时根据实际类型是 Resource 还是 literal 进行强制转换。
Property 的 getResource 方法:返回属性值的资源。如果属性值不是Resource,则报错。
Property 的 getString 方法:返回属性值的文本内容。如果属性值不是文本,则报错。
Resource 的 listProperties 方法:列出所找到符合条件的属性。

6.

使用model.listSubjectsWithProperyty()方法查询

import java.io.InputStream;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.VCARD;public class RDFQuery {public static String fileName = "resource.rdf";public static void main(String[] args) {Model model = ModelFactory.createDefaultModel();InputStream in = FileManager.get().open(fileName);if(in == null)throw new IllegalArgumentException("file: "+fileName+" not found");model.read(in,null);ResIterator itor = model.listSubjectsWithProperty(VCARD.FN);if(itor.hasNext()){System.out.println("The database contains vcard for: ");while(itor.hasNext())System.out.println(" "+itor.nextResource().getProperty(VCARD.FN).getString());}elseSystem.out.println("No vcards were found in the database");}
}
Result:
The database contains vcard for: Becky SmithMatt JonesSarah JonesJohn Smith

也可以使用Seletor查询

StmtIterator itor = model.listStatements(new SimpleSelector(null,VCARD.FN,(RDFNode)null));
if(itor.hasNext())
{System.out.println("The database contains vcard for: ");while(itor.hasNext())System.out.println(" "+itor.nextStatement().getString());}elseSystem.out.println("No vcards were found in the database");

结果同上

7.

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.VCARD;public class ControlRDF {public static void main(String[] args) {String personURI = "http://somewhere/JohnSmith";  String givenName = "John";  String familyName = "Smith";  String fullName = givenName + " " + familyName;  Model model = ModelFactory.createDefaultModel();  Resource johnSmith = model.createResource(personURI);  johnSmith.addProperty(VCARD.FN, fullName);  johnSmith.addProperty(VCARD.N,   model.createResource()  .addProperty(VCARD.Given, givenName)  .addProperty(VCARD.Family, familyName));  System.out.println("原始内容:");model.write(System.out);//删除Statementmodel.removeAll(null, VCARD.N, (RDFNode)null);//也可以用model.remove(StmtIterator itor)方法// model.remove(model.listStatements(null, VCARD.N,(RDFNode)null )); 下面两句也可以相对替换model.removeAll(null,VCARD.Given,(RDFNode)null);model.removeAll(null,VCARD.Family,(RDFNode)null);System.out.println("\n删除后的内容:");  model.write(System.out);  //增加Statement//用model.add(Resource arg1, Property arg2, RDFNode arg3)方法model.add(johnSmith,VCARD.N,model.createResource().addProperty(VCARD.Given, givenName).addProperty(VCARD.Family, familyName));System.out.println("\n重新增加后的内容:");  model.write(System.out);  }
}
Result:
原始内容:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:N rdf:nodeID="A0"/><vcard:FN>John Smith</vcard:FN></rdf:Description><rdf:Description rdf:nodeID="A0"><vcard:Family>Smith</vcard:Family><vcard:Given>John</vcard:Given></rdf:Description>
</rdf:RDF>删除后的内容:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:FN>John Smith</vcard:FN></rdf:Description>
</rdf:RDF>重新增加后的内容:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:nodeID="A0"><vcard:Family>Smith</vcard:Family><vcard:Given>John</vcard:Given></rdf:Description><rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:N rdf:nodeID="A0"/><vcard:FN>John Smith</vcard:FN></rdf:Description>
</rdf:RDF>

除了直接使用 Model 的方法外,对Model 中的Resource(资源)或Property(属性,实际上也继承自Resource)进行增删操作也可以达到更改 Model 的目的。

Model 的合并主要分为 交、并、补三种操作。
如下图所示:

这两个图分别代表一个Model。它们的名字相同,且具有相同的属性 vcard:FN ,值为John Smith。因此,我们对这两个Model 进行“并”(union)操作。所得到的Model 的图形表示如下:

其中重复的 vcard:FN 值只出现一个。
这三种操作的方法分别为:

  • Model.intersection(Model model): 交操作。创建一个新Model ,内容是前两个Model的共有部分。(交集)
  • Model.union(Model model): 并操作。创建一个新Model,内容是前两个Model内容的叠加,重复的去掉。(并集)
  • Model1.difference(Model model2): 补操作。创建一个新Model,内容是Model1去掉与Model2共有的,和Model2独有的部分。(差集)

8.

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.VCARD;public class ModelOperation {public static void main(String[] args) {String personURI = "http://somewhere/JohnSmith";  String givenName = "John";  String familyName = "Smith";  String fullName = givenName + " " + familyName;  Model model1 = ModelFactory.createDefaultModel();Resource johnSmith1 = model1.createResource(personURI);johnSmith1.addProperty(VCARD.FN,fullName);johnSmith1.addProperty(VCARD.N,model1.createResource().addProperty(VCARD.Given, givenName).addProperty(VCARD.Family, familyName));System.out.println("Model1的内容是:");model1.write(System.out);Model model2 = ModelFactory.createDefaultModel();Resource johnSmith2 = model2.createResource(personURI);johnSmith2.addProperty(VCARD.FN, fullName);johnSmith2.addProperty(VCARD.EMAIL, model2.createResource().addProperty(RDF.type, "vcard.internet").addProperty(RDF.value, "John@qq.com"));System.out.println("\nModel2的内容是:");model2.write(System.out);//UnionModel model3 = model1.union(model2);System.out.println("\nUnion后的新Model");model3.write(System.out);//IntersectionModel model4 = model1.intersection(model2);System.out.println("\nIntersection后的新Model");model4.write(System.out);//DifferenceModel model5 = model1.difference(model2);System.out.println("\n从model1中Difference掉model2后的新Model");model5.write(System.out);//DifferenceModel model6 = model2.difference(model1);System.out.println("\n从model2中Difference掉model1后的新Model");model6.write(System.out);}
}
Result:
Model1的内容是:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:N rdf:nodeID="A0"/><vcard:FN>John Smith</vcard:FN></rdf:Description><rdf:Description rdf:nodeID="A0"><vcard:Family>Smith</vcard:Family><vcard:Given>John</vcard:Given></rdf:Description>
</rdf:RDF>Model2的内容是:
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:nodeID="A0"><rdf:value>John@qq.com</rdf:value><rdf:type>vcard.internet</rdf:type></rdf:Description><rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:EMAIL rdf:nodeID="A0"/><vcard:FN>John Smith</vcard:FN></rdf:Description>
</rdf:RDF>Union后的新Model
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:nodeID="A0"><rdf:type>vcard.internet</rdf:type><rdf:value>John@qq.com</rdf:value></rdf:Description><rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:EMAIL rdf:nodeID="A0"/><vcard:FN>John Smith</vcard:FN><vcard:N rdf:nodeID="A1"/></rdf:Description><rdf:Description rdf:nodeID="A1"><vcard:Given>John</vcard:Given><vcard:Family>Smith</vcard:Family></rdf:Description>
</rdf:RDF>Intersection后的新Model
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:FN>John Smith</vcard:FN></rdf:Description>
</rdf:RDF>从model1中Difference掉model2后的新Model
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:N rdf:nodeID="A0"/></rdf:Description><rdf:Description rdf:nodeID="A0"><vcard:Given>John</vcard:Given><vcard:Family>Smith</vcard:Family></rdf:Description>
</rdf:RDF>从model2中Difference掉model1后的新Model
<rdf:RDFxmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > <rdf:Description rdf:nodeID="A0"><rdf:type>vcard.internet</rdf:type><rdf:value>John@qq.com</rdf:value></rdf:Description><rdf:Description rdf:about="http://somewhere/JohnSmith"><vcard:EMAIL rdf:nodeID="A0"/></rdf:Description>
</rdf:RDF>

使用Jena进行SPARQL查询

Requirement: 查询谓语是vcard.FN,宾语是John Smith的所有主语

import java.io.InputStream;import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;public class RDFQuery {public static void main(String[] args) {String inputFileName = "resource.rdf";Model model = ModelFactory.createDefaultModel();InputStream in = FileManager.get().open(inputFileName);if(in == null)throw new IllegalArgumentException("File: "+inputFileName+" not found");model.read(in,null);//创建一个查询语句String queryString = "SELECT ?subject"+" WHERE "+"{ ?subject <http://www.w3.org/2001/vcard-rdf/3.0#FN> \"John Smith\"}";//创建一个查询Query query = QueryFactory.create(queryString);//执行查询,获得结果QueryExecution qe = QueryExecutionFactory.create(query, model);ResultSet results = qe.execSelect();//向控制台输出结果//用这句一样ResultSetFormatter.out(results);ResultSetFormatter.out(System.out, results, query);//释放资源qe.close();}
}

首先程序要先读入该本体(这里是RDF文件)
使用QueryFactory.create创建一个Query,然后用使用QueryExecutionFactory.create将Query和Model都传入执行工厂进行执行,用ResultSet收集结果,最后用ResultSetFormatter.out向控制台输出结果

Apache Jena入门相关推荐

  1. Apache Solr入门教程

    转自:http://blog.csdn.net/u011936655/article/details/51960005 Apache Solr入门教程(初学者之旅) 写在前面:本文涉及solr入门的各 ...

  2. 知识图谱实践篇(四):Apache jena SPARQL endpoint及推理

    在上一篇我们学习了如何利用D2RQ来开启endpoint服务,但它有两个缺点: 1. 不支持直接将RDF数据通过endpoint发布到网络上. 2. 不支持推理. 这次我们介绍的Apache Jena ...

  3. Apache Camel入门

    在先前的博文中,我们了解了企业集成模式(EIP). 现在,在这篇文章中,我们将研究实现这些模式的Apache Camel框架. 关于骆驼: Apache Camel是一个开放源代码项目,已有将近5年的 ...

  4. Apache Shiro入门

    Apache Shiro入门 @(Shiro)[shiro,安全框架] Apache Shiro入门 Apache Shiro基本概述 Apache Shiro基本概念 使用Shiro能做什么 Shi ...

  5. Apache CXF入门

    Apache CXF入门 @(WebService)[WebService, CXF,wsdl, soap,uddi] Apache CXF入门 Apache CXF基本概述 Apache CXF框架 ...

  6. Apache NIFI入门(读完即入门)

    Apache NIFI入门(读完即入门) 编辑人(全网同名):酷酷的诚 邮箱:zhangchengk@foxmail.com 我将在本文中介绍: 什么是ApacheNIFI,应在什么情况下使用它,理解 ...

  7. Apache Camel入门教程

    Apache Camel入门教程 本文我们学习Apache Camel,介绍基本概念并重点探讨消息路由.从基本概念和术语开始,然后通过介绍两种方式定义路由----java dsl 和 Spring d ...

  8. 4.1 数据仓库基础与Apache Hive入门

    数据仓库基础与Apache Hive入门 一.数据仓库基本概念 1.数据仓库概念 2. 案例:数据仓库为何而来 (1)业务数据的存储问题 (2)分析型决策的制定 3.数据仓库主要特征 面向主题性(Su ...

  9. Apache Kafka 入门 - Kafka命令详细介绍

    Apache Kafka 入门 Apache Kafka 入门大概分为5篇博客,内容都比较基础,计划包含以下内容: Kafka的基本配置和运行 Kafka命令详细介绍 Kafka-manager的基本 ...

  10. Apache Hive入门1

    Apache Hive入门1 Hive是Hadoop项目中的一个子项目,由FaceBook向Apache基金会贡献,其中TaoBao也是其中一位使用者+贡献者,Hive被视为 一个仓库工具,可以将结构 ...

最新文章

  1. 更改UISwitch大小
  2. matlab单元刚度矩阵整合成整刚,求结构总刚矩阵Matlab源代码
  3. java 更新ui_你怎么解决Android开发中更新UI报错的异常吗-百度经验
  4. OpenGL 着色器的N体仿真
  5. flask对mysql数据库增删改查_flask后台框架1.4(mysql配置+数据库增删改查)-Go语言中文社区...
  6. Linux配置中文输入法
  7. xmm1是什么器件_第三章基于Multisim10在模拟电路实验中的应用
  8. 商务周刊:别了,摩托罗拉
  9. 微信小程序真机调试常见问题汇总
  10. C++之内存管理:申请与释放
  11. JAVA之线程池详解
  12. 源码学习之LAMMPS的一个时间步是如何工作的
  13. 判断高ROE指数的投资价值
  14. 牛客社区论坛项目(二)
  15. html中如何在页面底部增加,HTML中footer固定在页面底部的若干种方法
  16. S7-1500与G120变频器通过标准报文1进行Profinet通信的具体步骤
  17. TensorFlow 中的 BatchToSpaceOp
  18. 微信小程序6位支付密码输入框
  19. 批量处理图形大小如何更改图片大小尺寸修改图片视频教程ps学习ps教程ps基础新教程
  20. k5b计算机联锁文库,Ds6-K5B计算机联锁系统

热门文章

  1. 2022-2028年中国新疆旅游行业发展动态及投资前景分析报告
  2. python turtle setheading_Python turtle.right与turtle.setheading的区别
  3. 《指弹:HARD RAIN》
  4. Python绘图库Matplotlib.pyplot之网格线设置(plt.grid())
  5. 联盛德 HLK-W806 (十三): 运行FatFs读写FAT和exFat格式的SD卡/TF卡
  6. teredo 未能解析服务器名,Win10系统Xboxlive显示Teredo无法进行限定怎么解决
  7. 《Matlab算法》 part1 误差分析
  8. docker部署minio分享图片链接ip问题
  9. r去掉向量中的空字符串 在R里如何去掉字符串矩阵中的空字符串 r r 识别字符串中的双引号 识别字符串中的双引号 str_detect
  10. OSChina 周六乱弹 —— 假如你被熊困到树上