-
Notifications
You must be signed in to change notification settings - Fork 32
feat(cdk): add CombinedExtractor #551
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
949872b
Add Combined Extractor
lazebnyi 4ee3fb5
Auto-fix lint and format issues
79f3f1a
Update models
lazebnyi 9c5da1d
Rollback not related changes
lazebnyi 91880f7
Remove un related to issue changes
lazebnyi f79bb74
Auto-fix lint and format issues
49f4900
Remove unsued ref
lazebnyi c452c67
Auto-fix lint and format issues
9010f19
Merge branch 'main' into lazebnyi/add-combined-extractor
lazebnyi ed166ab
Add unittest for test_combined_extractor
lazebnyi 39369ce
Merge branch 'lazebnyi/add-combined-extractor' of github.com:airbyteh…
lazebnyi 87d6bf1
Auto-fix lint and format issues
cf8acbe
Update unittest
lazebnyi b5bfbd5
Add dummy key to unittest
lazebnyi 94add38
Fix unittest
lazebnyi 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 hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
airbyte_cdk/sources/declarative/extractors/combined_extractor.py
This file contains hidden or 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,45 @@ | ||
| # | ||
| # Copyright (c) 2025 Airbyte, Inc., all rights reserved. | ||
| # | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any, Iterable, List, MutableMapping | ||
|
|
||
| import requests | ||
|
|
||
| from airbyte_cdk.sources.declarative.extractors.record_extractor import RecordExtractor | ||
|
|
||
|
|
||
| @dataclass | ||
| class CombinedExtractor(RecordExtractor): | ||
| """ | ||
| Extractor that merges the output of multiple sub-extractors into a single record. | ||
|
|
||
| This extractor takes a list of `RecordExtractor` instances (`extractors`), each of which | ||
| independently extracts records from the response. For each response, the extractor: | ||
|
|
||
| 1. Invokes each sub-extractor to generate iterables of records. | ||
| 2. Zips the results together, so that the first record from each extractor is combined, | ||
| the second from each, and so on. | ||
| 3. Merges each group of records into a single dictionary using `dict.update()`. | ||
|
|
||
| The result is a sequence of dictionaries where each dictionary contains the merged keys | ||
| and values from the corresponding records across all extractors. | ||
|
|
||
| Example: | ||
| keys_extractor -> yields: [{"name": "Alice", "age": 30}] | ||
| extra_data_extractor -> yields: [{"country": "US"}] | ||
| CombinedExtractor(extractors=[keys_extractor, extra_data_extractor]) -> | ||
| yields: [{"name": "Alice", "age": 30, "country": "US"}] | ||
| """ | ||
|
|
||
| extractors: List[RecordExtractor] | ||
|
|
||
| def extract_records(self, response: requests.Response) -> Iterable[MutableMapping[Any, Any]]: | ||
| extractors_records = [extractor.extract_records(response) for extractor in self.extractors] | ||
|
|
||
| for records in zip(*extractors_records): | ||
| merged = {} | ||
| for record in records: | ||
| merged.update(record) # merge all fields | ||
| yield merged |
42 changes: 42 additions & 0 deletions
42
airbyte_cdk/sources/declarative/extractors/key_value_extractor.py
This file contains hidden or 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,42 @@ | ||
| # | ||
| # Copyright (c) 2025 Airbyte, Inc., all rights reserved. | ||
| # | ||
|
|
||
| from dataclasses import dataclass | ||
| from itertools import islice | ||
| from typing import Any, Iterable, MutableMapping | ||
|
|
||
| import requests | ||
|
|
||
| from airbyte_cdk.sources.declarative.extractors.record_extractor import RecordExtractor | ||
|
|
||
|
|
||
| @dataclass | ||
| class KeyValueExtractor(RecordExtractor): | ||
| """ | ||
| Extractor that combines keys and values from two separate extractors. | ||
| The `keys_extractor` and `values_extractor` extract records independently | ||
| from the response. Their outputs are zipped together to form key-value mappings. | ||
| Each key from `keys_extractor` should correspond to a key in the resulting dictionary, | ||
| and each value from `values_extractor` is the value for that key. | ||
| Example: | ||
| keys_extractor -> yields: ["name", "age"] | ||
| values_extractor -> yields: ["Alice", 30] | ||
| result: { "name": "Alice", "age": 30 } | ||
| """ | ||
|
|
||
| keys_extractor: RecordExtractor | ||
| values_extractor: RecordExtractor | ||
|
|
||
| def extract_records(self, response: requests.Response) -> Iterable[MutableMapping[Any, Any]]: | ||
| keys = list(self.keys_extractor.extract_records(response)) | ||
| values = self.values_extractor.extract_records(response) | ||
|
|
||
| while True: | ||
| chunk = list(islice(values, len(keys))) | ||
| if not chunk: | ||
| break | ||
| yield dict(zip(keys, chunk)) | ||
lazebnyi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.