

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 SPARQL UPDATE UNLOAD 从 Neptune 中删除数据
<a name="sparql-api-reference-unload"></a>

Neptune 还提供了自定义 SPARQL 操作 `UNLOAD`，用于删除远程源中指定的数据。`UNLOAD` 可以视为 `LOAD` 操作的反操作。它的语法如下：

```
UNLOAD SILENT (URL of the remote data to be unloaded) FROM GRAPH (named graph from which to remove the data)
```
+ **`SILENT`** –（*可选*）即使在处理数据时出错，也使操作返回成功。

  当单个事务包含多个语句（例如 `"LOAD ...; LOAD ...; UNLOAD ...; LOAD ...;"`）并且即使某些远程数据无法处理也希望事务完成时，这可能很有用。
+ *URL of the remote data to be unloaded*—（*必需*）指定包含要从图表中卸载的数据的远程数据文件。

  远程文件必须具有以下扩展名之一（这些扩展名与 UPDATE-LOAD 支持的格式相同）：
  + `.nt`对于 NTriples。
  + `.nq`对于 NQuads。
  + `.trig` 表示 Trig。
  + `.rdf` 表示 RDF/XML。
  + `.ttl` 表示 Turtle。
  + `.n3` 表示 N3。
  + `.jsonld` 表示 JSON-LD。

  `UNLOAD` 操作将从您的数据库集群中删除该文件包含的所有数据。

  要卸载的数据的 URL 中必须包含任何 Amazon S3 身份验证。您可以预签署 Amazon S3 文件，然后使用生成的 URL 来安全地访问该文件。例如：

  ```
  aws s3 presign --expires-in (number of seconds) s3://(bucket name)/(path to file of data to unload)
  ```

  然后：

  ```
  curl https://(a Neptune endpoint URL):8182/sparql \
    --data-urlencode 'update=unload (pre-signed URL of the remote Amazon S3 data to be unloaded) \
                             from graph (named graph)'
  ```

  有关更多信息，请参阅[身份验证请求：使用查询参数](https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html)。
+ **`FROM GRAPH `***(named graph from which to remove the data)*—（*可选*）指定应从中卸载远程数据的命名图。

  Neptune 将每个三元组与一个命名图形相关联。您可以使用后备命名图形 URI `http://aws.amazon.com/neptune/vocab/v01/DefaultNamedGraph` 来指定默认的命名图形，如下所示：

  ```
  FROM GRAPH <http://aws.amazon.com/neptune/vocab/v01/DefaultNamedGraph>
  ```

按照 `LOAD` 与 `INSERT DATA { (inline data) }` 对应的相同方式，`UNLOAD` 对应于 `DELETE DATA { (inline data) }`。与 `DELETE DATA` 相似，`UNLOAD` 不适用于包含空白节点的数据。

例如，如果本地 Web 服务器提供的文件命名为 `data.nt`，其中包含以下 2 个三元组：

```
<http://example.org/resource#a> <http://example.org/resource#p> <http://example.org/resource#b> .
<http://example.org/resource#a> <http://example.org/resource#p> <http://example.org/resource#c> .
```

以下 `UNLOAD` 命令将从命名图形 `<http://example.org/graph1>` 中删除这两个三元组：

```
UNLOAD <http://localhost:80/data.nt> FROM GRAPH <http://example.org/graph1>
```

这与使用以下 `DELETE DATA` 命令的效果相同：

```
DELETE DATA {
  GRAPH <http://example.org/graph1> {
    <http://example.org/resource#a> <http://example.org/resource#p> <http://example.org/resource#b> .
    <http://example.org/resource#a> <http://example.org/resource#p> <http://example.org/resource#c> .
  }
}
```

**`UNLOAD` 命令引发的异常**
+ **`InvalidParameterException`** – 数据中有空白节点。*HTTP 状态*：400 错误请求。

  *消息*：` Blank nodes are not allowed for UNLOAD`

   
+ **`InvalidParameterException`** – 数据中存在语法错误。*HTTP 状态*：400 错误请求。

  *消息*：`Invalid syntax in the specified file.`

   
+ **`UnloadUrlAccessDeniedException `** – 访问被拒绝。*HTTP 状态*：400 错误请求。

  *消息*：`Update failure: Endpoint (Neptune endpoint) reported access denied error. Please verify access.`

   
+ **`BadRequestException `** – 无法检索远程数据。*HTTP 状态*：400 错误请求。

  *消息*：*（取决于 HTTP 响应）。*