接入教程
1. 访问Superset官方API文档获取认证密钥
2. 使用API端点创建或连接数据源
3. 通过API构建和配置可视化仪表板
4. 设置仪表板访问权限和分享选项
5. 调用监控接口跟踪使用数据
使用Python获取仪表板列表
import requests
base_url = 'https://api.example.com'
api_key = 'YOUR_API_KEY'
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
# 获取所有仪表板
response = requests.get(f'{base_url}/api/v1/dashboard/', headers=headers)
if response.status_code == 200:
dashboards = response.json()
print(f'Found {len(dashboards["result"])} dashboards')
else:
print(f'Error: {response.status_code}')
print(response.text)
使用PHP创建新数据源
<?php
$baseUrl = 'https://api.example.com';
$apiKey = 'YOUR_API_KEY';
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
];
// 创建新数据源的示例数据
$data = [
'database_name' => 'Example Database',
'sqlalchemy_uri' => 'postgresql://user:pass@host:port/dbname',
'extra' => '{"schemas_allowed_for_csv_upload": ["public"]}'
];
$ch = curl_init($baseUrl . '/api/v1/database/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode === 201) {
echo 'Data source created successfully.';
} else {
echo 'Error: ' . $httpCode;
echo $response;
}
curl_close($ch);
?>
使用JavaScript更新图表配置
const baseUrl = 'https://api.example.com';
const apiKey = 'YOUR_API_KEY';
async function updateChart(chartId, newConfig) {
const url = `${baseUrl}/api/v1/chart/${chartId}`;
const response = await fetch(url, {
method: 'PUT',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(newConfig)
});
if (response.ok) {
const data = await response.json();
console.log('Chart updated:', data.result);
return data;
} else {
console.error('Update failed:', response.status);
throw new Error('Chart update failed');
}
}
// 使用示例
const updatedConfig = {
params: {
metric: 'revenue',
groupby: ['region', 'product'],
time_range: 'Last 30 days'
}
};
// updateChart(123, updatedConfig);
常见问题
如何获取API认证密钥?
请登录到您的Superset实例,在个人设置或安全管理区域中生成API密钥。具体位置可能因Superset版本而异,通常位于“安全”>“API密钥”菜单下。
API请求频率是否有限制?
是的,Superset API通常会有请求频率限制以防止滥用。具体限制取决于您的实例配置,常见设置为每分钟60-100次请求。建议在代码中添加适当的延迟和处理429状态码(请求过多)。
API支持哪些数据源类型?
Superset API支持所有Superset平台兼容的数据源,包括但不限于PostgreSQL、MySQL、SQLite、BigQuery、Snowflake、Redshift等。通过API创建数据源时,需要提供正确的SQLAlchemy连接URI和必要的认证信息。
Aitishiku.com