-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved schema_generator tool #13518
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c67acea
create package for schema_generator
dougpm caa4939
add docs
dougpm d1954a4
re-instate licence
dougpm 4a7e99b
fix docs
dougpm 9aa2843
add prerequisites
dougpm b5eec8b
add tests
dougpm df2ef0e
fix install
alafanechere fc59cd5
Merge branch 'master' into douglas/issue-11674
alafanechere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Schema Generator | ||
Util for generating catalog schema from a connector `read` command output. | ||
|
||
## Prerequisites | ||
|
||
To use this tool you first need to: | ||
|
||
- Define all your streams. | ||
- Create schema files for each stream, containing only `{}` (valid json files). See [this doc section](https://docs.airbyte.com/connector-development/cdk-python/schemas#static-schemas) for instructions on how to name these files. | ||
- Build you container docker image. | ||
|
||
Going through all the steps above should enable you to run the `read` command of your connector using the docker image, which is the input for this tool. | ||
|
||
## Usage | ||
|
||
First install the tools in it's own virtual environment: | ||
|
||
```bash | ||
$ cd tools/schema_generator # assumes you are starting from the root of the Airbyte project. | ||
$ python -m venv .venv # Create a virtual environment in the .venv directory | ||
$ source .venv/bin/activate # enable the venv | ||
$ pip install -r requirements.txt | ||
``` | ||
|
||
To use a connectors `run` command we first need an AirbyteConfiguredCatalog: | ||
|
||
```bash | ||
$ cd ../../connectors/<your-connector> # you need to use the tool at the root folder of a connector | ||
$ docker run --rm -v $(pwd)/secrets:/secrets airbyte/<your-connector-image-name>:dev discover --config /secrets/config.json | schema_generator --configure-catalog | ||
``` | ||
This will created the file **configured_catalog.json** in the **integration_tests** folder in the current working directory. | ||
|
||
Now you're all set to run the following command and generate your schemas: | ||
|
||
```bash | ||
$ docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/integration_tests:/integration_tests airbyte/<your-connector-image-name>:dev read --config /secrets/config.json --catalog /integration_tests/configured_catalog.json | schema_generator --infer-schemas | ||
``` | ||
Which will create schema files for all streams and place them in the **schemas** folder in the current working directory. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-e . |
Empty file.
22 changes: 22 additions & 0 deletions
22
tools/schema_generator/schema_generator/configure_catalog.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import json | ||
import os | ||
|
||
from airbyte_cdk.models import ConfiguredAirbyteCatalog, AirbyteMessage, ConfiguredAirbyteStream, DestinationSyncMode | ||
|
||
|
||
def configure_catalog(): | ||
record = AirbyteMessage.parse_raw(input()) | ||
for stream in record.catalog.streams: | ||
stream.json_schema = {} | ||
streams = [ | ||
ConfiguredAirbyteStream(stream=stream, sync_mode=stream.supported_sync_modes[0], destination_sync_mode=DestinationSyncMode.append) | ||
for stream in record.catalog.streams | ||
] | ||
configured_catalog = ConfiguredAirbyteCatalog(streams=streams) | ||
|
||
default_folder = os.path.join(os.getcwd(), "integration_tests") | ||
if not os.path.exists(default_folder): | ||
os.mkdir(default_folder) | ||
output_file_name = os.path.join(default_folder, "configured_catalog.json") | ||
with open(output_file_name, "w") as outfile: | ||
json.dump(json.loads(configured_catalog.json()), outfile, indent=2, sort_keys=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import argparse | ||
import sys | ||
|
||
from .infer_schemas import infer_schemas | ||
from .configure_catalog import configure_catalog | ||
|
||
|
||
def main(): | ||
|
||
parser = argparse.ArgumentParser(description="Airbyte Schema Generator") | ||
|
||
if len(sys.argv) == 1: | ||
parser.print_help() | ||
|
||
parser.add_argument("--configure-catalog", action="store_true", help="Generate a Configured Catalog") | ||
parser.add_argument("--infer-schemas", action="store_true", help="Infer Stream Schemas") | ||
|
||
args, unknown_args = parser.parse_known_args() | ||
|
||
if unknown_args: | ||
print(f"Invalid arguments: {unknown_args}.") | ||
parser.print_help() | ||
elif args.configure_catalog: | ||
configure_catalog() | ||
elif args.infer_schemas: | ||
infer_schemas() | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
from setuptools import find_packages, setup | ||
|
||
MAIN_REQUIREMENTS = ["airbyte_cdk==0.1.60", "genson==1.2.2"] | ||
|
||
TEST_REQUIREMENTS = ["pytest"] | ||
|
||
|
||
setup( | ||
version="0.1.0", | ||
name="schema_generator", | ||
description="Util to create catalog schemas for an Airbyte Connector.", | ||
author="Airbyte", | ||
author_email="contact@airbyte.io", | ||
packages=find_packages(), | ||
install_requires=MAIN_REQUIREMENTS, | ||
extras_require={ | ||
"tests": TEST_REQUIREMENTS, | ||
}, | ||
python_requires=">=3.9", | ||
entry_points={ | ||
"console_scripts": ["schema_generator = schema_generator.main:main"], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import io | ||
import sys | ||
import tempfile | ||
import os | ||
import json | ||
|
||
import pytest | ||
|
||
from schema_generator.configure_catalog import configure_catalog | ||
from schema_generator.infer_schemas import infer_schemas | ||
|
||
from airbyte_cdk.models import ( | ||
AirbyteMessage, | ||
AirbyteCatalog, | ||
AirbyteRecordMessage, | ||
AirbyteStream, | ||
ConfiguredAirbyteCatalog, | ||
ConfiguredAirbyteStream, | ||
DestinationSyncMode, | ||
SyncMode, | ||
Type, | ||
) | ||
|
||
|
||
def test_configure_catalog(): | ||
stream = AirbyteStream(name="stream", supported_sync_modes=[SyncMode.full_refresh], json_schema={}) | ||
catalog = AirbyteCatalog(streams=[stream]) | ||
catalog_message = AirbyteMessage(type=Type.CATALOG, catalog=catalog) | ||
sys.stdin = io.StringIO(catalog_message.json()) | ||
|
||
expected_configured_catalog = ConfiguredAirbyteCatalog( | ||
streams=[ConfiguredAirbyteStream(stream=stream, sync_mode=SyncMode.full_refresh, destination_sync_mode=DestinationSyncMode.append)] | ||
) | ||
|
||
expected_configured_catalog_json = json.loads(expected_configured_catalog.json()) | ||
|
||
with tempfile.TemporaryDirectory() as temp_dir: | ||
os.chdir(temp_dir) | ||
configure_catalog() | ||
assert os.path.exists("integration_tests/configured_catalog.json") | ||
|
||
with open("integration_tests/configured_catalog.json") as f: | ||
configured_catalog_json = json.loads(f.read()) | ||
assert configured_catalog_json == expected_configured_catalog_json | ||
|
||
|
||
def test_infer_schemas(): | ||
expected_schema = { | ||
"$schema": "http://json-schema.org/schema#", | ||
"properties": {"a": {"type": "integer"}, "b": {"type": "string"}}, | ||
"type": "object", | ||
} | ||
|
||
with tempfile.TemporaryDirectory() as temp_dir: | ||
os.chdir(temp_dir) | ||
record = {"a": 1, "b": "test"} | ||
record_message = AirbyteMessage(type=Type.RECORD, record=AirbyteRecordMessage(stream="stream", data=record, emitted_at=111)).json() | ||
sys.stdin = io.StringIO(record_message) | ||
infer_schemas() | ||
assert os.path.exists("schemas/stream.json") | ||
|
||
with open("schemas/stream.json") as f: | ||
schema = json.loads(f.read()) | ||
assert schema == expected_schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from airbyte_cdk.models import ( | ||
AirbyteCatalog, | ||
AirbyteConnectionStatus, | ||
AirbyteMessage, | ||
AirbyteRecordMessage, | ||
AirbyteStateMessage, | ||
AirbyteStream, | ||
ConfiguredAirbyteCatalog, | ||
ConfiguredAirbyteStream, | ||
ConnectorSpecification, | ||
DestinationSyncMode, | ||
Status, | ||
SyncMode, | ||
Type, | ||
) | ||
|
||
dummy_catalog = ConfiguredAirbyteCatalog( | ||
streams=[ | ||
ConfiguredAirbyteStream( | ||
stream=AirbyteStream(name="mystream", json_schema={"type": "object"}), | ||
sync_mode=SyncMode.full_refresh, | ||
destination_sync_mode=DestinationSyncMode.overwrite, | ||
) | ||
] | ||
) | ||
|
||
print(dummy_catalog.json()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any unit tests for the tool?