如何使用java从RDF文件中获取特定的标签相关信息
我使用Jena读取RDF文件,但我想获取信息只是特定的语句。 下面是我读取sample.rdf
示例代码。
sample.rdf
:
4 6588 network, specific mechanisms for implementing VoIP applications. Topics include imple-menting a [applications. Topics include imple- menting a ]VoIP[ network, specific mechanisms for implementing] Cisco IOS 2 Cisco IOS
将sample.rdf
加载到Jena模型中的Java代码:
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){} } }
期望的输出:
2 Cisco IOS
谢谢
工作数据
这是我们可以实际使用的一些数据。 它基于您的数据,定义了rdf
前缀,以及一些用于制作ctag:SocialTag
附加前缀ctag:SocialTag
和csys:InstanceInfo
工作。 这些是可选的,但是需要c
的前缀定义,因为它已在您的数据中使用。 为方便起见,我只使用了http://example.org/c#
,但您可能已经定义了其他内容。
Cisco IOS 2 Cisco IOS 4 6588 network, specific mechanisms for implementing VoIP applications. Topics include imple-menting a [applications. Topics include imple- menting a ]VoIP[ network, specific mechanisms for implementing]
一旦你在某处获得了这些数据,有两种简单的方法可以使用Jena来获取它。 第一种是使用Jena的Model API,它提供了检索语句的方法。 第二种是使用SPARQL查询。 您可以使用Jena的命令行工具运行SPARQL查询,也可以从Java程序运行。
使用Model API
这是创建results
模型以存储所需输出的Java代码,从input
检索SocialTag和定义其名称和重要性的语句,并将语句复制到results
。
public static Model queryWithAPI() { // Create a model for the output, and add the prefix mappings // from the input model. This step isn't necessary, but it // makes the output easier to read. final Model results = ModelFactory.createDefaultModel(); results.setNsPrefixes( input ); // Iterate through the SocialTags in the data, and for each SocialTag s, retrieve // the statements [s, name, ?name] and [s, importance, ?importance] from the input // model, and add them to the results. for ( final ResIterator it = input.listResourcesWithProperty( RDF.type, SocialTag ); it.hasNext(); ) { final Resource socialTag = it.next(); results.add( socialTag.getProperty( importance )); results.add( socialTag.getProperty( name )); } return results; }
使用SPARQL
以下SPARQL construct
查询还检索SocialTags,然后构造所需的图形。
prefix rdf: prefix ctag: prefix c: construct { ?tag c:name ?name ; c:importance ?importance . } where { ?tag a ctag:SocialTag ; c:name ?name ; c:importance ?importance . }
这是在input
模型上执行该查询的Java代码。
public static Model queryWithSPARQL() { // A SPARQL query that retrieves each SocialTag and its name // and importance, and constructs a model containing just the // name and importance statements. final String query = "" + "prefix rdf: <"+RDF.getURI()+">\n" + "prefix ctag: <"+CTAG+">\n" + "prefix c: <"+C+">\n" + "construct {\n" + " ?tag c:name ?name ;\n" + " c:importance ?importance .\n" + "}\n" + "where {\n" + " ?tag a ctag:SocialTag ;\n" + " c:name ?name ;\n" + " c:importance ?importance .\n" + "}"; // Create and execute the query on the input model. return QueryExecutionFactory.create( query, input ).execConstruct(); }
现在都在一起了
上面的列表只是一个定义input
和读取数据的工作示例的片段。 这是整个列表:
import com.hp.hpl.jena.query.QueryExecutionFactory; 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.ResIterator; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.vocabulary.RDF; public class CalaisExample { static final String C = "http://example.org/c#"; static final String CTAG = "http://s.opencalais.com/1/type/tag/"; static final Resource SocialTag = ResourceFactory.createResource( CTAG+"SocialTag" ); static final Property importance = ResourceFactory.createProperty( C+"importance" ); static final Property name = ResourceFactory.createProperty( C+"name" ); // Create a model for the input and read in the data. static final Model input = ModelFactory.createDefaultModel() .read( "file:///home/taylorj/tmp/jena-calais/calais.rdf" ); public static void main(String[] args) { System.out.println( "== Using API ==" ); queryWithAPI().write( System.out ); System.out.println(); System.out.println( "== Using SPARQL ==" ); queryWithSPARQL().write( System.out ); } public static Model queryWithAPI() { // Create a model for the output, and add the prefix mappings // from the input model. This step isn't necessary, but it // makes the output easier to read. final Model results = ModelFactory.createDefaultModel(); results.setNsPrefixes( input ); // Iterate through the SocialTags in the data, and for each SocialTag s, retrieve // the statements [s, name, ?name] and [s, importance, ?importance] from the input // model, and add them to the results. for ( final ResIterator it = input.listResourcesWithProperty( RDF.type, SocialTag ); it.hasNext(); ) { final Resource socialTag = it.next(); results.add( socialTag.getProperty( importance )); results.add( socialTag.getProperty( name )); } return results; } public static Model queryWithSPARQL() { // A SPARQL query that retrieves each SocialTag and its name // and importance, and constructs a model containing just the // name and importance statements. final String query = "" + "prefix rdf: <"+RDF.getURI()+">\n" + "prefix ctag: <"+CTAG+">\n" + "prefix c: <"+C+">" + "construct {\n" + " ?tag c:name ?name ;\n" + " c:importance ?importance .\n" + "}\n" + "where {\n" + " ?tag a ctag:SocialTag ;\n" + " c:name ?name ;\n" + " c:importance ?importance .\n" + "}"; // Create and execute the query on the input model. return QueryExecutionFactory.create( query, input ).execConstruct(); } }
这是输出:
== Using API == Cisco IOS 2 == Using SPARQL == 2 Cisco IOS
- 我的EL表达式没有在MyFaces 2.3和Spring Boot 2.0.3应用程序下进行评估
- 为什么这个java 8流操作求值为Object而不是List 或只是List?
- Java中的最终字符
- 在AWT-EventQueue-0中导致此NullPointerException的原因是什么?
- 集合的hashCode方法的最佳实现
- 在Spring 2.5中注册关闭钩子
- 只需一步即可在HashMap中声明和放置String数组
- 在Spring Boot IntegrationTest上禁用@Schedule
- 强制从java.time中的`DateTimeFormatter.ofLocalized …`生成的本地化字符串中的4位数年份