---
title: Charmhub | Deploy Cassandra using Charmhub - The Open Operator Collection
description: Deploy the latest version of Cassandra as a Kubernetes Operator on any
  cloud.
url: https://charmhub.io/cassandra-k8s/libraries/cql
---

# Cassandra

[Dylan Stephano-Shachter](https://charmhub.io/publisher/dylanstathis "View all packages from Dylan Stephano-Shachter")

* [Dylan Stephano-Shachter](https://charmhub.io/publisher/dylanstathis "View all packages from Dylan Stephano-Shachter")

Platform:

edge 11

```
juju deploy cassandra-k8s --channel edge
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/cassandra-k8s/libraries/cql#drawer)

## charms.cassandra\_k8s.v0.cql

* [*Docstrings*Docstrings](https://charmhub.io/cassandra-k8s/libraries/cql)
  [*Code*Source code](https://charmhub.io/cassandra-k8s/libraries/cql/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.cassandra_k8s.v0.cql
    ```

    [Download cql.py](https://charmhub.io/cassandra-k8s/libraries/cql/download)
  + *Last updated* 16 Nov 2021
  + *Revision* Library version 0.3

#### Cassandra charm library.

##### Overview

This document explains how to integrate with the Cassandra charm for the
purposes of consuming a cassandra database. It also explains how alternative
implementations of the Cassandra charm may maintain the same interface and be
compatible with all currently integrated charms. Finally this document is the
authoritative reference on the structure of relation data that is shared
between Cassandra charms and any other charm that intends to use the database.

##### Consumer Library Usage

Charms that would like to use a Cassandra database must use the `CassandraConsumer`
object from the charm library. Using the `CassandraConsumer` object requires
instantiating it, typically in the constructor of your charm. The `CassandraConsumer`
constructor requires the name of the relation over which a database will be used.
This relation must use the `cassandra` interface.

```
from charms.cassandra_k8s.v0.cassandra import CassandraConsumer

def __init__(self, *args):
    super().__init__(*args)
    ...
    self.cassandra_consumer = CassandraConsumer(
        self, "monitoring"}
    )
    ...
```

The `address`, `port`, and `credentials` methods of the
`CassandraConsumer` object can all be called to get the relevant connection
information from the relation data.

##### Provider Library Usage

The `CassandraProvider` object may be used by Cassandra charms to manage
relations with their clients. For this purpose a Cassandra charm needs to do
three things

1. Instantiate the `CassandraProvider` object providing it with the required
   parameters:

* Name of the relation that the Cassandra charm uses to interact with
  clients. This relation must conform to the `cql` interface.

For example a Cassandra charm may instantiate the `CassandraProvider` in its
constructor as follows

```
from charms.cassandra_k8s.v0.cassandra import CassandraProvider

def __init__(self, *args):
    super().__init__(*args)
    ...
    self.prometheus_provider = CassandraProvider(
            self, "database"
        )
    ...
```

2. A Cassandra charm must set the address, port, and credentials when the
   information becomes available. For example

   ...
   self.cassandra\_provider.update\_port("database", port)
   self.cassandra\_provider.update\_address("database", address)
   self.cassandra\_provider.set\_credentials(rel\_id, [username, password])
   ...

---

Index

* [class CassandraConsumerError](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraconsumererror)
* [class NameDuplicateError](https://charmhub.io/cassandra-k8s/libraries/cql#nameduplicateerror)
* [class NameLengthError](https://charmhub.io/cassandra-k8s/libraries/cql#namelengtherror)
* [class CassandraConsumer](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraconsumer)
* + [def \_\_init\_\_(
    self,
    charm,
    name)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraconsumer-__init__)
* + [def credentials(
    self,
    rel\_id)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraconsumer-credentials)
* + [def port(
    self,
    rel\_id)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraconsumer-port)
* + [def address(
    self,
    rel\_id)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraconsumer-address)
* [class CassandraProvider](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraprovider)
* + [def \_\_init\_\_(
    self,
    charm,
    name)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraprovider-__init__)
* + [def update\_port(
    self,
    relation\_name,
    port)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraprovider-update_port)
* + [def update\_address(
    self,
    relation\_name,
    address)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraprovider-update_address)
* + [def credentials(
    self,
    rel\_id)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraprovider-credentials)
* + [def set\_credentials(
    self,
    rel\_id,
    creds)](https://charmhub.io/cassandra-k8s/libraries/cql#cassandraprovider-set_credentials)
* [def sanitize\_name(
  name
  )](https://charmhub.io/cassandra-k8s/libraries/cql#sanitize_name)

#### class CassandraConsumerError

Description

Error base class. None

#### class NameDuplicateError

Description

Duplicate db names. None

#### class NameLengthError

Description

Name is too long. None

#### class CassandraConsumer

Description

Cassandra consumer object. None

Methods

CassandraConsumer.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
name: str
)

Constructor fot the CassandraConsumer object.

Arguments

charm

The charm object that instantiated this class.

name

The name of the cql relation.

CassandraConsumer.
credentials(

*self*

,
rel\_id: int
)

Returns the credentials.

Arguments

rel\_id

Relation id. Required for multi mode.

Returns

The credentials in the form of [<username>, <password>]

CassandraConsumer.
port(

*self*

,
rel\_id: int
)

Return the port which the cassandra instance is listening on.

Arguments

rel\_id

Relation id. Required for multi mode.

CassandraConsumer.
address(

*self*

,
rel\_id: int
)

Return the address which the cassandra instance is listening on.

Arguments

rel\_id

Relation id. Required for multi mode.

#### class CassandraProvider

Description

Cassandra provider object. None

Methods

CassandraProvider.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
name: str
)

Constructor for CassandraProvider.

Arguments

charm

The charm object that instantiated this class.

name

The name of the cql relation.

CassandraProvider.
update\_port(

*self*

,
relation\_name: str

,
port: int
)

Update the port which Cassandra is listening on.

Arguments

relation\_name

The name of the cql relation.

port

The port number.

CassandraProvider.
update\_address(

*self*

,
relation\_name: str

,
address: str
)

Update the address which Cassandra is listening on.

Arguments

relation\_name

The name of the cql relation.

address

The address which Cassandra is listening on.

CassandraProvider.
credentials(

*self*

,
rel\_id: int
)

Return the set credentials.

Arguments

rel\_id

Relation id to look up credentials for.

CassandraProvider.
set\_credentials(

*self*

,
rel\_id: int

,
creds
)

Set the credentials for a related charm.

Arguments

rel\_id

Relation id to set credentials for.

creds

A tuple or list of the form [username, password].

#### def sanitize\_name(name: str)

Description

Make a name safe for use as a keyspace name. None
