Apache Cassandra Python Driver

repository·trunk·Indexed 23 days ago

https://github.com/apache/cassandra-python-driver

The official Python client for Apache Cassandra and DataStax Enterprise (DSE), providing a high-performance, asynchronous interface. It includes the Cluster and Session classes for CQL execution, support for Execution Profiles, custom authentication via AuthProvider and Authenticator, and a built-in Object Mapper (cqlengine) for mapping Python classes to CQL tables.

Tokens
48.2K
Snippets
111
Records
255
Agent score
80%

What's inside cassandra-python-driver

  1. Overview of the DataStax Python Driver for Apache Cassandra

    trunk

    The DataStax Python Driver is a client driver for Apache Cassandra that works exclusively with Cassandra Query Language v3 (CQL3) and Cassandra's native protocol.

    Compatibility:

    • Cassandra Versions: Supports Cassandra 2.1+ and DSE 4.7+.
    • Python Versions: Supports Python 3.10 through 3.14.
    • Architecture Note: DataStax products do not support big-endian systems.

    License: Apache v2 License.

  2. Overview of Apache Cassandra Python Driver features

    trunk

    The driver provides a highly-tunable interface for interacting with Cassandra clusters. Key features include:

    • Execution APIs: Both Synchronous and Asynchronous APIs.
    • Statement Types: Support for Simple, Prepared, and Batch statements.
    • Performance & Scaling: Asynchronous IO, parallel execution, request pipelining, and connection pooling.
    • Cluster Management: Automatic node discovery, automatic reconnection, and configurable load balancing and retry policies.
    • Utilities: Concurrent execution utilities and an Object Mapper.
    • Specialized Support: Connecting to DataStax Astra (cloud), DSE Graph execution, DSE Geometric type serialization, and DSE PlainText/GSSAPI authentication.
  3. Use the Object Mapper (cqlengine)

    trunk

    The Object Mapper (cqlengine) allows you to interact with Cassandra using Python objects instead of raw CQL. It provides high-level abstractions for defining data models and performing queries. Key components include:

    • cassandra.cqlengine.models: Defining table models.
    • cassandra.cqlengine.columns: Defining column types for models.
    • cassandra.cqlengine.query: Performing high-level queries on models.
    • cassandra.cqlengine.connection: Managing the connection for the mapper.
    • cassandra.cqlengine.management: Tools for managing keyspaces and tables.
    • cassandra.cqlengine.usertype: Support for User Defined Types (UDTs).
  4. Use the DataStax Graph Fluent API

    trunk

    The Graph Fluent API extends the core driver with TinkerPop support, allowing you to build and execute Gremlin traversals directly in Python. It provides a GraphTraversalSource builder, execute_graph methods for explicit execution, GraphSON serializers for DSE graph types, and DSE Search predicates.

    Note: This API depends on Apache TinkerPop and requires additional installation steps. It is not included in the core driver by default.

  5. Use the cassandra.datastax.graph.fluent module for graph queries

    trunk

    The cassandra.datastax.graph.fluent module provides a fluent API for interacting with DataStax Graph. The primary entry point is the DseGraph class, which allows you to execute graph queries and manage traversals.

    Key capabilities of DseGraph include:

    • traversal_source(...): Creates a traversal source to start building graph queries. You can specify a session, graph_name, and an execution_profile.
    • query_from_traversal(...): Executes a query derived from a traversal.
    • batch(...): Provides batching capabilities for graph operations.
    • create_execution_profile(...): Configures execution profiles for graph queries.

    It also provides specialized row factories like graph_traversal_row_factory and graph_traversal_dse_object_row_factory for handling query results.

  6. Explore the Core Driver API

    trunk

    The Core Driver provides the fundamental interface for interacting with Apache Cassandra. It includes modules for managing clusters, connection pooling, authentication, query execution, and protocol handling. Key modules include:

    • cassandra.cluster: Managing connections to the cluster.
    • cassandra.query: Executing CQL queries.
    • cassandra.policies: Load balancing and retry policies.
    • cassandra.auth: Authentication mechanisms.
    • cassandra.metadata: Accessing cluster and keyspace metadata.
    • cassandra.concurrent: Concurrent execution utilities.
  7. Use DataStax Graph API

    trunk

    The driver includes support for DataStax Graph, providing both an index-based API and a fluent query API.

    • cassandra.datastax.graph: The main entry point for Graph operations.
    • cassandra.datastax.graph.fluent: A fluent interface for building graph queries, including predicates for filtering results.
  8. Use the correct factories for data return formats

    trunk

    The cassandra.decoder module contains factory functions for defining how query results are formatted. However, all functions in this module have been deprecated since version 2.0.0.

    To ensure compatibility with future versions, you should use the corresponding factories located in the cassandra.query module instead of cassandra.decoder.