Jena ARQ small test knife

sequence

This article mainly shows how to use apache jena to perform SPARQL query on RDF documents

related information

RDF

Resource Description Framework, a markup language for describing Web resources. Metadata is described as a data model using XML syntax and RDF Schema (RDFS).

SPARQL

SPARQL (SPARQL Protocol and RDF Query Language) is a query language and data acquisition protocol developed for RDF. It is defined for the RDF data model developed by W3C, but can be used for any information resources that can be represented by RDF. .

Apache Jena

Jena is a Java toolkit for developing RDF and OWL semantic (semantic) web applications. It provides an RDF API; ARP, an RDF parser; SPARQL, the W3C RDF Query Language; an OWL API; and a rule-based interface between RDFS and OWL. ARQ is the SPARQL query engine in Jena.

Example

maven

        <dependency>
            <groupId>org.apache.jena</groupId>
            <artifactId>jena-core</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.jena</groupId>
            <artifactId>jena-arq</artifactId>
            <version>3.7.0</version>
        </dependency>

RDF documents

<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.example.com/cd#">

<rdf:Description
 rdf:about="http://www.example.com/cd/EmpireBurlesque">
  <cd:artist>Bob Dylan</cd:artist>
  <cd:country>USA</cd:country>
  <cd:company>Columbia</cd:company>
  <cd:price>10.90</cd:price>
  <cd:year>1985</cd:year>
</rdf:Description>

<rdf:Description
 rdf:about="http://www.example.com/cd/Hideyourheart">
  <cd:artist>Bonnie Tyler</cd:artist>
  <cd:country>UK</cd:country>
  <cd:company>CBS Records</cd:company>
  <cd:price>9.90</cd:price>
  <cd:year>1988</cd:year>
</rdf:Description>
</rdf:RDF>

SPARQL query

    @Test
    public void testJenaQuery(){
        Model model = ModelFactory.createDefaultModel();
        model.read(this.getClass().getClassLoader().getResourceAsStream("demo.rdf"), null);
        String queryString = "PREFIX cd: <http://www.example.com/cd#> \n " +
                "SELECT ?name where { ?item cd:artist ?name }";
        Query query = QueryFactory.create(queryString);
        QueryExecution qe = QueryExecutionFactory.create(query, model);
        ResultSet results = qe.execSelect();
        ResultSetFormatter.out(System.out, results, query);
    }

output

------------------
| name           |
==================
| "Bonnie Tyler" |
| "Bob Dylan"    |
------------------

summary

SPARQL is a query language for RDF, similar to SQL, but not the same. It has a little learning cost and needs to be studied in depth.

doc

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324412867&siteId=291194637