Connect the Python driver to your database

DataStax recommends using the Python client with HCD databases. Use the Python driver only if you are working with an existing application that previously used a CQL-based driver or if you plan to explicitly use CQL.

Select one of the following methods for connecting the Python driver to your database.

Prerequisite

Basic configuration

This configuration is recommended for basic use cases that are not proofs of concept or production use. It initializes a session for connecting to your database using the cassandra-driver and sets up the connection with authentication details sourced from environment variables.

import os from cassandra.cluster import Cluster from cassandra.auth import PlainTextAuthProvider

auth_provider = PlainTextAuthProvider(username='cassandra', password='cassandra') cluster = Cluster(auth_provider=auth_provider) session = cluster.connect()

Production configuration

This configuration is for proofs of concept or production use. This code example includes additional configurations for connection timeout, request timeout, and protocol version.

from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT, ProtocolVersion
auth_provider=PlainTextAuthProvider("username", "password"])
profile = ExecutionProfile(request_timeout=30)
cluster = Cluster(
    cloud=cloud_config,
    auth_provider=auth_provider,
    execution_profiles={EXEC_PROFILE_DEFAULT: profile},
    protocol_version=ProtocolVersion.V4
)
session = cluster.connect()

See the Python quickstart for details on how to install the package, connect to an HCD database, create a table and vector-compatible Storage-Attached Index (SAI), load data, and perform a similarity search.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com