通过 dsql 进行查询

This commit is contained in:
YuCheng Hu 2021-07-31 17:33:00 -04:00
parent 7219b643e1
commit f9b2199b8f
No known key found for this signature in database
GPG Key ID: C395DC68EF030B59
2 changed files with 43 additions and 64 deletions

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@ -45,25 +45,21 @@ Druid 控制台提供了视图能够让用户更加容易的在 Druid 进行查
![Query results](../assets/tutorial-query-04.png "Query results") ![Query results](../assets/tutorial-query-04.png "Query results")
Notice that the results are limited in the console to about a hundred, by default, due to the **Smart query limit** 需要注意的是,通过控制台进行查询的返回结果集被限制为默认 100 条记录,这是在 **Smart query limit** 特性中进行配置的。
feature. This helps users avoid inadvertently running queries that return an excessive amount of data, possibly 这个能够帮助用户避免在运行查询的时候返回大量的数据,有可能会让其系统过载。
overwhelming their system.
7. Let's edit the query directly and take a look at a few more query building features in the editor. 7. 让我们对上面的查询语句进行一些编辑来看看在查询构建器中能够提供那些特性,请在查询构建起器中进行下面的一些修改:
Click in the query edit pane and make the following changes:
1. Add a line after the first column, `"page"` and Start typing the name of a new column, `"countryName"`. Notice that the autocomplete menu suggests column names, functions, keywords, and more. Choose "countryName" and 1. 第一列的 `"page"` 后面开始输入一个新列的名字 `"countryName"`。请注意自动完成菜单将会针对你输入的字符提示 列名,函数,关键字以及其他的内容
add the new column to the GROUP BY clause as well, either by name or by reference to its position, `2`. 选择 "countryName" 和添加新的列到 GROUP BY 语句中,可以通过名字或者位置 `2` 来完成操作。
2. For readability, replace `Count` column name with `Edits`, since the `COUNT()` function actually 2. 为了让我们的 SQL 更加具有可读性,将 `Count` 列的名字替换为 `Edits`,这是因为这一列是使用 `COUNT()` 函数来进行计算的,实际上的目的是返回编辑的次数。
returns the number of edits for the page. Make the same column name change in the ORDER BY clause as well. 在 ORDER BY 语句中使用同样的名字来进行排序。
The `COUNT()` function is one of many functions available for use in Druid SQL queries. You can mouse over a function name `COUNT()` 函数是 Druid 提供的多个可用函数的一个。你可以将你的鼠标移动到函数的名字上面,在随后弹出的自动完成对话框中将会对函数的功能进行一个简要的描述
in the autocomplete menu to see a brief description of a function. Also, you can find more information in the Druid 同时,你可以可以通过 Druid 的文档来了解更多的内容,例如, `COUNT()` 函数的文档位于 [Aggregation functions](../querying/sql.md#aggregation-functions) 页面中。
documentation; for example, the `COUNT()` function is documented in
[Aggregation functions](../querying/sql.md#aggregation-functions).
The query should now be: 当完成上面的所有操作后,你的 SQL 脚本应该看起来和下面的是一样的了:
```sql ```sql
SELECT SELECT
@ -75,27 +71,25 @@ returns the number of edits for the page. Make the same column name change in th
ORDER BY "Edits" DESC ORDER BY "Edits" DESC
``` ```
When you run the query again, notice that we're getting the new dimension,`countryName`, but for most of the rows, its value 当你对上面的 SQL 脚本再次运行以后你会注意到我们会返回一个新的列dimension`countryName`,但是这一列的大部分行的值都是空的。
is null. Let's 让我们通过修改 SQL 来只显示 `countryName` 不为空的行。
show only rows with a `countryName` value.
8. Click the countryName dimension in the left pane and choose the first filtering option. It's not exactly what we want, but 8. 单击 countryName 这一列在左侧的面部中选择第一个过滤器first filtering的选项。这个过滤器的内容可能并不是我们想要的我们会在后面对其进行编辑
we'll edit it by hand. The new WHERE clause should appear in your query. WHERE 语句将会显示在你的查询中。
9. Modify the WHERE clause to exclude results that do not have a value for countryName: 9. 修改 WHERE 语句来将 countryName 不为空的列去除掉。
```sql ```sql
WHERE "countryName" IS NOT NULL WHERE "countryName" IS NOT NULL
``` ```
Run the query again. You should now see the top edits by country: 然后再次运行修改后的 SQL 脚本,你应该可以只看到编辑次数最多的国家:
![Finished query](../assets/tutorial-query-035.png "Finished query") ![Finished query](../assets/tutorial-query-035.png "Finished query")
10. Under the covers, every Druid SQL query is translated into a query in the JSON-based _Druid native query_ format before it runs 10. 在 Druid 使用 SQL 进行查询的后面,所有的 Druid SQL 查询都可以被转换为基于 JSON 格式的 _Druid native query_ 来在 Druid 的数据节点中进行查询。
on data nodes. You can view the native query for this query by clicking `...` and **Explain SQL Query**. 你可以通过单击查询运行按钮的后面`...` 然后选择 **Explain SQL Query** 来进行查看。
While you can use Druid SQL for most purposes, familiarity with native query is useful for composing complex queries and for troubleshooting 尽管你可以在大部分的情况下使用 Druid SQL但是如果你能够了解 Druid 原生查询的意义,那么对你在问题解决和有关性能问题的调试上面会更加有效,请参考 [Native queries](../querying/querying.md) 页面来获得更多信息。
performance issues. For more information, see [Native queries](../querying/querying.md).
![Explain query](../assets/tutorial-query-06.png "Explain query") ![Explain query](../assets/tutorial-query-06.png "Explain query")
@ -116,17 +110,18 @@ performance issues. For more information, see [Native queries](../querying/query
from the command line or over HTTP. from the command line or over HTTP.
9. Finally, click `...` and **Edit context** to see how you can add additional parameters controlling the execution of the query execution. In the field, enter query context options as JSON key-value pairs, as described in [Context flags](../querying/query-context.md). 11. 最后,单击 `...` 然后选择 **Edit context** 来查看你可以添加的其他参数来控制查询的执行。
在这个字段中,可以通过输入基于 JSON 格式的 key-value 对,请参考 [Context flags](../querying/query-context.md) 页面描述的更多内容。
That's it! We've built a simple query using some of the query builder features built into the Druid Console. The following 上面就是我们如何通过使用 Druid 控制的查询构建特性来构建的一个简单的数据查询。
sections provide a few more example queries you can try. Also, see [Other ways to invoke SQL queries](#other-ways-to-invoke-sql-queries) to learn how 在本页面的后续部分提供了更多的一些你可以尝试使用的查询实例。同时请查看 [进行查询的其他方法](#进行查询的其他方法) 部分中的内容来了解如何
to run Druid SQL from the command line or over HTTP. 在命令行工具或者 HTTP 上运行 Druid SQL 查询。
## More Druid SQL examples ## 更多的查询实例
Here is a collection of queries to try out: 下面是你可以在 Druid 上尝试进行查询的一些实例供你测试:
### Query over time ### 对时间进行查询
```sql ```sql
SELECT FLOOR(__time to HOUR) AS HourTime, SUM(deleted) AS LinesDeleted SELECT FLOOR(__time to HOUR) AS HourTime, SUM(deleted) AS LinesDeleted
@ -136,7 +131,7 @@ GROUP BY 1
![Query example](../assets/tutorial-query-07.png "Query example") ![Query example](../assets/tutorial-query-07.png "Query example")
### General group by ### 基本的 group by
```sql ```sql
SELECT channel, page, SUM(added) SELECT channel, page, SUM(added)
@ -148,13 +143,13 @@ ORDER BY SUM(added) DESC
![Query example](../assets/tutorial-query-08.png "Query example") ![Query example](../assets/tutorial-query-08.png "Query example")
## Other ways to invoke SQL queries ## 进行查询的其他方法
### Query SQL via dsql ### 通过 dsql 进行查询
For convenience, the Druid package includes a SQL command-line client, located at `bin/dsql` in the Druid package root. 为了便于使用Druid 包中还提供了一个 SQL 命令行客户端工具,这个工具位于 `bin/dsql` 目录中。
Let's now run `bin/dsql`; you should see the following prompt: 如果你直接运行 `bin/dsql` 的话,你将会看到下面的提示输出:
```bash ```bash
Welcome to dsql, the command-line client for Druid SQL. Welcome to dsql, the command-line client for Druid SQL.
@ -162,7 +157,7 @@ Type "\h" for help.
dsql> dsql>
``` ```
To submit the query, paste it to the `dsql` prompt and press enter: 如果希望进行查询的话,将你的 SQL 张贴到 `dsql` 提示光标后面,然后单击回车:
```bash ```bash
dsql> SELECT page, COUNT(*) AS Edits FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY page ORDER BY Edits DESC LIMIT 10; dsql> SELECT page, COUNT(*) AS Edits FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY page ORDER BY Edits DESC LIMIT 10;
@ -248,34 +243,8 @@ See the [Druid SQL documentation](../querying/sql.md) for more information on us
See the [Queries documentation](../querying/querying.md) for more information on Druid native queries. See the [Queries documentation](../querying/querying.md) for more information on Druid native queries.
## 查询数据
本教程将以Druid SQL和Druid的原生查询格式的示例演示如何在Apache Druid中查询数据。
本教程假定您已经完成了摄取教程之一因为我们将查询Wikipedia编辑样例数据。
* [加载本地文件](tutorial-batch.md)
* [从Kafka加载数据](./chapter-2.md)
* [从Hadoop加载数据](./chapter-3.md)
Druid查询通过HTTP发送,Druid控制台包括一个视图用于向Druid发出查询并很好地格式化结果。
### Druid SQL查询
Druid支持SQL查询。
该查询检索了2015年9月12日被编辑最多的10个维基百科页面
```json
SELECT page, COUNT(*) AS Edits
FROM wikipedia
WHERE TIMESTAMP '2015-09-12 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2015-09-13 00:00:00'
GROUP BY page
ORDER BY Edits DESC
LIMIT 10
```
让我们来看几种不同的查询方法
#### 通过控制台查询SQL #### 通过控制台查询SQL