Skip to main content

A simple CRUD library for Apache Cassandra

Project description

CassandraCRUD Library

CassandraCRUD is a Python library that provides a simple and flexible interface for interacting with Apache Cassandra databases. It wraps the DataStax Cassandra driver and offers additional utility functions for common database operations.

Features

  • Easy connection management
  • Flexible query execution (synchronous and asynchronous)
  • Batch operations support
  • Customizable consistency levels
  • Built-in CRUD operations
  • Table management utilities
  • Configurable through environment variables or direct parameters

Installation

To install CassandraCRUD, you can use pip:

pip install cassandracrud

Usage

Here's a basic example of how to use CassandraCRUD:

from cassandracrud import CassandraCRUD

# Initialize the CassandraCRUD instance
crud = CassandraCRUD(
    contact_points=["localhost"],
    keyspace="my_keyspace",
    username="user",
    password="pass"
)

# Connect to the database
crud.connect()

# Execute a query
result = crud.execute("SELECT * FROM my_table")

# Create a new record
crud.create("my_table", {"id": 1, "name": "John Doe"})

# Close the connection
crud.close()

Configuration

You can configure CassandraCRUD either by passing parameters to the constructor or by setting environment variables:

  • CASSANDRA_PROD_CONTACT_POINTS: Comma-separated list of contact points
  • CASSANDRA_PROD_KEYSPACE: Keyspace name
  • CASSANDRA_PROD_USERNAME: Username for authentication
  • CASSANDRA_PROD_PASSWORD: Password for authentication

If both environment variables and constructor parameters are provided, the constructor parameters take precedence.

Main Methods

  • connect(): Establishes a connection to the Cassandra cluster
  • execute(query, params=None): Executes a CQL query
  • execute_async(query, params=None): Executes a CQL query asynchronously
  • prepare(query): Prepares a CQL statement
  • execute_batch(statements): Executes a batch of CQL statements
  • create(table, data): Inserts a new record into a table
  • read(table, conditions=None): Retrieves records from a table
  • update(table, data, conditions): Updates records in a table
  • delete(table, conditions): Deletes records from a table

Utility Methods

  • table_exists(table_name): Checks if a table exists
  • create_table(table_name, column_definitions): Creates a new table
  • drop_table(table_name): Drops a table
  • get_table_schema(table_name): Retrieves the schema of a table
  • get_metrics(): Retrieves cluster metrics
  • set_consistency_level(consistency_level): Sets the consistency level for queries

Advanced Usage

Asynchronous Queries

future = crud.execute_async("SELECT * FROM my_table")
# Do other work...
result = future.result()

Batch Operations

statements = [
    ("INSERT INTO my_table (id, name) VALUES (%s, %s)", (1, "John")),
    ("INSERT INTO my_table (id, name) VALUES (%s, %s)", (2, "Jane"))
]
crud.execute_batch(statements)

Changing Consistency Level

from cassandra import ConsistencyLevel
crud.set_consistency_level(ConsistencyLevel.ALL)

Contributing

Contributions to CassandraCRUD are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cassandracrud-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cassandracrud-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file cassandracrud-0.1.0.tar.gz.

File metadata

  • Download URL: cassandracrud-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for cassandracrud-0.1.0.tar.gz
Algorithm Hash digest
SHA256 34474e30ad2d1c9520e8a2de7ebd7dced9e1d2887e6c483900f79d64b8a27d4e
MD5 6b3ae3c097be640da4da727581843e50
BLAKE2b-256 5f907aed8dbb4c43bb9144ba0123b5838a3e9229b813548f4fbf1d6fa965a62d

See more details on using hashes here.

File details

Details for the file cassandracrud-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cassandracrud-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for cassandracrud-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64958c72d6c3a8eba19e11b4c978e2491c3b82e60fe7940116c8754190b98d80
MD5 8b6d9519cb661bf845c10188f85b4e91
BLAKE2b-256 ec2b42dd212dfff5b0b447879a78571d731cf817346a654262a2029be65e8809

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page