python using rdflib create rdf, execute SPARQL queries on jena fuseki

  1. Build and launch jena fuseki Services
    Reference: https://www.cnblogs.com/bincoding/p/11223372.html
  2. Rdf file created using rdflib
import rdflib

def create_rdf():
    g = rdflib.Graph()
    # 实体
    pinganfu = rdflib.URIRef('http://www.example.org/pinganfu')
    yiwaixian = rdflib.URIRef('http://www.example.org/yiwaixian')
    # 关系
    price = rdflib.URIRef('http://www.example.org/price')
    product_from = rdflib.URIRef('http://www.example.org/from')
    # 属性
    price_100 = rdflib.URIRef('http://www.example.org/100')
    price_200 = rdflib.URIRef('http://www.example.org/200')
    from_paic = rdflib.URIRef('http://www.example.org/paic')
    from_pajiankang = rdflib.URIRef('http://www.example.org/pingan jiankangxian')
    g.add((pinganfu, price, price_100))
    g.add((yiwaixian, price, price_200))
    g.add((pinganfu, product_from, from_paic))
    g.add((yiwaixian, product_from, from_pajiankang))

    g.serialize("graph.rdf")

if __name__ == "__main__":
    create_rdf()
  1. jena fuseki import files generated rdf need utf-8 format

  2. Execute the query
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select *
where {
  ?product <http://www.example.org/price> ?price .
}

Query results

jena data format

Reference:
https://blog.csdn.net/Oeljeklaus/article/details/65436866
https://www.w3.org/TR/sparql11-query/#WritingSimpleQueries

Guess you like

Origin www.cnblogs.com/bincoding/p/11223575.html