[ PROMPT_NODE_24228 ]
R2 Data Catalog API 参考
[ SKILL_DOCUMENTATION ]
# API 参考
R2 数据目录公开了标准的 [Apache Iceberg REST 目录 API](https://github.com/apache/iceberg/blob/main/open-api/rest-catalog-open-api.yaml)。
## 快速参考
**最常见的操作:**
| 任务 | PyIceberg 代码 |
|------|----------------|
| 连接 | `RestCatalog(name="r2", warehouse=bucket, uri=uri, token=token)` |
| 列出命名空间 | `catalog.list_namespaces()` |
| 创建命名空间 | `catalog.create_namespace("logs")` |
| 创建表 | `catalog.create_table(("ns", "table"), schema=schema)` |
| 加载表 | `catalog.load_table(("ns", "table"))` |
| 追加数据 | `table.append(pyarrow_table)` |
| 查询数据 | `table.scan().to_pandas()` |
| 压缩文件 | `table.rewrite_data_files(target_file_size_bytes=128*1024*1024)` |
| 过期快照 | `table.expire_snapshots(older_than=timestamp_ms, retain_last=10)` |
## REST 端点
基础地址:`https://.r2.cloudflarestorage.com/iceberg/`
| 操作 | 方法 | 路径 |
|-----------|--------|------|
| 目录配置 | GET | `/v1/config` |
| 列出命名空间 | GET | `/v1/namespaces` |
| 创建命名空间 | POST | `/v1/namespaces` |
| 删除命名空间 | DELETE | `/v1/namespaces/{ns}` |
| 列出表 | GET | `/v1/namespaces/{ns}/tables` |
| 创建表 | POST | `/v1/namespaces/{ns}/tables` |
| 加载表 | GET | `/v1/namespaces/{ns}/tables/{table}` |
| 更新表 | POST | `/v1/namespaces/{ns}/tables/{table}` |
| 删除表 | DELETE | `/v1/namespaces/{ns}/tables/{table}` |
| 重命名表 | POST | `/v1/tables/rename` |
**认证:** 请求头中的 Bearer 令牌:`Authorization: Bearer `
## PyIceberg 客户端 API
大多数用户使用 PyIceberg,而非原始 REST API。
### 连接
python
from pyiceberg.catalog.rest import RestCatalog
catalog = RestCatalog(
name="my_catalog",
warehouse="",
uri="",
token="",
)
### 命名空间操作
python
from pyiceberg.exceptions import NamespaceAlreadyExistsError
namespaces = catalog.list_namespaces() # [('default',), ('logs',)]
catalog.create_namespace("logs", properties={"owner": "team"})
catalog.drop_namespace("logs") # 必须为空
### 表操作
python
from pyiceberg.schema import Schema
from pyiceberg.types import NestedField, StringType, IntegerType
schema = Schema(
NestedField(1, "id", IntegerType(), required=True),
NestedField(2, "name", StringType(), required=False),
)
table = catalog.create_table(("logs", "app_logs"), schema=schema