rdflib的存储后端,允许读取和查询HDT文档

rdflib-hdt的Python项目详细描述


rdflib-htd logo

Build StatusPyPI version

rdflib的存储后端,允许读取和查询HDT文档。在

Online Documentation

要求

  • Pythonversion 3.6.4或更高版本

  • pip

  • gcc/clang支持c++11

  • Python开发头文件 .. 在

    You should have the ^{tt1}$ header available on your system.For example, for Python 3.6, install the ^{tt2}$ package on Debian/Ubuntu systems.

安装

{a8}强烈建议安装{a7}!

手动安装

要求:pipenv

^{pr2}$

入门

您可以在两种模式下使用rdflib-hdt库:作为rdflib图或作为原始HDT文档。在

HDT文档使用

fromrdflib_hdtimportHDTDocument# Load an HDT file. Missing indexes are generated automatically.# You can provide the index file by putting them in the same directory than the HDT file.document=HDTDocument("test.hdt")# Display some metadata about the HDT document itselfprint(f"Number of RDF triples: {document.total_triples}")print(f"Number of subjects: {document.nb_subjects}")print(f"Number of predicates: {document.nb_predicates}")print(f"Number of objects: {document.nb_objects}")print(f"Number of shared subject-object: {document.nb_shared}")# Fetch all triples that matches { ?s foaf:name ?o }# Use None to indicates variablestriples,cardinality=document.search_triples((None,FOAF("name"),None))print(f"Cardinality of (?s foaf:name ?o): {cardinality}")fors,p,ointriples:print(triple)# The search also support limit and offsettriples,cardinality=document.search_triples((None,FOAF("name"),None),limit=10,offset=100)# etc ...

HDT文档还支持在一组三元组模式上评估连接。在

fromrdflib_hdtimportHDTDocumentfromrdflibimportVariablefromrdflib.namespaceimportFOAF,RDFdocument=HDTDocument("test.hdt")# find the names of two entities that know each othertp_a=(Variable("a"),FOAF("knows"),Variable("b"))tp_b=(Variable("a"),FOAF("name"),Variable("name"))tp_c=(Variable("b"),FOAF("name"),Variable("friend"))query=set([tp_a,tp_b,tp_c])iterator=document.search_join(query)print(f"Estimated join cardinality: {len(iterator)}")# Join results are produced as ResultRow, like in the RDFlib SPARQL APIforrowiniterator:print(f"{row.name} knows {row.friend}")

在python中处理非UTF-8字符串

如果HDT文档是用非UTF-8编码编码的,则前面的代码将无法正常工作,并将导致UnicodeDecodeError。 有关如何将字符串从C++转换为Python ^ {A11}

的更多细节

为了解决这个问题,我们将HDT文档的API增加了一倍:

  • search_triples_bytes(...)返回一个三元组的迭代器(py::bytes,py::bytes,py::bytes)
  • search_join_bytes(...)返回映射为py::set(py::bytes,py::bytes)
  • convert_tripleid_bytes(...)返回三元组为:(py::bytes,py::bytes,py::bytes)
  • convert_id_bytes(...)返回一个py::bytes

参数和文档与标准版本相同

fromrdflib_hdtimportHDTDocumentdocument=HDTDocument("test.hdt")it=document.search_triple_bytes("","","")fors,p,oinit:print(s,p,o)# print b'...', b'...', b'...'# now decode it, or handle any errortry:s,p,o=s.decode('UTF-8'),p.decode('UTF-8'),o.decode('UTF-8')exceptUnicodeDecodeErroraserr:# try another other codecs, ignore error, etcpass

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将cassandra中的行数据转换为与列相关的嵌套json   java如何使用jcr XPath在jcr:content/@jcr:data中搜索?   java在使用openCV进行安卓开发时如何利用手机的广角镜头   java解析扩展了接口,结束了一个潜在的无限循环   位置服务的@Override方法中存在java Android应用程序错误   java本地线程的用途和需求是什么   具有左右子访问的java节点树遍历   java验证JsonWebToken签名   JUL日志处理程序中的java日志记录   嵌入式Java读取给定时间段的串行数据。   java有没有办法从多个URL获取多个图像?   java线程通过等待intent阻止自己发送intent   java Spring MVC解析多部分内容请求   java JPA/Hibernate静态元模型属性未填充NullPointerException   java格式错误的字符(需要引号,得到I)~正在处理   java为什么PrintWriter对象抛出FileNotFoundException?   java Neo4j未正确保存标签   java IE不加载图像