lakeFS PullsApi 完全指南:用 Python SDK 管理数据湖 Pull Request

发布时间:2026/10/13 3:03:29
lakeFS PullsApi 完全指南:用 Python SDK 管理数据湖 Pull Request 数据工程数据湖大数据对象存储后端【免费下载链接】lakeFSlakeFS - Data version control for your data lake | Git for data项目地址https://gitcode.com/gh_mirrors/la/lakeFS点击查看免费下载本指南以 lakeFS Python SDKlakefs_sdk的 PullsApi 为纲系统讲解 pull request 数据版本控制的创建、查询、列表、更新与合并五个 API 端点并深入服务端 Go 实现帮助你掌握在数据湖上用代码驱动分支评审-合并工作流的完整方法。读完本文你将能用 Python 编写一套可复用的 PR 自动化脚本并理解其底层权限、校验与 KV 存储机制。PullsApi 概览PullsApi 是 lakeFS Python SDK 中负责 pull requestPR资源的 API 类所有请求路径均以/api/v1为前缀围绕仓库下的/pulls资源展开。其生成的源码位于 clients/python/lakefs_sdk/api/pulls_api.py对应的 OpenAPI 定义在 api/swagger.yml该接口在规范中被标记为experimental。五个方法及其 HTTP 映射如下方法HTTP 请求说明create_pull_requestPOST/repositories/{repository}/pulls创建 pull requestget_pull_requestGET/repositories/{repository}/pulls/{pull_request}获取 pull request 详情list_pull_requestsGET/repositories/{repository}/pulls分页列出 pull requestsmerge_pull_requestPUT/repositories/{repository}/pulls/{pull_request}/merge合并 pull requestupdate_pull_requestPATCH/repositories/{repository}/pulls/{pull_request}更新 pull request每个方法都成对提供xxx_with_http_info变体返回(响应体, 状态码, 响应头)三元组便于需要原始 HTTP 细节的场景使用。客户端初始化与认证在使用 PullsApi 之前需要构造lakefs_sdk.Configuration与lakefs_sdk.ApiClient。SDK 支持三种认证方式HTTP 基本认证basic_auth、API Key 认证cookie_auth与 BearerJWT认证jwt_token三者可任选其一示例来自文档中的标准初始化模式clients/python/README.md 有完整说明。import time import os import lakefs_sdk from lakefs_sdk.models.pull_request_creation import PullRequestCreation from lakefs_sdk.models.pull_request_creation_response import PullRequestCreationResponse from lakefs_sdk.rest import ApiException from pprint import pprint # 方式一定义 host可选默认 /api/v1 configuration lakefs_sdk.Configuration( host /api/v1 ) # 方式二HTTP 基本认证 configuration lakefs_sdk.Configuration( username os.environ[USERNAME], password os.environ[PASSWORD] ) # 方式三API Key 认证cookie_auth可按需设置前缀 configuration.api_key[cookie_auth] os.environ[API_KEY] # configuration.api_key_prefix[cookie_auth] Bearer # 方式四BearerJWT认证 configuration lakefs_sdk.Configuration( access_token os.environ[BEARER_TOKEN] ) # 进入 ApiClient 上下文创建 API 实例 with lakefs_sdk.ApiClient(configuration) as api_client: api_instance lakefs_sdk.PullsApi(api_client) # ... 在此调用 PullsApi 的各个方法注意basic_auth、cookie_auth、jwt_token三种授权方式的详细说明见 clients/python/README.md。服务端按basic_auth、cookie_auth、jwt_token顺序尝试认证见 pulls_api.py 的_auth_settings。创建 Pull Requestcreate_pull_request(repository, pull_request_creation)用于在指定仓库中创建一条 PR核心入参为PullRequestCreation模型。请求模型 PullRequestCreation字段类型必填说明titlestr是PR 标题descriptionstr否PR 描述source_branchstr是源分支被合并方destination_branchstr是目标分支合并接收方该模型在 SDK 中由 pull_request_creation.py 定义其 OpenAPI schema 见 swagger.yml。服务端在 pkg/catalog/catalog.go 中会校验仓库 ID 与源/目标分支 ID 合法、源目标分支不能相同否则返回ErrSameBranch、且两个分支都必须真实存在。创建示例with lakefs_sdk.ApiClient(configuration) as api_client: api_instance lakefs_sdk.PullsApi(api_client) repository my-repo # str | 仓库名 pull_request_creation lakefs_sdk.PullRequestCreation( titleAdd new data pipeline, descriptionMerge feature branch into main, source_branchfeature/pipeline, destination_branchmain, ) try: # create pull request api_response api_instance.create_pull_request(repository, pull_request_creation) print(The response of PullsApi-create_pull_request:\n) pprint(api_response) # PullRequestCreationResponse(id...) except Exception as e: print(Exception when calling PullsApi-create_pull_request: %s\n % e)返回与响应返回类型PullRequestCreationResponse仅含一个id字段PR 的 ID后续查询、更新、合并都要用到。请求头Content-Type: application/jsonAccept: application/json。状态码状态码说明201pull request created创建成功返回PullRequestCreationResponse400Validation Error401Unauthorized403Forbidden404Resource Not Found409Resource Conflicts With Target429too many requests0Internal Server Error服务端在 pkg/api/controller.go 中实现CreatePullRequest先校验调用者具备pr:WritePullRequest权限见 pkg/permissions/actions.go 的权限常量定义再从当前登录用户推导authoruser.Committer()最终由catalog.CreatePullRequest生成 PR ID 并落库。查询 Pull Request 详情get_pull_request(repository, pull_request)按 ID 获取单个 PR 的完整信息。with lakefs_sdk.ApiClient(configuration) as api_client: api_instance lakefs_sdk.PullsApi(api_client) repository my-repo # str | 仓库名 pull_request pr-id-from-create-response # str | pull request id try: # get pull request api_response api_instance.get_pull_request(repository, pull_request) print(The response of PullsApi-get_pull_request:\n) pprint(api_response) # PullRequest 对象 except Exception as e: print(Exception when calling PullsApi-get_pull_request: %s\n % e)返回模型 PullRequest查询返回 PullRequest它继承PullRequestBasicstatus、title、description并增加以下字段字段类型必填说明idstr是PR IDcreation_datedatetime是创建时间authorstr是创建者source_branchstr是源分支destination_branchstr是目标分支merged_commit_idstr否已合并 PR 对应的合并 commit id仅合并后存在closed_datedatetime否关闭时间仅 closed/merged 状态存在status字段是枚举类型取值仅限open、closed、merged三种由 pull_request.py 中的 pydantic validator 强制校验。OpenAPI 定义见 swagger.yml。请求头Accept: application/json无请求体Content-Type 未定义。状态码200返回PullRequest、400、401、404、429、0。底层实现服务端 GetPullRequest 需要pr:ReadPullRequest权限返回时将 graveler 层的PullRequest定义于 pkg/graveler/graveler.go转换为 API 模型并补全MergedCommitId与ClosedDate。分页列出 Pull Requestlist_pull_requests(repository, prefix..., after..., amount..., status...)以游标分页方式列出仓库下符合条件的 PR返回PullRequestsList包含pagination与results数组模型见 pull_requests_list.py分页结构见 pagination.py。with lakefs_sdk.ApiClient(configuration) as api_client: api_instance lakefs_sdk.PullsApi(api_client) repository my-repo # str | 仓库名 prefix prefix_example # str | 返回以该值作为前缀的条目可选 after after_example # str | 返回该值之后的条目可选 amount 100 # int | 每页返回多少条可选默认 100 status all # str | 按状态过滤可选默认 all try: # list pull requests api_response api_instance.list_pull_requests(repository, prefixprefix, afterafter, amountamount, statusstatus) print(The response of PullsApi-list_pull_requests:\n) pprint(api_response) except Exception as e: print(Exception when calling PullsApi-list_pull_requests: %s\n % e)查询参数名称类型必填默认值说明repositorystr是-仓库名prefixstr否-仅返回以该值开头的条目afterstr否-仅返回排序位置在该值之后的条目游标amountint否100每页返回数量statusstr否all按状态过滤关于amount与status有两个来自源码的重要细节amount 的取值范围SDK 生成代码将其声明为conint(strictTrue, le1000, ge-1)见 pulls_api.py即合法区间为-1到1000。服务端 pkg/catalog/catalog.go 定义ListPullsLimitMax 1000越界或为负时会归一化为 1000。status 的过滤语义OpenAPI 定义中status枚举为[open, closed, all]默认allswagger.yml。服务端 shouldSkipByStatus 的语义是closed同时包含CLOSED与MERGED两类open仅匹配OPEN其余值如merged则返回全部。请求头Accept: application/json无请求体。状态码200返回 PR 列表、400、401、404、429、0。底层实现服务端 ListPullRequests 需要pr:ListPullRequests权限其分页游标基于 PR ID 的字典序配合prefix实现前缀过滤翻页时用返回的pagination.next_offset作为下一次调用的after。同时注意列表接口出于权限最小化考虑不返回 description 字段见 controller.go 的注释说明完整描述需调用get_pull_request。更新 Pull Requestupdate_pull_request(repository, pull_request, pull_request_basic)通过PATCH方法局部更新 PR 的标题、描述或状态。请求体为 PullRequestBasic三个字段均为可选语义为提供哪个字段就更新哪个字段字段类型说明statusstropen/closed/merged之一可省略titlestr新标题可省略descriptionstr新描述可省略with lakefs_sdk.ApiClient(configuration) as api_client: api_instance lakefs_sdk.PullsApi(api_client) repository my-repo # str | 仓库名 pull_request pr-id # str | pull request id pull_request_basic lakefs_sdk.PullRequestBasic( statusclosed, descriptionUpdate: resolved by discussion, ) try: # update pull request api_instance.update_pull_request(repository, pull_request, pull_request_basic) except Exception as e: print(Exception when calling PullsApi-update_pull_request: %s\n % e)返回类型void无响应体。请求头Content-Type: application/jsonAccept: application/json。状态码200更新成功、400、401、403、404、429、0。底层实现服务端 UpdatePullRequest 需要pr:WritePullRequest权限。graveler 层的 UpdatePullRequest 采用读取-修改-写回模式只对非 nil 字段做变更当status变为closed或merged即isPullClosed见 graveler.go时会自动写入ClosedDate为当前时间。最终通过 ref/manager.go 的UpdatePullRequest以 KV 条件更新SetMsgIf完成持久化避免并发覆盖。合并 Pull Requestmerge_pull_request(repository, pull_request)是 PR 工作流的收尾操作通过PUT触发一次真实的合并把 PR 的源分支合并到目标分支。with lakefs_sdk.ApiClient(configuration) as api_client: api_instance lakefs_sdk.PullsApi(api_client) repository my-repo # str | 仓库名 pull_request pr-id # str | pull request id try: # merge pull request api_response api_instance.merge_pull_request(repository, pull_request) print(The response of PullsApi-merge_pull_request:\n) pprint(api_response) # MergeResult(reference...) except Exception as e: print(Exception when calling PullsApi-merge_pull_request: %s\n % e)返回模型 MergeResult合并成功返回 MergeResult仅含reference字段——即合并后生成的 commit ID。请求头Accept: application/json无请求体。状态码状态码说明200merge completed返回MergeResult400Validation Error401Unauthorized403Forbidden404Resource Not Found409ConflictDeprecated响应内容 schema 将改为 Error 格式而非空MergeResult412precondition failed返回Error429too many requests0Internal Server Error底层实现服务端 MergePullRequest 的合并权限要求较特殊——需要同时具备目标分支上的fs:CreateCommit与仓库级的pr:WritePullRequest权限NodeTypeAnd逻辑。合并流程为① 检查 PR 状态必须为OPEN否则返回 400 bad pull request status② 调用Catalog.Merge将源分支合并进目标分支③ 合并成功后把 PR 状态更新为merged并写入合并产生的 commit IDMergedCommitID。合并失败时 PR 保持 open可重试。完整实战一个 PR 生命周期脚本综合以上 API下面给出一个端到端示例创建 PR → 查询详情 → 分页列出 → 更新状态 → 合并并打印每个环节的响应。import os import lakefs_sdk from lakefs_sdk.rest import ApiException from pprint import pprint configuration lakefs_sdk.Configuration( hosthttp://localhost:8000/api/v1, # 替换为你的 lakeFS 端点 usernameos.environ[LAKEFS_ACCESS_KEY], passwordos.environ[LAKEFS_SECRET_KEY], ) with lakefs_sdk.ApiClient(configuration) as api_client: pulls lakefs_sdk.PullsApi(api_client) repo my-repo try: # 1. 创建 PR created pulls.create_pull_request( repo, lakefs_sdk.PullRequestCreation( titleAdd weekly ETL job, descriptionIntroduces the weekly aggregation pipeline, source_branchfeature/weekly-etl, destination_branchmain, ), ) pr_id created.id print(created PR:, pr_id) # 2. 查询详情 detail pulls.get_pull_request(repo, pr_id) pprint(detail.to_dict()) # 3. 列出 open 状态的 PR每页 20 条 listing pulls.list_pull_requests(repo, amount20, statusopen) print(has_more:, listing.pagination.has_more) # 4. 更新描述 pulls.update_pull_request( repo, pr_id, lakefs_sdk.PullRequestBasic(descriptionUpdated after review), ) # 5. 合并 merged pulls.merge_pull_request(repo, pr_id) print(merged commit reference:, merged.reference) # 合并后查询可见 statusmerged 与 merged_commit_id after pulls.get_pull_request(repo, pr_id) pprint(after.to_dict()) except ApiException as e: print(Exception when calling PullsApi: %s\n % e)与服务端实现对照数据流全景从 Python SDK 到最终落库一次 PR 操作经过以下层次对应仓库中的真实文件Python SDK 层PullsApi方法构造 HTTP 请求路径形如/repositories/{repository}/pulls由 clients/python/lakefs_sdk/api/pulls_api.py 生成经ApiClient.call_api发送。API 规范层路由与 schema 由 api/swagger.yml 定义SDK 正是由该 OpenAPI 文件生成的Python 客户端生成配置见 clients/python/python-static/python-codegen-config.yaml。HTTP Controller 层Go 服务端 pkg/api/controller.go 负责鉴权pr:ReadPullRequest/pr:WritePullRequest/pr:ListPullRequests见 pkg/permissions/actions.go、审计日志LogAction与参数解析。Catalog 层pkg/catalog/catalog.go 负责输入校验、ID 生成graveler.NewRunID()、状态过滤与分页。Graveler/Ref 层pkg/graveler/graveler.go 定义 PR 领域模型与更新语义pkg/graveler/ref/manager.go 通过 KV 存储持久化——PR 本体存在主键分区同时按源分支-目标分支建立二级索引PullBySrcDstPath使用SetMsgIf条件写保证并发安全。值得一提的是PR 功能的设计初衷记录在仓库的 design/open/pull-request.md其目标是把变更请求建模为仓库级资源支持评审、讨论与合并前的自动化校验actions。当前仓库实现聚焦于 PR 的创建、查询、列表、更新与合并核心流程。常见问题与注意事项401/403 与权限若调用报Unauthorized或Forbidden请检查认证方式是否与服务端配置一致以及当前用户是否具备对应仓库的pr:ReadPullRequest读、pr:WritePullRequest写/更新/合并、pr:ListPullRequests列表权限。409 Conflict创建/合并创建时目标资源冲突如同一源-目标分支组合已存在 PR合并时则代表分支合并冲突——服务端明确提示 409 的响应内容 schema 未来会从空MergeResult迁移到Error格式注意兼容处理。amount 越界传入的amount超过 1000 或小于 0 时服务端会归一化为 1000SDK 侧也通过 pydantic 约束在-1~1000之间客户端请求即校验。status 过滤语义statusclosed同时覆盖closed与merged两类 PR若只想看已合并的 PR需要自行结合返回数据的status字段二次过滤。列表不含 descriptionlist_pull_requests出于权限考虑不返回描述字段需要完整信息请对每个 PR 调用get_pull_request。延伸阅读API 端点总览与认证方式clients/python/README.mdPullsApi 生成源码clients/python/lakefs_sdk/api/pulls_api.py相关数据模型PullRequest、PullRequestCreation、PullRequestBasic、PullRequestsList、MergeResult、PaginationOpenAPI 定义api/swagger.yml服务端实现pkg/api/controller.go、pkg/catalog/catalog.go、pkg/graveler/graveler.go、pkg/graveler/ref/manager.go功能设计文档design/open/pull-request.md赞分享数据工程数据湖大数据对象存储后端【免费下载链接】lakeFSlakeFS - Data version control for your data lake | Git for data项目地址https://gitcode.com/gh_mirrors/la/lakeFS点击查看免费下载相关推荐lakeFS Java SDK PullsApi 全解用代码驱动数据湖的 Pull Request 全生命周期lakeFS Java SDK PullsApi 全解用代码驱动数据湖的 Pull Request 全生命周期 导读本文围绕 lakeFS Java SDK数据工程数据湖大数据对象存储后端lakeFS Rust SDK 的 Pulls API 完全指南用代码管理数据湖的 Pull Request 生命周期lakeFS Rust SDK 的 Pulls API 完全指南用代码管理数据湖的 Pull Request 生命周期 导读 本文以 lakeFS 官方 Ru数据工程数据湖大数据对象存储后端Python 脚本如何自动抢大麦门票ticket-purchase 完整使用指南Python 脚本如何自动抢大麦门票ticket purchase 完整使用指南 20:00:00 整点大麦页面上的立即购买按钮刚亮起你手忙脚乱地点进数据工程数据湖大数据对象存储后端上一篇ToolJet Star Rating 组件完全指南星级评分组件的属性、事件、样式与源码实现下一篇oh-my-codex 0.12.5 稳定性补丁深度解析session 作用域、团队启动恢复与 Windows/tmux 修复创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询