Metabase API
官网介绍:
Metabase Api
Api Documentation
1、登陆并获取会话令牌
POST /api/session/
用于登陆并获取令牌
PARAMS:
-
username
value must be a non-blank string. -
password
value must be a non-blank string. -
request
示例:
请求:
curl -X POST \-H "Content-Type: application/json" \-d '{"username": "person@metabase.com", "password": "fakepassword"}' \http://localhost:3000/api/session
返回:
{"id":"38f4939c-ad7f-4cbe-ae54-30946daf8593"}
2、一般方式的查询接口(需认证)
POST /api/card/:card-id/query
需要认证的访问配置的报表(question,这里叫card)
PARAMS:
-
card-id
配置的报表的ID,是数字 -
parameters
查询参数 -
ignore_cache
value may be nil, or if non-nil, value must be a boolean. -
dashboard_id
value may be nil, or if non-nil, value must be an integer greater than zero.
2.1、应用场景:
如果要修改前端页面,将metabase配置的报表,重新用自己的前端页面进行修改,那么可以使用这个API,通过自己写的前端,调用metabase提供的API获取报表查询数据结果(JSON)。
相对public的方式而言,这种加了权限验证,更加安全。(metabase的前端也是调用的这些接口)
2.2、具体示例(带查询条件):
由于Metabase不支持分页,是直接查出所有的。而在报表明细中通常会有使用分页进行展现。下面拿一个将分页作为查询条件的示例。
1)配报表
所以下面配置一个question(报表),将分页作为查询条件(平时)。
2)报表分享
然后将这个question进行分享,拿到UUID:8b630704-f23f-4745-9a81-f59d41240690
3)使用API进行访问
a) 调用登陆获取令牌
请求:
curl -X POST \-H "Content-Type: application/json" \-d '{"username": "xxx", "password": "xxx"}' \http://ip:3000//api/session
返回:
{"id":"38f4939c-ad7f-4cbe-ae54-30946daf8593"}
b) 调用API查询
uuid就是分享出来的UUID
parameters参数,就是带入查询条件,这里是start=0,pagesize=20
请求:
curl -X POST \-H "Content-Type: application/json" \-H "X-Metabase-Session: 38f4939c-ad7f-4cbe-ae54-30946daf8593" \-d '{ "ignore_cache": true,\"parameters": [{\"type": "category",\"target": ["variable", ["template-tag", "start"]],\"value": "0"\}, {\"type": "category",\"target": ["variable", ["template-tag", "pagesize"]],\"value": "20"\}]}' \http://ip:3000/api/card/104/query
从metabase抓包也可以拿到parameters。
返回:
{"data": {"rows": [["IC21102700003"],["IC21102700016"],["IC21102700015"],["IC21102700009"],["IC21102700014"],["21102600005"],["IC21102700005"],["IC21102700002"],["IC21102800003"],["IC21102900003"],["21102600006"],["IC21102900002"],["IC21102700006"],["IC21110500004"],["IC21110500007"],["IC21102900008"],["WG21110300003"],["IC21110500002"],["IC21102700004"],["IC21102700008"]],"cols": [{"display_name": "QUESTION_BUSI_ID","source": "native","field_ref": ["field-literal","QUESTION_BUSI_ID","type/Text"],"name": "QUESTION_BUSI_ID","base_type": "type/Text"}],"native_form": {"query": "","params": []},"results_timezone": "Asia/Shanghai","results_metadata": {"checksum": "r+TtQmdTF+/hnugKE9fV1g==","columns": [{"base_type": "type/Text","display_name": "QUESTION_BUSI_ID","name": "QUESTION_BUSI_ID","special_type": null,"fingerprint": {"global": {"distinct-count": 20,"nil%": 0.0},"type": {"type/Text": {"percent-json": 0.0,"percent-url": 0.0,"percent-email": 0.0,"percent-state": 0.0,"average-length": 15.0}}}}]},"insights": null},"database_id": 2,"started_at": "2022-04-24T11:41:22.412+08:00","json_query": {"constraints": {"max-results": 10000,"max-results-bare-rows": 2000},"type": "native","middleware": {"js-int-to-string?": true},"native": {"query": "xxx","template-tags": {"start": {"id": "c7ab1e98-2e91-2698-0114-4a87f0ae35dc","name": "start","display-name": "Start","type": "number","default": null},"pagesize": {"id": "1281cd3e-4941-f119-b0b0-8a22945b5135","name": "pagesize","display-name": "Pagesize","type": "number","default": null}}},"database": 2,"parameters": [{"type": "category","target": ["variable",["template-tag","start"]],"value": "0"},{"type": "category","target": ["variable",["template-tag","pagesize"]],"value": "20"}],"async?": true,"cache-ttl": null},"average_execution_time": null,"status": "completed","context": "question","row_count": 20,"running_time": 27
}
3、public方式的查询接口(免认证)
GET /api/public/card/:uuid/query
不需要认证的访问分享出来的报表(question,这里叫card)
PARAMS:
-
uuid
public出来的uuid -
parameters
请求参数,可以添加过滤条件
3.1、应用场景:
如果要修改前端页面,将metabase配置的报表,重新用自己的前端页面进行修改,那么可以使用这个API,通过自己写的前端,调用metabase提供的API获取报表查询数据结果(JSON)。
3.2、具体示例(带查询条件):
由于Metabase不支持分页,是直接查出所有的。而在报表明细中通常会有使用分页进行展现。下面拿一个将分页作为查询条件的示例。
1)配报表
所以下面配置一个question(报表),将分页作为查询条件。
2)报表分享
然后将这个question进行分享,拿到UUID:8b630704-f23f-4745-9a81-f59d41240690
3)使用API进行访问
1、uuid就是分享出来的UUID
2、parameters参数,就是带入查询条件,这里是start=0,pagesize=20
请求:
http://ip:3000/api/public/card/8b630704-f23f-4745-9a81-f59d41240690/query?parameters=[{"type":"category","target":["variable",["template-tag","start"]],"value":"0"},{"type":"category","target":["variable",["template-tag","pagesize"]],"value":"20"}]
响应:
{"data":{"rows":[
["IC21102700003"],
["IC21102700016"],
["IC21102700015"],
["IC21102700009"],
["IC21102700014"],
["21102600005"],
["IC21102700005"],
["IC21102700002"],
["IC21102800003"],
["IC21102900003"],
["21102600006"],
["IC21102900002"],
["IC21102700006"],
["IC21110500004"],
["IC21110500007"],
["IC21102900008"],
["WG21110300003"],
["IC21110500002"],
["IC21102700004"],
["IC21102700008"]
],
"cols":[{"display_name":"QUESTION_BUSI_ID","source":"native","field_ref":["field-literal","QUESTION_BUSI_ID","type/Text"],"name":"QUESTION_BUSI_ID","base_type":"type/Text"}],"insights":null,"results_timezone":"Asia/Shanghai"},
"json_query":{"parameters":[{"type":"category","target":["variable",["template-tag","start"]],"value":"0"},{"type":"category","target":["variable",["template-tag","pagesize"]],"value":"20"}]},"status":"completed"}