编辑:使这个问题更加具体。请提供使用您选择的工具在 Linux 上本地对 .ttl 文件执行 sparql 查询的“hello world”示例。
csvsql
允许通过SQL直接查询.csv
文件(即不导入);例如:
$ csvsql --query "select m.usda_id, avg(i.sepal_length) as
mean_sepal_length from iris as i join irismeta as m on (i.species =
m.species) group by m.species" examples/iris.csv examples/irismeta.csv
我希望能够在 Turtle .ttl
或其他典型的 RDF 文件上使用SPARQL 进行类似的查询。
如何为SPARQL和Turtle或类似文件实现类似的“一次性”“直接查询”功能? (例如,将给定文件加载到内存中的小脚本,例如运行 blazegraph 实例,并运行查询返回结果并提取内存中的内容;或者可能使用
librdf
,例如 Rasqal/Redland 或 Neo4j 或 任何其他 SPARQL 实现 - 最好是不运行后台实例的东西,一次性,KISS)
恕我直言,这样的工具对于业余爱好者和爱好者来说非常有用,他们可能想在不启动完整服务器的情况下以三元组存储数据并查询它。这对教育目的也非常有益。
您能否提供由代码片段支持的具体示例,展示如何做到这一点? (本地 Linux 上)
Rasqal 的命令行查询工具
roqet
可以做到这一点;参见http://librdf.org/rasqal/roqet.html
。
http://triplr.org/query
。
编辑示例如何工作和打包:
Rasqal 示例包:
让我们尝试从以下位置执行“hello world”查询:https://wiki.blazegraph.com/wiki/index.php/Quick_Start教程。
这是示例
data.ttl
文件:
PREFIX : <http://blazegraph.com/>
PREFIX schema: <http://schema.org/>
:systap a schema:Organization ;
schema:owns :blazegraph .
:blazegraph a schema:Product ;
schema:brand :systap;
:productOf <http://systap.com/>;
:implements <http://rdf4j.org>,<http://blueprints.tinkerpop.com> .
示例“hello world”Sparql 查询:
$ roqet -i sparql -e 'SELECT * WHERE { <http://blazegraph.com/blazegraph> ?p ?o }' -D data.ttl
roqet: Running query 'SELECT * WHERE { <http://blazegraph.com/blazegraph> ?p ?o }'
roqet: Query has a variable bindings result
row: [p=uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, o=uri<http://schema.org/Product>]
row: [p=uri<http://schema.org/brand>, o=uri<http://blazegraph.com/systap>]
row: [p=uri<http://blazegraph.com/productOf>, o=uri<http://systap.com/>]
row: [p=uri<http://blazegraph.com/implements>, o=uri<http://rdf4j.org>]
row: [p=uri<http://blazegraph.com/implements>, o=uri<http://blueprints.tinkerpop.com>]
roqet: Query returned 5 results
或者更通用
$ roqet -i sparql -e 'SELECT * WHERE { ?s ?p ?o }' -D data.ttl | xsel -b
roqet: Running query 'SELECT * WHERE { ?s ?p ?o }'
roqet: Query has a variable bindings result
row: [s=uri<http://blazegraph.com/systap>, p=uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, o=uri<http://schema.org/Organization>]
row: [s=uri<http://blazegraph.com/systap>, p=uri<http://schema.org/owns>, o=uri<http://blazegraph.com/blazegraph>]
row: [s=uri<http://blazegraph.com/blazegraph>, p=uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, o=uri<http://schema.org/Product>]
row: [s=uri<http://blazegraph.com/blazegraph>, p=uri<http://schema.org/brand>, o=uri<http://blazegraph.com/systap>]
row: [s=uri<http://blazegraph.com/blazegraph>, p=uri<http://blazegraph.com/productOf>, o=uri<http://systap.com/>]
row: [s=uri<http://blazegraph.com/blazegraph>, p=uri<http://blazegraph.com/implements>, o=uri<http://rdf4j.org>]
row: [s=uri<http://blazegraph.com/blazegraph>, p=uri<http://blazegraph.com/implements>, o=uri<http://blueprints.tinkerpop.com>]
roqet: Query returned 7 results
具体的 Apache Jena 命令是
sparql
。
http://apache.org/dist/jena/binaries/
的二进制下载。解压后,有 bin/
和 bat/
目录可以从命令行运行脚本。
Apache Jena SPARQL CLI 设置
在 Ubuntu 23.10 上,有一个软件包
libapache-jena-java
,但它没有公开 CLI 工具,这很烦人。
要安装它们,我们必须下载预构建的二进制文件,部分记录在https://jena.apache.org/documentation/tools/
sudo apt install openjdk-22-jre
wget https://dlcdn.apache.org/jena/binaries/apache-jena-4.10.0.zip
unzip apache-jena-4.10.0.zip
cd apache-jena-4.10.0
export JENA_HOME="$(pwd)"
export PATH="$PATH:$(pwd)/bin"
我们可以确认它适用于:
sparql -version
输出:
Apache Jena version 4.10.0
只需确保您有足够新的 Java 来运行这些二进制文件,并且
JAVA_HOME
指向它。
然后要实际进行查询,我们可以查看SPARQL教程:https://jena.apache.org/tutorials/sparql.html
以 Turtle 语法给出的数据文件:
mydata.ttl
@prefix vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <#> .
<http://somewhere/MattJones/>
vCard:FN "Matt Jones" ;
vCard:N [ vCard:Family
"Jones" ;
vCard:Given
"Matthew"
] .
<http://somewhere/RebeccaSmith/>
vCard:FN "Becky Smith" ;
vCard:N [ vCard:Family
"Smith" ;
vCard:Given
"Rebecca"
] .
<http://somewhere/JohnSmith/>
vCard:FN "John Smith" ;
vCard:N [ vCard:Family
"Smith" ;
vCard:Given
"John"
] .
<http://somewhere/SarahJones/>
vCard:FN "Sarah Jones" ;
vCard:N [ vCard:Family
"Jones" ;
vCard:Given
"Sarah"
] .
以及一个 SPARQLE 查询文件,用于查询全名“John Smith:
”的用户myquery.rq
SELECT ?x
WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith" . }
我们可以直接在
.ttl
上运行查询,如下所示:
sparql --data=mydata.ttl --query=myquery.rq
输出所需的:
---------------------------------
| x |
=================================
| <http://somewhere/JohnSmith/> |
---------------------------------
在 Ubuntu 23.10 上测试。