> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# AstraDB 集成

> 与AstraDB文档加载器通过LangChain Python集成

> [DataStax Astra DB](https://docs.datastax.com/en/astra-db-serverless/index.html) 是基于 `Apache Cassandra®` 构建的 serverless AI-ready 数据库，可通过易于使用的 JSON API 方便获取。

## 概述

Astra DB 文档加载器会从 Astra DB 集合中读取 LangChain @\[`Document`] 对象并返回列表。

加载器接受以下参数：

* `api_endpoint`: Astra DB API 端点。格式类似 `https://01234567-89ab-cdef-0123-456789abcdef-us-east1.apps.astra.datastax.com`
* `token`: Astra DB 令牌。格式类似 `AstraCS:aBcD0123...`
* `collection_name` : AstraDB 集合名称
* `namespace`: (可选) AstraDB 命名空间（在 Astra DB 中称为 *keyspace*）
* `filter_criteria`: (可选) 查询 find 的过滤条件
* `projection`: (可选) 查询 find 的投影字段
* `limit`: (可选) 要检索的最大文档数
* `extraction_function`: (可选) 将 AstraDB 文档转换为 LangChain `page_content` 字符串的函数。默认值为 `json.dumps`

加载器为读取的文档设置以下元数据：

```python theme={null}
metadata={
    "namespace": "...",
    "api_endpoint": "...",
    "collection": "..."
}
```

## 配置

```python theme={null}
!pip install "langchain-astradb>=0.6,<0.7"
```

## 使用文档加载器加载数据

```python theme={null}
from langchain_astradb import AstraDBLoader
```

[**API 参考：** `AstraDBLoader`](https://reference.langchain.com/python/langchain-astradb/document_loaders/AstraDBLoader)

```python theme={null}
from getpass import getpass

ASTRA_DB_API_ENDPOINT = input("ASTRA_DB_API_ENDPOINT = ")
ASTRA_DB_APPLICATION_TOKEN = getpass("ASTRA_DB_APPLICATION_TOKEN = ")
```

```text theme={null}
ASTRA_DB_API_ENDPOINT =  https://01234567-89ab-cdef-0123-456789abcdef-us-east1.apps.astra.datastax.com
ASTRA_DB_APPLICATION_TOKEN =  ········
```

```python theme={null}
loader = AstraDBLoader(
    api_endpoint=ASTRA_DB_API_ENDPOINT,
    token=ASTRA_DB_APPLICATION_TOKEN,
    collection_name="movie_reviews",
    projection={"title": 1, "reviewtext": 1},
    limit=10,
)
```

```python theme={null}
docs = loader.load()
```

```python theme={null}
docs[0]
```

```text theme={null}
Document(metadata={'namespace': 'default_keyspace', 'api_endpoint': 'https://01234567-89ab-cdef-0123-456789abcdef-us-east1.apps.astra.datastax.com', 'collection': 'movie_reviews'}, page_content='{"_id": "659bdffa16cbc4586b11a423", "title": "Dangerous Men", "reviewtext": "\\"Dangerous Men,\\" the picture\'s production notes inform, took 26 years to reach the big screen. After having seen it, I wonder: What was the rush?"}')
```

***

> 官方原文：[src/oss/python/integrations/document\_loaders/astradb.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/document_loaders/astradb.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.