Home  >  Article  >  Backend Development  >  Build Semantic Web CRUD Operations with PHP_PHP Tutorial

Build Semantic Web CRUD Operations with PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:37:16866browse

创建、读、更新和删除(Create/Read/Update/Delete,CRUD)操作是最基本的数据库操作,但是它们也是最重要的操作。CRUD 操作通常是使用关系数据库系统中的结构化查询语言(Structured Query Language,SQL)完成的。随着 Web 变得更加具有面向数据特性,因此需要从基于 SQL 的 CRUD 操作转移到基于语义 Web 的 CRUD 操作。了解如何使用PHP通过基于语义的 Web 执行 CRUD 操作。 常用缩写词

API — 应用程序编程接口(Application Programming Interface)

CRUD — 创建/读/更新/删除(Create/Read/Update/Delete)

HTTP —超文本传输协议(Hypertext Transfer Protocol)

MVC — 模式—视图—控制器(Model-View-Controller)

OOP — 面向对象的编程(Object-Oriented Programming)

RDF — 资源描述框架(Resource Description Framework)

SPARQL — 简单协议和 RDF 查询语言(Simple Protocol and RDF Query Language)

SQL — 结构化查询语言(Structured Query Language)

UI — 用户界面(User interface)

W3C — 万维网联盟(World Wide Web Consortium)

在开发Web 应用程序时,为逻辑层和 UI 层创建放置服务器端代码的数据库结构是一种标准实践。要连接到数据库,服务器端代码需要执行一些基本的创建、更新、删除和 — 最重要的 — 读取记录等操作。由于 Web 应用程序的后台数据库通常都是关系数据库,因此这些 CRUD 操作都是使用众所周知的 SQL 语言执行的。但是,随着 Web 开发越来越多地采用面向对象的编程(OOP),模型也随之发生改变。

资源描述框架(Resource Description Framework,RDF)是描述对象同时保留数据含义的理想方法。简单协议和 RDF 查询语言(Simple Protocol and RDF Query Language,SPARQL — 发音为 “sparkle”)是通常用于针对该数据进行查询的语言,因为它在语句构成上匹配 RDF 本身的结构。RDF 和 SPARQL 都是所谓 语义 Web 栈(semantic Web stack)中的技术。

要彻底地应用语义 Web 理念,您可以使用 SPARQL 将传统的 Web 开发技术应用到 RDF 数据中。本文将展示如何使用简化的模式—视图—控制器(Model-View-Controller,MVC)设计模型、PHP 服务器端脚本语言和 SPARQL 连接到 RDF — 与使用关系数据库系统中的 SQL 相反。

SQL 和 SPARQL CRUD 操作

先决条件 本文假定您基本了解 SQL、PHP 和 Web 应用程序开发。了解语义 Web 也十分有利。要对基于语义 Web 的数据运行 create、update 和 delete 命令,需要具有支持 SPARQL/Update 规范的语义 Web 数据库。

在使用 SQL 和 SPARQL 进行开发时,需要查看一下 CRUD 操作之间的异同。清单 1 显示了 read 操作的 SQL 代码。

SELECT realname, dob, locationFROM UserTable WHERE realname = "John Smith";

将这段基于 SQL 的代码与清单 2 中所示的基于 SPARQL 的代码相比较。采用这两个 read 操作的原因在于它们最易于理解、实现和说明。这对于 SQL 和 SPARQL 来说都是一样的。

PREFIX foaf: PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#>SELECT ?uri ?name ?dob ?locationFROMhttp://www.example.org/graph>WHERE{ ?urirdf:type foaf:Person ;foaf:name "John Smith" ;foaf:birthday?dob ;foaf:location ?location .} ;

在比较两张清单时,您的第一个想法很可能是 SPARQL 版本明显比 SQL 版本长很多。这是事实,但是请不要误以为 SQL 必然更简单而且更干净。根据所运行引擎的不同,SPARQL 可以全部通过称为链接数据结果(linked data effect)的内容进行分发。此外,它允许拥有动态模式,因为它拥有互相链接的面向对象的透视图,与严格的 SQL 关系透视图形成对照。如果您想要把关系数据库表分隔为许多数据孤岛,则实际上使用的 SQL 代码行将比 SPARQL 多很多 — 更不必说 SQL 中会出现大量令人讨厌的 JOIN 描述符。

SPARQL 的前两行是 PREFIX 声明。根据语义 Web 理论,一切内容 — 无论是对象还是数据图来源(也是一个对象)— 都有统一资源标识符(Uniform Resource Identifier,URI)。PREFIX 行只是将临时标签应用到一些 URI 中 — 在本例中为 Friend of a Friend 和 RDF 模式。其中的好处是您以后可以在查询中使用 PREFIX 声明而不必使用完整的 URI。

SPARQL 代码的下一行描述了查询请求。这条语句在本质上与 SQL 语句相同,不同之处是对 URI 的附加请求。注意问号的使用(?)是为了表示术语是变量。

The FROM statement describes the location from which data is obtained. This is the same in SQL and SPARQL, except that in SPARQL the data source name is a URI rather than a string representing a physical location on your computer or network.

The WHERE statements of the two are completely different, because with SPARQL, you must specify the schema used to get the data. Again, if you've tried to do this using the relational approach, it's much more expensive than plain SQL: you need to use PHP, the Java programming language, or some other server-side language to perform the check between data sources. What the SPARQL line of code accomplishes is relatively self-explanatory, including ensuring that the data being retrieved is only of type Person. SPARQL will get the name and location and perform some pattern matching to find the correct John Smith.

Create

CRUD operations in SPARQL are often more arcane than read operations. However, these operations can be completed. First, the create operation inserts a new record or object into the table or chart.

INSERT INTO UserTable (realname, dob, location) VALUES ("John Smith", "1985-01-01", "Bristol, UK");

Now, compare the create operation in the SQL-based code in Listing 3 with the SPARQL-based code in Listing 4.

PREFIX foaf: PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax- ns#>INSERT INTO GRAPH (?realname, ?dob, ?location) { rdf:Type foaf:Person ; foaf:name "John Smith" ; foaf:birthday <1985-01-01T00:00:00> ; foaf:location "Bristol, UK" }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486568.htmlTechArticleCreate/Read/Update/Delete (CRUD) operations are the most basic database operations operations, but they are also the most important operations. CRUD operations are typically performed using relational data...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn