从表格数据推断SQL DDL语句
项目描述
关于
您可以使用Skeem从表格数据推断SQL DDL语句。
Skeem基于出色的ddlgenerator、frictionless、fsspec、pandas、SQLAlchemy和xarray包,既可以作为独立程序使用,也可以作为库使用。
支持的输入数据
NDJSON(以前称为LDJSON)即JSON Lines,另请参阅JSON流
支持的输入来源
请注意,Skeem是alpha质量的软件,仍在开发中。欢迎各种贡献,以使其更加稳固。在1.0版本发布之前,应该预料到会有破坏性的更改,因此建议进行版本锁定,尤其是在将其作为库使用时。
摘要
skeem infer-ddl --dialect=postgresql data.ndjson
CREATE TABLE "data" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"date" TIMESTAMP WITHOUT TIME ZONE,
"fruits" TEXT NOT NULL,
"price" DECIMAL(2, 2) NOT NULL,
PRIMARY KEY ("id")
);
快速入门
如果您很着急,并且想在不进行安装的情况下运行Skeem,只需使用Podman或Docker上的OCI镜像即可。
docker run --rm ghcr.io/daq-tools/skeem-standard \
skeem infer-ddl --dialect=postgresql \
https://github.com/daq-tools/skeem/raw/main/tests/testdata/basic.ndjson
设置
从PyPI安装Skeem。
pip install skeem
安装支持NetCDF等额外数据格式的Skeem。
pip install 'skeem[scientific]'
用法
本节概述了一些Skeem的示例调用,包括命令行和库使用。除了网络上的资源外,测试数据还可以从存储库的testdata文件夹中获取。
命令行使用
帮助
skeem info
skeem --help
skeem infer-ddl --help
从文件读取
# NDJSON, Parquet, and InfluxDB line protocol (ILP) formats.
skeem infer-ddl --dialect=postgresql data.ndjson
skeem infer-ddl --dialect=postgresql data.parquet
skeem infer-ddl --dialect=postgresql data.lp
# CSV, JSON, ODS, and XLSX formats.
skeem infer-ddl --dialect=postgresql data.csv
skeem infer-ddl --dialect=postgresql data.json
skeem infer-ddl --dialect=postgresql data.ods
skeem infer-ddl --dialect=postgresql data.xlsx
skeem infer-ddl --dialect=postgresql data.xlsx --address="Sheet2"
从URL读取
# CSV, NDJSON, XLSX
skeem infer-ddl --dialect=postgresql https://github.com/daq-tools/skeem/raw/main/tests/testdata/basic.csv
skeem infer-ddl --dialect=postgresql https://github.com/daq-tools/skeem/raw/main/tests/testdata/basic.ndjson
skeem infer-ddl --dialect=postgresql https://github.com/daq-tools/skeem/raw/main/tests/testdata/basic.xlsx --address="Sheet2"
# Google Sheets: Address first sheet, and specific sheet of workbook.
skeem infer-ddl --dialect=postgresql --table-name=foo https://docs.google.com/spreadsheets/d/1ExyrawjlyksbC6DOM6nLolJDbU8qiRrrhxSuxf5ScB0/view
skeem infer-ddl --dialect=postgresql --table-name=foo https://docs.google.com/spreadsheets/d/1ExyrawjlyksbC6DOM6nLolJDbU8qiRrrhxSuxf5ScB0/view#gid=883324548
# InfluxDB line protocol (ILP)
skeem infer-ddl --dialect=postgresql https://github.com/influxdata/influxdb2-sample-data/raw/master/air-sensor-data/air-sensor-data.lp
# Compressed files in gzip format
skeem --verbose infer-ddl --dialect=crate --content-type=ndjson https://s3.amazonaws.com/crate.sampledata/nyc.yellowcab/yc.2019.07.gz
# CSV on S3
skeem --verbose infer-ddl --dialect=postgresql s3://noaa-ghcn-pds/csv/by_year/2022.csv
# CSV on Google Cloud Storage
skeem --verbose infer-ddl --dialect=postgresql gs://tinybird-assets/datasets/nations.csv
skeem --verbose infer-ddl --dialect=postgresql gs://tinybird-assets/datasets/medals1.csv
# CSV on GitHub
skeem --verbose infer-ddl --dialect=postgresql github://daq-tools:skeem@/tests/testdata/basic.csv
# GRIB2, NetCDF
skeem infer-ddl --dialect=postgresql https://github.com/earthobservations/testdata/raw/main/opendata.dwd.de/weather/nwp/icon/grib/18/t/icon-global_regular-lat-lon_air-temperature_level-90.grib2
skeem infer-ddl --dialect=postgresql https://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3-example.nc
skeem infer-ddl --dialect=postgresql https://www.unidata.ucar.edu/software/netcdf/examples/WMI_Lear.nc
OCI
OCI镜像可在GitHub容器注册库(GHCR)上找到。为了在Podman或Docker上运行它们,请调用
docker run --rm ghcr.io/daq-tools/skeem-standard \
skeem infer-ddl --dialect=postgresql \
https://github.com/daq-tools/skeem/raw/main/tests/testdata/basic.csv
如果您想处理文件系统上的文件,您需要在运行时将工作目录挂载到容器中,如下所示
docker run --rm --volume=$(pwd):/data ghcr.io/daq-tools/skeem-standard \
skeem infer-ddl --dialect=postgresql /data/basic.csv
为了始终运行最新开发版本,并为此创建快捷方式,本节概述了如何使用别名skeem和变量来存储URL,这可能有助于节省一些击键。
alias skeem="docker run --rm --pull=always ghcr.io/daq-tools/skeem-standard:nightly skeem"
URL=https://github.com/daq-tools/skeem/raw/main/tests/testdata/basic.ndjson
skeem infer-ddl --dialect=postgresql $URL
更多
使用不同的后端(默认:ddlgen)
skeem infer-ddl --dialect=postgresql --backend=frictionless data.ndjson
从标准输入读取数据需要分别获取表名和内容类型
skeem infer-ddl --dialect=crate --table-name=foo --content-type=ndjson - < data.ndjson skeem infer-ddl --dialect=crate --table-name=foo --content-type=json - < data.json skeem infer-ddl --dialect=crate --table-name=foo --content-type=csv - < data.csv
从标准输入读取数据也如此操作,如果您喜欢使用管道
cat data.ndjson | skeem infer-ddl --dialect=crate --table-name=foo --content-type=ndjson - cat data.json | skeem infer-ddl --dialect=crate --table-name=foo --content-type=json - cat data.csv | skeem infer-ddl --dialect=crate --table-name=foo --content-type=csv -
库使用
import io
from skeem.core import SchemaGenerator
from skeem.model import Resource, SqlTarget
INDATA = io.StringIO(
"""
{"id":1,"name":"foo","date":"2014-10-31 09:22:56","fruits":"apple,banana","price":0.42}
{"id":2,"name":"bar","date":null,"fruits":"pear","price":0.84}
"""
)
sg = SchemaGenerator(
resource=Resource(data=INDATA, content_type="ndjson"),
target=SqlTarget(dialect="crate", table_name="testdrive"),
)
print(sg.to_sql_ddl().pretty)
CREATE TABLE "testdrive" (
"id" INT NOT NULL,
"name" STRING NOT NULL,
"date" TIMESTAMP,
"fruits" STRING NOT NULL,
"price" DOUBLE NOT NULL,
PRIMARY KEY ("id")
);
开发
要从源安装项目,请遵循 开发 文档。
项目信息
致谢
所有其他无数的贡献者和优秀 Python 包的作者,Python 本身,以及所有向下到海龟的一切。
现有技术
我们维护了一个 其他项目列表,这些项目与 Skeem 有相同或类似的目标。
词源
程序原本打算命名为 Eskema,但后来发现已经有另一个 Eskema 存在。因此,它被重命名为 Skeem,这是爱沙尼亚语,意为“模式”、“大纲”或“计划”。
项目详情
下载文件
下载适用于您的平台文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源分布
构建分布
skeem-0.1.0.tar.gz 的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | afeae0f679c0e5a39dfa7f5de81b207dfacc36e85e954e4103d040280995b16d |
|
MD5 | ce66910d64fb95338c16acabcdc21cf8 |
|
BLAKE2b-256 | 6ac64b09e6fbecb92f2c2a4613fb922fbc12d091d27a24c88f893d9d001a3219 |
skeem-0.1.0-py3-none-any.whl 的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | e78a09a9f401b7315f87bb972b568229911f4d27cd998138f567222e33846820 |
|
MD5 | 33dbdcd105b9f8245588058592467912 |
|
BLAKE2b-256 | adc588a91532ff1b4370bc76371447914ec22e1d17fc63c7a1421f4889b5ca67 |