Airbyte CDK(已弃用)
注意:AirbyteCDKLoader 已弃用。请改用 AirbyteLoader。
Airbyte is a data integration platform for ELT pipelines from APIs, databases & files to warehouses & lakes. It has the largest catalog of ELT connectors to data warehouses and databases.
许多源连接器是使用 Airbyte CDK 实现的。此加载器允许运行这些连接器中的任意一个,并将数据作为文档返回。
安装
首先,你需要安装 airbyte-cdk Python 包。
%pip install --upgrade --quiet airbyte-cdk
然后,您可以从 Airbyte Github 仓库 安装现有的连接器,或使用 Airbyte CDK 创建您自己的连接器。
例如,要安装 GitHub 连接器,请运行
%pip install --upgrade --quiet "source_github@git+https://github.com/airbytehq/airbyte.git@master#subdirectory=airbyte-integrations/connectors/source-github"
部分源码也以常规包的形式发布在 PyPI 上。
示例
现在,您可以基于导入的源创建一个AirbyteCDKLoader。它需要一个传递给连接器的config对象。您还必须按名称选择要从中检索记录的流(stream_name)。请查看连接器文档页面和规范定义,以获取有关配置对象和可用流的更多信息。对于 Github 连接器,这些是:
- https://github.com/airbytehq/airbyte/blob/master/airbyte-integrations/connectors/source-github/source_github/spec.json.
- https://docs.airbyte.com/integrations/sources/github/
from langchain_community.document_loaders.airbyte import AirbyteCDKLoader
from source_github.source import SourceGithub # plug in your own source here
config = {
# your github configuration
"credentials": {"api_url": "api.github.com", "personal_access_token": "<token>"},
"repository": "<repo>",
"start_date": "<date from which to start retrieving records from in ISO format, e.g. 2020-10-20T00:00:00Z>",
}
issues_loader = AirbyteCDKLoader(
source_class=SourceGithub, config=config, stream_name="issues"
)
现在您可以按常规方式加载文档
docs = issues_loader.load()
由于 load 返回一个列表,它会阻塞直到所有文档加载完成。为了更好地控制此过程,您也可以使用 lazy_load 方法,该方法返回一个迭代器:
docs_iterator = issues_loader.lazy_load()
请注意,默认情况下页面内容为空,而元数据对象包含记录中的所有信息。若要以不同方式创建文档,请在创建加载器时传入 record_handler 函数:
from langchain_core.documents import Document
def handle_record(record, id):
return Document(
page_content=record.data["title"] + "\n" + (record.data["body"] or ""),
metadata=record.data,
)
issues_loader = AirbyteCDKLoader(
source_class=SourceGithub,
config=config,
stream_name="issues",
record_handler=handle_record,
)
docs = issues_loader.load()
增量加载
某些流支持增量加载,这意味着源会跟踪已同步的记录,不会再次加载它们。这对于数据量大且频繁更新的源非常有用。
为了利用这一点,请存储加载器的 last_state 属性,并在重新创建加载器时传入该属性。这将确保仅加载新记录。
last_state = issues_loader.last_state # store safely
incremental_issue_loader = AirbyteCDKLoader(
source_class=SourceGithub, config=config, stream_name="issues", state=last_state
)
new_docs = incremental_issue_loader.load()