gpt4 book ai didi

sql - 是否可以通过 REST API 从 Metabase MBQL/SQL 查询中获取原始数据?

转载 作者:行者123 更新时间:2023-12-04 21:31:15 30 4
gpt4 key购买 nike

是否有接受 MBQL/SQL 查询并返回原始数据的 Metabase REST API?

我可以通过 API 在两步过程中执行 MBQL 查询,方法是执行通过 Metabase Web 应用程序 UI 创建问题的中间步骤,然后查询问题,但我还没有想出如何将 MBQL 与 REST 结合起来API 一步到位。

我想通过在 API 请求中使用 MBQL 而不是 UI 生成的问题来完成一些项目:

  • 更好的版本管理,因为可以使用代码
  • 将 MBQL 查询 checkin 源代码控制
  • 更好的隔离,因为 API 调用不会依赖于可以改变的问题

  • 以下是有关如何执行两步过程的一些信息。

    两步过程

    两步过程是:
  • 使用 Web 应用程序创建 MBQL/SQL 元数据库 问题
  • 使用 REST API 查询使用 Card API
  • 在 Web 应用程序中创建的现有问题

    步骤 1) 通过 Web UI 创建问题

    登录网络应用程序,然后单击顶部菜单中的“新问题”按钮。

    创建您的问题后,您将被定向到如下所示的 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 使用术语“卡片”来表示 Web UI “问题”对象,因此对以下卡片 API 进行 API 调用:
  • API 端点:POST /api/card/:card-id/query/:export-format

  • 在这个网址中:
  • :card-id:question-id来自 Web UI URL
  • :export-format可以是json或其他格式

  • API 文档中提供了有关 API 的更多信息:

    https://github.com/metabase/metabase/blob/master/docs/api-documentation.md

    问题

    有没有办法通过在 API 请求中一步发送 MBQL/SQL 查询来直接执行此操作而无需预先存在的问题/卡片?

    最佳答案

    通过 POST /api/dataset/ 可以通过原始 SQL 和 MBQL 进行查询。 API。端点的文档提到了 query请求定义但没有定义它。

    我最终做了一些更多的研究并在 Metabase Discourse 论坛上提问。以下示例由 sbelak 发布.

    原始 SQL 查询

    我能够使用 go-metabase 成功地进行原生 SQL 查询。 SDK 发出以下请求:

    POST /api/dataset
    Content-Type: application/json
    X-Metabase-Session: <sessionId>

    {
    "database": 1,
    "native": {
    "query": "SELECT COUNT(*) FROM orders"
    },
    type: "native"
    }

    Notes:

    1. The POST /api/dataset does not set the response Content-Type header.
    2. There is a POST /api/dataset/json endpoint, but that does not seem to accept the native property.
    3. 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:

    1. To set X-Metabase-Session see oauth2more/metabase.

    关于sql - 是否可以通过 REST API 从 Metabase MBQL/SQL 查询中获取原始数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50620095/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com