Skip to Content
学习 SPARQL,第 2 版
book

学习 SPARQL,第 2 版

by Bob DuCharme
July 2025
Beginner to intermediate
386 pages
5h 25m
Chinese
O'Reilly Media, Inc.
Content preview from 学习 SPARQL,第 2 版

第 11 章 SPARQL CookbookSPARQL Cookbook

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章介绍了一些可用于各种数据集的 SPARQL 查询。这些查询在这些情况下特别有用,可以帮助你更多地了解在探索一组新数据时需要处理哪些数据。

在本章中,我们将学习以下内容:

SPARQL 提供了一些简单的功能,你可以用它们来增强本章的大部分查询。 本节将回顾这些功能。

探索数据

本节提供的查询可用于查看特定数据集中的内容--例如,定义了多少结构、使用了多少结构,以及如果没有太多结构,如何仍能了解数据的类型。

创建和更新数据

对数据集进行某些更改时,手工操作非常繁琐,而使用正确的 UPDATE 查询则非常简单,尤其是需要执行各种全局替换时。

主题和变体

本章所展示的查询可分为几个主题,其中许多主题都有几种变体。例如,根据查询输出的一列或多列中的值对输出进行排序,可以更容易地发现模式,尤其是在得到大量结果的情况下;有关如何使用 ORDER BY 短语的详细信息,请参阅数据排序。在本章中,当示例输入提供了足够多的查询结果,排序可以使输出更容易阅读时,你会在这里或那里看到它的使用。

对于某些数据集来说,有些查询要求的信息量很大,甚至可能过多。例如,"使用了哪些属性?"(What Properties Are Used?本章中可能要求过多的查询在LIMIT 50 行末尾注释掉了。如果你看到它,并且将把查询发送到一个大型数据集,请取消注释该行,看看效果如何。也许你可以在决定是否值得注释掉这一行并真正查询所有符合指定模式的三元组之前,将数字提高到 50 以上几次。

在某些数据集上使用这些查询可能比在其他数据集上使用更合理,这就引出了另一个问题:这些查询中有几个效率不高,当你在越来越大的数据集上使用它们时,这就成了一个问题。例如,如果查询包含大量 OPTIONAL 图形模式,或者包含一个以上变量的三重模式,这些都会降低查询速度,尤其是在查询早期出现时。有关哪些 SPARQL 功能会降低大型数据集查询速度的更多背景信息,请参阅第 7 章

在向数据集中添加数据时,最好先以 CONSTRUCT 查询的方式尝试 INSERT 更新请求,以检查如果真的 INSERT 了,会添加哪些三元组。虽然可以将 INSERT 和 DELETE 指令结合在一起,但不能将 CONSTRUCT 和 DELETE 结合在一起,因此在临时将 INSERT 请求作为 CONSTRUCT 查询进行测试时,请记住注释掉任何 DELETE 指令。例如,在按照本章所示的方式运行《如何全局替换属性值?

# filename: ex479.rq

PREFIX dcterms: <http://purl.org/dc/terms/> 

#DELETE {?document dcterms:dateCopyrighted "2013" . }
#INSERT {?document dcterms:dateCopyrighted "2014" . }
CONSTRUCT{?document dcterms:dateCopyrighted "2014" . }
WHERE  {?document dcterms:dateCopyrighted "2013" . }

作为另一个测试,你可以使用注释来临时替换 DELETE 的 CONSTRUCT 语句,以了解到底会删除什么。下面将列出dcterms:dateCopyrighted 值为 "2013 "的三元组,这正是 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

向量数据库 (Chinese Edition)

向量数据库 (Chinese Edition)

Nitin Borwankar
Python文本分析

Python文本分析

Jens Albrecht, Sidharth Ramachandran, Christian Winkler

Publisher Resources

ISBN: 9798341663039