

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

# 使用 SQL 查询您的亚马逊 OpenSearch 服务数据
<a name="sql-support"></a>

您可以使用 SQL 来查询您的亚马逊 OpenSearch 服务，而不必使用基于 JSON 的[OpenSearch 查询](https://docs.opensearch.org/latest/opensearch/query-dsl/full-text/) DSL。如果您已经熟悉该语言，或者希望将您的域与使用该语言的应用程序集成，那么使用 SQL 查询非常有用。SQL 支持适用于运行 OpenSearch 或 Elasticsearch 6.5 或更高版本的域名。

**注意**  
本文档描述了 OpenSearch 服务与各种版本的 SQL 插件以及 JDBC 和 ODBC 驱动程序之间的版本兼容性。有关基本和复杂查询、函数、元数据查询和聚合函数的语法的信息，请参阅开源[OpenSearch文档](https://opensearch.org/docs/latest/search-plugins/sql/sql/index/)。

使用下表查找每个版本 OpenSearch 和 Elasticsearch 版本支持的 SQL 插件版本。


**OpenSearch**  

| OpenSearch 版本 | SQL 插件版本 | 显著功能 | 
| --- | --- | --- | 
| 2.19.0 | [2.19.0.0](https://github.com/opensearch-project/sql/releases/tag/2.19.0.0) |    | 
| 2.18.0 | [2.18.0.0](https://github.com/opensearch-project/sql/releases/tag/2.18.0.0) |    | 
| 2.17.0 | [2.17.0.0](https://github.com/opensearch-project/sql/releases/tag/2.17.0.0) |    | 
| 2.15.0 | [2.15.0.0](https://github.com/opensearch-project/sql/releases/tag/2.15.0.0) |    | 
| 2.13.0 | [2.13.0.0](https://github.com/opensearch-project/sql/releases/tag/2.13.0.0) |    | 
| 2.11.0 | [2.11.0.0](https://github.com/opensearch-project/sql/releases/tag/2.11.0.0) |  添加对 PPL 语言和查询的支持  | 
| 2.9.0 | [2.9.0.0](https://github.com/opensearch-project/sql/releases/tag/2.9.0.0) |  添加 Spark 连接器，支持表和 PromQL 函数  | 
| 2.7.0 | [2.7.0.0](https://github.com/opensearch-project/sql/releases/tag/2.7.0.0) |  添加 `datasource` API  | 
| 2.5.0 | [2.5.0.0](https://github.com/opensearch-project/sql/releases/tag/2.5.0.0) |    | 
| 2.3.0 | [2.3.0.0](https://github.com/opensearch-project/sql/releases/tag/2.3.0.0) |  添加 `maketime` 和 `makedate` 日期时间函数  | 
| 1.3.0 | [1.3.0.0](https://github.com/opensearch-project/sql/releases/tag/1.3.0.0) |  支持默认查询限制大小，以及可从值列表中选择的 IN 子句  | 
| 1.2.0 | [1.2.0.0](https://github.com/opensearch-project/sql/releases/tag/1.2.0.0) |  增加新的可视化响应格式协议  | 
|  1.1.0  | [1.1.0.0](https://github.com/opensearch-project/sql/releases/tag/1.1.0.0) |  支持将匹配函数作为 SQL 和 PPL 中的过滤器  | 
| 1.0.0 | [1.0.0.0](https://github.com/opensearch-project/sql/releases/tag/1.0.0.0) | 支持查询数据流 | 


**Open Distro for Elasticsearch**  

| Elasticsearch 版本 | SQL 插件版本 | 显著功能 | 
| --- | --- | --- | 
| 7.10 | [1.13.0](https://github.com/opendistro-for-elasticsearch/sql/releases/tag/v1.13.0.0) | window 函数 NULL FIRST 和 LAST、CAST()函数，SHOW 和 DESCRIBE 命令 | 
| 7.9 | [1.11.0](https://github.com/opendistro-for-elasticsearch/sql/releases/tag/v1.11.0.0) | 添加其他 date/time 函数，按关键字排序 | 
| 7.8 | [1.9.0](https://github.com/opendistro-for-elasticsearch/sql/releases/tag/v1.9.0.0) |  | 
| 7.7 | [1.8.0](https://github.com/opendistro-for-elasticsearch/sql/releases/tag/v1.8.0.0) |  | 
|  7.3  | [1.3.0](https://github.com/opendistro-for-elasticsearch/sql/releases/tag/v1.3.0.0) | 多个字符串和数字运算符 | 
| 7.1 | [1.1.0](https://github.com/opendistro-for-elasticsearch/sql/releases/tag/v1.1.0.0) |  | 

## 调用示例
<a name="sql-sample"></a>

若要使用 SQL 查询数据，请使用以下格式将 HTTP 请求发送至 `_sql`：

```
POST domain-endpoint/_plugins/_sql
{
  "query": "SELECT * FROM my-index LIMIT 50"
}
```

**注意**  
如果您的域名运行的是 Elasticsearch 而不是 OpenSearch，则格式为。`_opendistro/_sql`

## 说明和差异
<a name="sql-diff"></a>

对 `_plugins/_sql` 的调用在请求正文中包含索引名称，因此与 bulk、mget 和 msearch 操作具有相同的[访问策略注意事项](ac.md#ac-advanced)。与往常一样，在向 API 操作授予权限时，请遵循[最低特权原则](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。

有关将 SQL 与精细访问控制结合使用的安全注意事项，请参阅 [Amazon 服务中的精细访问控制 OpenSearch](fgac.md)。

S OpenSearch QL 插件包含许多[可调整的设置](https://docs.opensearch.org/latest/search-plugins/sql/settings/)。在 S OpenSearch ervice 中，使用`_cluster/settings`路径，而不是插件设置路径 (`_plugins/_query/settings`)：

```
PUT _cluster/settings
{
  "transient" : {
    "plugins.sql.enabled" : true
  }
}
```

对于旧式 Elasticsearch 域，请将 `plugins` 替换为 `opendistro`：

```
PUT _cluster/settings
{
  "transient" : {
    "opendistro.sql.enabled" : true
  }
}
```

## SQL Workbench
<a name="workbench"></a>

SQL Workbench 是一个 OpenSearch 仪表板用户界面，允许你运行按需 SQL 查询、将 SQL 转换为 REST 等效项，以及以文本、JSON、JDBC 或 CSV 的形式查看和保存结果。有关更多信息，请参阅[查询 Workbench](https://docs.opensearch.org/latest/search-plugins/sql/workbench/)。

## SQL CLI
<a name="cli"></a>

SQL CLI 是一个独立的 Python 应用程序，您可以使用 `opensearchsql` 命令启动该应用程序。有关安装、配置和使用步骤，请参阅 [SQL CLI](https://docs.opensearch.org/latest/search-plugins/sql/cli/)。

## JDBC 驱动程序
<a name="jdbc-driver"></a>

Java 数据库连接 (JDBC) 驱动程序允许您将 OpenSearch 服务域与您最喜欢的商业智能 (BI) 应用程序集成。要下载驱动程序，请单击[此处](https://artifacts.opensearch.org/opensearch-clients/jdbc/opensearch-sql-jdbc-1.1.0.1.jar)。有关更多信息，请参阅[GitHub存储库](https://github.com/opensearch-project/sql-jdbc)。

## ODBC 驱动程序
<a name="odbc"></a>

开放式数据库连接 (ODBC) 驱动程序是针对 Windows 和 macOS 的只读 ODBC 驱动程序，让您可将 [Microsoft Excel](https://github.com/opensearch-project/sql-odbc/blob/main/docs/user/microsoft_excel_support.md) 等商业智能和数据可视化应用程序连接到 SQL 插件。

有关安装驱动程序的信息，请参阅[上的 SQL 存储库 GitHub](https://github.com/opensearch-project/sql-odbc)。