是否可以通过 REST API 从 Metabase MBQL / SQL 查询中获取原始数据?
Is it possible to get raw data from a Metabase MBQL / SQL query via the REST API?
是否有采用 MBQL/SQL 查询和 returns 原始数据的元数据库 REST API?
我可以在两步过程中通过 API 执行 MBQL 查询,方法是通过 Metabase Web 应用程序 UI 创建问题然后查询问题的中间步骤,但是我还没有想出如何一步结合 MBQL 和 REST API。
有些项目我想通过在 API 请求中使用 MBQL 而不是 UI 生成的问题来完成:
- 更好的版本管理,因为可以使用代码将 MBQL 查询签入源代码管理
- 更好的隔离,因为 API 调用将不依赖于可以更改的问题
这里有一些关于如何执行两步过程的信息。
两步过程
两步过程是:
- 使用网络应用程序创建一个 MBQL/SQL 元数据库问题
- 使用 REST API 查询使用卡片在网络应用程序中创建的现有问题 API
步骤 1) 通过 Web 创建问题 UI
登录网络应用程序并单击顶部菜单中的 "New Question" 按钮。
创建问题后,您将被引导至如下所示的 URL,其中 :question-id
是一个整数。
- Web UI 端点:
GET /question/:question-id
记下这个值并在下一步的API中使用它。
Note: an alternative for creating the card is to use the POST /api/card
API endpoint per YakovL. This can be useful in some scenarios were UI questions/cards are desirable, but I'm also trying to avoid creating creating cards / questions in the first place, since I'm not planning on using the Metabase UI to consume them. Reasons to avoid cards for me include needing to perform extra work to verify the card query definitions haven't changed but still having the SQL in the code to create the cards, and generate a lot of unneeded question cards in the UI.
步骤 2) REST API 问题数据
API 将术语 "card" 用于 Web UI "question" 对象,因此对以下 Card [=89] 进行 API 调用=]:
- API 端点:
POST /api/card/:card-id/query/:export-format
在此URL中:
:card-id
是来自网络的 :question-id
UI URL
:export-format
可以是 json
或其他格式
有关 API 的更多信息可在 API 文档中找到:
https://github.com/metabase/metabase/blob/master/docs/api-documentation.md
问题
有没有一种方法可以直接在 API 请求中发送 MBQL/SQL 查询而不需要预先存在 Question/Card?
通过原始查询 SQL 和 MBQL 都可以通过 POST /api/dataset/
API 获得。端点的文档提到了 query
请求定义,但没有定义它。
我最后做了一些更多的研究,并在 Metabase Discourse 论坛上提问。以下示例由 sbelak.
发布
原始SQL查询
我能够使用 go-metabase SDK 成功进行本机 SQL 查询以发出以下请求:
POST /api/dataset
Content-Type: application/json
X-Metabase-Session: <sessionId>
{
"database": 1,
"native": {
"query": "SELECT COUNT(*) FROM orders"
},
type: "native"
}
Notes:
- The
POST /api/dataset
does not set the response Content-Type
header.
- There is a
POST /api/dataset/json
endpoint, but that does not seem to accept the native
property.
- To set
X-Metabase-Session
see oauth2more/metabase.
MBQL
POST /api/dataset
Content-Type: application/json
X-Metabase-Session: <sessionId>
{
"database": 1,
"type": "query",
"query": {
"source-table": 2,
"breakout": [
[
"binning-strategy", ["field-id", 14], "default"
]
],
"aggregation": [["avg", ["field-id", 17]]]
}
}
Notes:
- To set
X-Metabase-Session
see oauth2more/metabase.
是否有采用 MBQL/SQL 查询和 returns 原始数据的元数据库 REST API?
我可以在两步过程中通过 API 执行 MBQL 查询,方法是通过 Metabase Web 应用程序 UI 创建问题然后查询问题的中间步骤,但是我还没有想出如何一步结合 MBQL 和 REST API。
有些项目我想通过在 API 请求中使用 MBQL 而不是 UI 生成的问题来完成:
- 更好的版本管理,因为可以使用代码将 MBQL 查询签入源代码管理
- 更好的隔离,因为 API 调用将不依赖于可以更改的问题
这里有一些关于如何执行两步过程的信息。
两步过程
两步过程是:
- 使用网络应用程序创建一个 MBQL/SQL 元数据库问题
- 使用 REST API 查询使用卡片在网络应用程序中创建的现有问题 API
步骤 1) 通过 Web 创建问题 UI
登录网络应用程序并单击顶部菜单中的 "New Question" 按钮。
创建问题后,您将被引导至如下所示的 URL,其中 :question-id
是一个整数。
- Web UI 端点:
GET /question/:question-id
记下这个值并在下一步的API中使用它。
Note: an alternative for creating the card is to use the
POST /api/card
API endpoint per YakovL. This can be useful in some scenarios were UI questions/cards are desirable, but I'm also trying to avoid creating creating cards / questions in the first place, since I'm not planning on using the Metabase UI to consume them. Reasons to avoid cards for me include needing to perform extra work to verify the card query definitions haven't changed but still having the SQL in the code to create the cards, and generate a lot of unneeded question cards in the UI.
步骤 2) REST API 问题数据
API 将术语 "card" 用于 Web UI "question" 对象,因此对以下 Card [=89] 进行 API 调用=]:
- API 端点:
POST /api/card/:card-id/query/:export-format
在此URL中:
:card-id
是来自网络的:question-id
UI URL:export-format
可以是json
或其他格式
有关 API 的更多信息可在 API 文档中找到:
https://github.com/metabase/metabase/blob/master/docs/api-documentation.md
问题
有没有一种方法可以直接在 API 请求中发送 MBQL/SQL 查询而不需要预先存在 Question/Card?
通过原始查询 SQL 和 MBQL 都可以通过 POST /api/dataset/
API 获得。端点的文档提到了 query
请求定义,但没有定义它。
我最后做了一些更多的研究,并在 Metabase Discourse 论坛上提问。以下示例由 sbelak.
发布原始SQL查询
我能够使用 go-metabase SDK 成功进行本机 SQL 查询以发出以下请求:
POST /api/dataset
Content-Type: application/json
X-Metabase-Session: <sessionId>
{
"database": 1,
"native": {
"query": "SELECT COUNT(*) FROM orders"
},
type: "native"
}
Notes:
- The
POST /api/dataset
does not set the responseContent-Type
header.- There is a
POST /api/dataset/json
endpoint, but that does not seem to accept thenative
property.- To set
X-Metabase-Session
see oauth2more/metabase.
MBQL
POST /api/dataset
Content-Type: application/json
X-Metabase-Session: <sessionId>
{
"database": 1,
"type": "query",
"query": {
"source-table": 2,
"breakout": [
[
"binning-strategy", ["field-id", 14], "default"
]
],
"aggregation": [["avg", ["field-id", 17]]]
}
}
Notes:
- To set
X-Metabase-Session
see oauth2more/metabase.