18 个版本 (稳定)
| 2.0.0 | 2024年2月8日 | 
|---|---|
| 1.10.0 | 2021年9月13日 | 
| 1.9.0 | 2021年3月30日 | 
| 1.8.1 | 2020年10月16日 | 
| 0.5.0 | 2019年5月3日 | 
#110 in 数据库接口
170KB
 4K  SLoC
jsonschema-transpiler
一个将 JSON Schema 转换为 Avro 和 BigQuery 架构的工具。
JSON Schema 主要用于验证传入数据,但包含足够的信息来描述数据结构。转换器将架构编码以用于数据序列化和处理框架。主要用例是通过 Avro 中间件将 JSON 文档引入 BigQuery。
此工具可以处理现代支持 SQL 接口的数据处理工具中常见的许多复合类型,例如列表、结构、键值映射和类型变体。
此工具旨在从 Firefox 数据平台中 JSON 架构的权威来源 mozilla-pipeline-schemas 生成新的架构。
安装
cargo install jsonschema-transpiler
用法
A tool to transpile JSON Schema into schemas for data processing
Usage: jsonschema-transpiler [OPTIONS] [FILE]
Arguments:
  [FILE]
          Sets the input file to use
Options:
  -t, --type <TYPE>
          The output schema format
          [default: avro]
          Possible values:
          - avro:     Avro format
          - bigquery: BigQuery format
  -r, --resolve <RESOLVE>
          The resolution strategy for incompatible or under-specified schema
          [default: cast]
          Possible values:
          - cast:  Cast incompatible/under-specified schemas
          - panic: Panic on incompatible/under-specified schemas
          - drop:  Drop incompatible/under-specified schemas
  -c, --normalize-case
          snake_case column-names for consistent behavior between SQL engines
  -n, --force-nullable
          Treats all columns as NULLABLE, ignoring the required section in the JSON Schema object
      --tuple-struct
          Treats tuple validation as an anonymous struct
  -w, --allow-maps-without-value
          Produces maps without a value field for incompatible or under-specified value schema
  -h, --help
          Print help (see a summary with '-h')
  -V, --version
          Print version
JSON 架构可以从 stdin 或文件中读取。
示例用法
# An object with a single, optional boolean field
$ schema='{"type": "object", "properties": {"foo": {"type": "boolean"}}}'
$ echo $schema | jq
{
  "type": "object",
  "properties": {
    "foo": {
      "type": "boolean"
    }
  }
}
$ echo $schema | jsonschema-transpiler --type avro
{
  "fields": [
    {
      "default": null,
      "name": "foo",
      "type": [
        {
          "type": "null"
        },
        {
          "type": "boolean"
        }
      ]
    }
  ],
  "name": "root",
  "type": "record"
}
$ echo $schema | jsonschema-transpiler --type bigquery
[
  {
    "mode": "NULLABLE",
    "name": "foo",
    "type": "BOOL"
  }
]
构建
要构建和测试软件包
cargo build
cargo test
旧版本的软件包(<= 1.9)依赖于 oniguruma 执行蛇形命名逻辑。要启用此模块的使用,请添加功能标志
cargo test --features oniguruma
贡献
欢迎贡献。API 可能会显著更改,但各种源格式之间的转换应保持一致。为帮助开发转换器,测试用例是在 tests/resources 下从与语言无关的格式生成的。
{
    "name": "test-suite",
    "tests": [
        {
            "name": "test-case",
            "description": [
                "A short description of the test case."
            ],
            "tests": {
                "avro": {...},
                "bigquery": {...},
                "json": {...}
            }
        },
        ...
    ]
}
模式提供数据结构的一种类型系统。大多数模式语言支持一组相似的原始类型。存在原子数据类型,如布尔值、整数和浮点数。这些原子数据类型可以形成复合结构单元,例如对象、数组和映射。值的缺失通常用null类型表示。存在类型修饰符,如两种类型的联合。
以下模式目前得到支持
- JSON Schema
- Avro
- BigQuery
将来,可能支持来自类似系统(如Parquet和Spark)的模式,或者支持各种交互式数据语言(IDL)(如Avro IDL)。
发布
jsonschema-transpiler通过Cargo作为crate分发。按照以下清单部署到crates.io。
- 按照语义版本控制,在Cargo.toml中提高版本号。
- 确保cargo test和CI测试成功。
- 运行cargo publish。由于问题#59,必须使用--no-verify标志运行。
- 在GitHub中草拟一个与版本升级相对应的新版本。
依赖
~4.5–7MB
~132K SLoC