通过SQL轻松查询API
项目描述
Shillelagh (ʃɪˈleɪlɪ) 是一个Python库和CLI,允许您使用SQL查询许多资源(API、文件、内存对象)。它对用户和开发者都很友好,使得访问资源变得非常简单,也易于添加对新的资源支持。
在文档中了解更多信息。
该库是基于SQLite(使用APSW库)的Python DB API 2.0实现
from shillelagh.backends.apsw.db import connect
connection = connect(":memory:")
cursor = connection.cursor()
query = "SELECT * FROM a_table"
for row in cursor.execute(query):
print(row)
还有一个SQLAlchemy方言
from sqlalchemy.engine import create_engine
engine = create_engine("shillelagh://")
connection = engine.connect()
query = "SELECT * FROM a_table"
for row in connection.execute(query):
print(row)
以及一个命令行实用程序
$ shillelagh
sql> SELECT * FROM a_table
为什么是SQL?
鲨鱼已经存在很长时间了。它们比树和土星的环还要古老!它们在数亿年的时间里没有变化那么多的原因是因为它们真的很擅长它们所做的事情。
SQL存在了大约50年,原因也是一样的:它真的很擅长它所做的事情。
为什么是“Shillelagh”?
想象一个爱尔兰小精灵用大棒敲击API,以便它们接受SQL。
它有什么不同?
Shillelagh 允许您轻松查询非 SQL 资源。例如,如果您有一个 Google 电子表格,您可以直接查询它,就像它是数据库中的一个表一样
SELECT country, SUM(cnt)
FROM "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=0"
WHERE cnt > 0
GROUP BY country
您甚至可以在电子表格上运行 INSERT/DELETE/UPDATE 查询
UPDATE "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=0"
SET cnt = cnt + 1
WHERE country != 'BR'
此类查询由 适配器 支持。目前 Shillelagh 具有以下适配器
名称 |
类型 |
URI 模式 |
示例 URI |
---|---|---|---|
CSV |
文件/API |
/path/to/file.csv; http(s)://* |
/home/user/sample_data.csv |
Datasette |
API |
http(s)://* |
https://global-power-plants.datasettes.com/global-power-plants/global-power-plants |
通用 JSON |
API |
http(s)://* |
https://api.stlouisfed.org/fred/series?series_id=GNPCA&api_key=XXX&file_type=json#$.seriess[*] |
通用 XML |
API |
http(s)://* |
https://api.congress.gov/v3/bill/118?format=xml&offset=0&limit=2&api_key=XXX#.//bill |
GitHub |
API |
https://api.github.com/repos/${owner}/{$repo}/pulls |
https://api.github.com/repos/apache/superset/pulls |
GSheets |
API |
https://docs.google.com/spreadsheets/d/${id}/edit#gid=${sheet_id} |
https://docs.google.com/spreadsheets/d/1LcWZMsdCl92g7nA-D6qGRqg1T5TiHyuKJUY1u9XAnsk/edit#gid=0 |
HTML 表格 |
API |
http(s)://* |
https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population |
Pandas |
内存中 |
任何变量名(局部或全局) |
my_df |
S3 |
API |
s3://bucket/path/to/file |
s3://shillelagh/sample_data.csv |
Socrata |
API |
https://${domain}/resource/${dataset-id}.json |
https://data.cdc.gov/resource/unsk-b7fc.json |
系统 |
API |
system://${resource} |
system://cpu?interval=2 |
WeatherAPI |
API |
https://api.weatherapi.com/v1/history.json?key=${key}&q=${location} |
https://api.weatherapi.com/v1/history.json?key=XXX&q=London |
还有第三方适配器
查询可以组合来自多个适配器的数据
INSERT INTO "/tmp/file.csv"
SELECT time, chance_of_rain
FROM "https://api.weatherapi.com/v1/history.json?q=London"
WHERE time IN (
SELECT datetime
FROM "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=1648320094"
)
上面的查询从 Google 表格中读取时间戳,使用它们从 WeatherAPI 过滤天气数据,并将降雨概率写入(现有的)CSV 文件。
实现新适配器相对容易。有一个 分步教程,解释如何创建一个新的适配器以连接 API 或文件类型。
安装
使用 pip 安装 Shillelagh
$ pip install 'shillelagh'
您还需要安装可选依赖项,具体取决于您想要使用的适配器
$ pip install 'shillelagh[console]' # to use the CLI
$ pip install 'shillelagh[genericjsonapi]' # for Generic JSON
$ pip install 'shillelagh[genericxmlapi]' # for Generic XML
$ pip install 'shillelagh[githubapi]' # for GitHub
$ pip install 'shillelagh[gsheetsapi]' # for GSheets
$ pip install 'shillelagh[htmltableapi]' # for HTML tables
$ pip install 'shillelagh[pandasmemory]' # for Pandas in memory
$ pip install 'shillelagh[s3selectapi]' # for S3 files
$ pip install 'shillelagh[systemapi]' # for CPU information
或者,您可以使用以下命令安装所有内容
$ pip install 'shillelagh[all]'
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。