跳至内容

Cassandra

Cassandra数据库工具规范 #

基类: BaseToolSpec

用于与Apache Cassandra数据库交互的基础工具。

Source code in llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class CassandraDatabaseToolSpec(BaseToolSpec):
    """Base tool for interacting with an Apache Cassandra database."""

    db: CassandraDatabase = Field(exclude=True)

    spec_functions = [
        "cassandra_db_query",
        "cassandra_db_schema",
        "cassandra_db_select_table_data",
    ]

    def __init__(self, db: CassandraDatabase) -> None:
        """DB session in context."""
        self.db = db

    def cassandra_db_query(self, query: str) -> List[Document]:
        """
        Execute a CQL query and return the results as a list of Documents.

        Args:
            query (str): A CQL query to execute.

        Returns:
            List[Document]: A list of Document objects, each containing data from a row.

        """
        documents = []
        result = self.db.run_no_throw(query, fetch="Cursor")
        for row in result:
            doc_str = ", ".join([str(value) for value in row])
            documents.append(Document(text=doc_str))
        return documents

    def cassandra_db_schema(self, keyspace: str) -> List[Document]:
        """
        Input to this tool is a keyspace name, output is a table description
            of Apache Cassandra tables.
            If the query is not correct, an error message will be returned.
            If an error is returned, report back to the user that the keyspace
            doesn't exist and stop.

        Args:
            keyspace (str): The name of the keyspace for which to return the schema.

        Returns:
            List[Document]: A list of Document objects, each containing a table description.

        """
        return [Document(text=self.db.get_keyspace_tables_str(keyspace))]

    def cassandra_db_select_table_data(
        self, keyspace: str, table: str, predicate: str, limit: int
    ) -> List[Document]:
        """
        Tool for getting data from a table in an Apache Cassandra database.
            Use the WHERE clause to specify the predicate for the query that uses the
            primary key. A blank predicate will return all rows. Avoid this if possible.
            Use the limit to specify the number of rows to return. A blank limit will
            return all rows.

        Args:
            keyspace (str): The name of the keyspace containing the table.
            table (str): The name of the table for which to return data.
            predicate (str): The predicate for the query that uses the primary key.
            limit (int): The maximum number of rows to return.

        Returns:
            List[Document]: A list of Document objects, each containing a row of data.

        """
        return [
            Document(text=self.db.get_table_data(keyspace, table, predicate, limit))
        ]

cassandra_db_query #

cassandra_db_query(query: str) -> List[Document]

执行一个CQL查询并将结果作为文档列表返回。

参数:

名称 类型 描述 默认值
query str

要执行的CQL查询。

required

返回:

类型 描述
List[Document]

List[Document]: 一个Document对象列表,每个对象包含来自一行的数据。

Source code in llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def cassandra_db_query(self, query: str) -> List[Document]:
    """
    Execute a CQL query and return the results as a list of Documents.

    Args:
        query (str): A CQL query to execute.

    Returns:
        List[Document]: A list of Document objects, each containing data from a row.

    """
    documents = []
    result = self.db.run_no_throw(query, fetch="Cursor")
    for row in result:
        doc_str = ", ".join([str(value) for value in row])
        documents.append(Document(text=doc_str))
    return documents

cassandra_db_schema #

cassandra_db_schema(keyspace: str) -> List[Document]

该工具的输入是一个键空间名称,输出是Apache Cassandra表的描述。 如果查询不正确,将返回错误信息。 如果返回错误,请向用户报告该键空间不存在并停止。

参数:

名称 类型 描述 默认值
keyspace str

要返回其架构的键空间名称。

required

返回:

类型 描述
List[Document]

List[Document]: 一个Document对象列表,每个对象包含一个表格描述。

Source code in llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def cassandra_db_schema(self, keyspace: str) -> List[Document]:
    """
    Input to this tool is a keyspace name, output is a table description
        of Apache Cassandra tables.
        If the query is not correct, an error message will be returned.
        If an error is returned, report back to the user that the keyspace
        doesn't exist and stop.

    Args:
        keyspace (str): The name of the keyspace for which to return the schema.

    Returns:
        List[Document]: A list of Document objects, each containing a table description.

    """
    return [Document(text=self.db.get_keyspace_tables_str(keyspace))]

cassandra_db_select_table_data #

cassandra_db_select_table_data(keyspace: str, table: str, predicate: str, limit: int) -> List[Document]

用于从Apache Cassandra数据库表中获取数据的工具。 使用WHERE子句指定查询主键的谓词条件。空谓词将返回所有行,应尽量避免这种情况。 使用limit参数指定返回的行数。空limit将返回所有行。

参数:

名称 类型 描述 默认值
keyspace str

包含该表的键空间名称。

required
table str

要返回数据的表名。

required
predicate str

使用主键的查询谓词。

required
limit int

返回的最大行数。

required

返回:

类型 描述
List[Document]

List[Document]: 一个Document对象列表,每个对象包含一行数据。

Source code in llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def cassandra_db_select_table_data(
    self, keyspace: str, table: str, predicate: str, limit: int
) -> List[Document]:
    """
    Tool for getting data from a table in an Apache Cassandra database.
        Use the WHERE clause to specify the predicate for the query that uses the
        primary key. A blank predicate will return all rows. Avoid this if possible.
        Use the limit to specify the number of rows to return. A blank limit will
        return all rows.

    Args:
        keyspace (str): The name of the keyspace containing the table.
        table (str): The name of the table for which to return data.
        predicate (str): The predicate for the query that uses the primary key.
        limit (int): The maximum number of rows to return.

    Returns:
        List[Document]: A list of Document objects, each containing a row of data.

    """
    return [
        Document(text=self.db.get_table_data(keyspace, table, predicate, limit))
    ]
优云智算