是否有一个带有RDFLib的SPARQL的Hello World示例?

时间:2013-05-30 06:27:18

标签: python sparql rdflib

我希望尽可能小,但仍然是自我一致的,并且使用RDFLib中的SPARQL。我有RDFLib版本'4.0.1'。

我想要一个执行以下操作的代码

  1. 导入RDFLib。
  2. 创建一个简单的图表(从2到4个关系)
  3. 将此图形以rdf格式写入文件。
  4. 从文件中读取图表。
  5. 使用SPARQL从图表中提取内容。
  6. ADDED

    我自己尝试过(首先没有写入和读取文件),我无法做到。这就是我所拥有的:

    import rdflib
    
    g = rdflib.ConjunctiveGraph()
    
    has_border_with = rdflib.URIRef('www.example.org/has_border_with')
    located_in = rdflib.URIRef('www.example.org/located_in')
    
    germany = rdflib.URIRef('www.example.org/country1')
    france = rdflib.URIRef('www.example.org/country2')
    china = rdflib.URIRef('www.example.org/country3')
    mongolia = rdflib.URIRef('www.example.org/country4')
    
    europa = rdflib.URIRef('www.example.org/part1')
    asia = rdflib.URIRef('www.example.org/part2')
    
    g.add((germany,has_border_with,france))
    g.add((china,has_border_with,mongolia))
    g.add((germany,located_in,europa))
    g.add((france,located_in,europa))
    g.add((china,located_in,asia))
    g.add((mongolia,located_in,asia))
    
    x = g.query("""select ?country where { ?country www.example.org/located_in www.example.org/part1 }""")
    print x
    

    结果我得到了:

    Traceback (most recent call last):
      File "hello_world.py", line 23, in <module>
        x = g.query("""select ?country where { ?country www.example.org/located_in www.example.org/part1 }""")
      File "/usr/local/lib/python2.7/dist-packages/rdflib-4.0.1-py2.7.egg/rdflib/graph.py", line 1045, in query
        query_object, initBindings, initNs, **kwargs))
      File "/usr/local/lib/python2.7/dist-packages/rdflib-4.0.1-py2.7.egg/rdflib/plugins/sparql/processor.py", line 72, in query
        parsetree = parseQuery(strOrQuery)
      File "/usr/local/lib/python2.7/dist-packages/rdflib-4.0.1-py2.7.egg/rdflib/plugins/sparql/parser.py", line 1034, in parseQuery
        return Query.parseString(q, parseAll=True)
      File "/usr/lib/python2.7/dist-packages/pyparsing.py", line 1032, in parseString
        raise exc
    pyparsing.ParseException: Expected "}" (at char 24), (line:1, col:25)
    

1 个答案:

答案 0 :(得分:9)

有几个问题:

  1. http://
  2. 开头命名您的资源
  3. SPARQL查询中的网址需要<>
  4. 使用简单的Graph代替ConjunctiveGraph
  5. 您可以使用Graph.serializeGraph.parse方法保存并从文件中读取(请参阅代码)
  6. 尝试对示例代码进行以下修改:

    import rdflib
    
    g = rdflib.Graph()
    has_border_with = rdflib.URIRef('http://www.example.org/has_border_with')
    located_in = rdflib.URIRef('http://www.example.org/located_in')
    
    germany = rdflib.URIRef('http://www.example.org/country1')
    france = rdflib.URIRef('http://www.example.org/country2')
    china = rdflib.URIRef('http://www.example.org/country3')
    mongolia = rdflib.URIRef('http://www.example.org/country4')
    
    europa = rdflib.URIRef('http://www.example.org/part1')
    asia = rdflib.URIRef('http://www.example.org/part2')
    
    g.add((germany,has_border_with,france))
    g.add((china,has_border_with,mongolia))
    g.add((germany,located_in,europa))
    g.add((france,located_in,europa))
    g.add((china,located_in,asia))
    g.add((mongolia,located_in,asia))
    
    q = "select ?country where { ?country <http://www.example.org/located_in> <http://www.example.org/part1> }"
    x = g.query(q)
    print list(x)
    # write graph to file, re-read it and query the newly created graph
    g.serialize("graph.rdf")
    g1 = rdflib.Graph()
    g1.parse("graph.rdf", format="xml")
    x1 = g1.query(q)
    print list(x1)