Skip to main content

A python package for connecting with database.

Project description

casdbops: A Streamlined Python Library for Cassandra on AstraDB

Introduction

casdbops is a user-friendly Python library designed to simplify interacting with Cassandra databases hosted on AstraDB, a cloud-based database service from DataStax. It streamlines common database operations, making it easier for developers to work with Cassandra without getting bogged down in low-level details.

What can you do with casdbops?

  • Effortless Database Management:

    • Create and manage Cassandra databases on AstraDB with ease.
    • Provision databases programmatically as needed within your applications.
  • Seamless Data Interaction:

    • Connect to existing databases using their API endpoints.
    • Create collections (tables) within your databases to organize data.
    • Insert data from CSV or Excel files into your collections for efficient storage.
    • Retrieve data from collections as pandas DataFrames for further analysis or manipulation.

Benefits of using casdbops:

  • Simplified Development: Abstracted complexities of Cassandra interaction, allowing you to focus on your application logic.
  • Improved Efficiency: Streamlined database operations lead to faster development and execution times.
  • Enhanced Readability: Pythonic code with clear method names promotes code maintainability and comprehension.
  • Reduced Boilerplate: Less code required for common tasks, minimizing redundancy and error potential.

Who is casdbops for?

  • Python developers who need to interact with Cassandra databases on AstraDB.
  • Data engineers and data scientists seeking a convenient way to manage and manipulate Cassandra data.
  • Backend developers building applications that rely on Cassandra for persistent storage.

By leveraging casdbops, you can significantly streamline your Cassandra development experience on AstraDB, allowing you to focus on building innovative and high-performing applications.

Installation

Install casdbops using pip:

pip install casdbops

Prerequisites

Before using casdbops, you'll need an AstraDB account. Here's how to set one up:

Usage

Import the library:

from casdbops.cassandra_crud import cassandra_operation

Create a casdbops object:

Provide your AstraDB API token as an argument:

obj = cassandra_operation("AstraCS:YOUR_CLIENT_ID:YOUR_TOKEN")
# Replace YOUR_CLIENT_ID and YOUR_TOKEN with your actual values.

Database Connection

Note:- casdbops offers two ways to connect to a database:

  1. Manual API Endpoint:

If you already have a database created, provide its API endpoint directly:

con_obj = obj.connect_to_database(api_endpoint="[https://your-database-endpoint.apps.astra.datastax.com](https://your-database-endpoint.apps.astra.datastax.com)")
  1. Automatic Creation and Connection:

If you want to create a new database and connect to it seamlessly, provide your password along with the token:

con_obj = obj.create_database(
    database_name="your_database_name",
    passwd="your_database_password",
    connect_to_same_database=True # Automatically connects to the created database
)  

Creating Collections

Use the create_collection method to create a collection within your database:

collection_obj = obj.create_collection("your_collection_name")

Inserting Data into the Collection

Pass the path to your CSV or Excel dataset to the insert_into_collection method:

obj.insert_into_collection("path/to/your/dataset.csv")
# or
obj.insert_into_collection("path/to/your/dataset.xlsx")

Fetching Data from the Collection

The Fetch_data_from_collection method retrieves data as a pandas DataFrame:

df = obj.Fetch_data_from_collection()

Code Prompts

This section offers interactive code prompts to help you experiment with the library and explore its functionalities:

  • Prompt 1: Connecting to an Existing Database

# Replace with your AstraDB API token
token = "YOUR_CLIENT_ID:YOUR_TOKEN"

# Replace with your database API endpoint
api_endpoint = "[https://your-database-endpoint.apps.astra.datastax.com](https://your-database-endpoint.apps.astra.datastax.com)"

from casdbops.cassandra_crud import cassandra_operation

# Create a casdbops object
obj = cassandra_operation(token)

# Connect to the database
try:
    con_obj = obj.connect_to_database(api_endpoint=api_endpoint)
    print("Successfully connected to database!")
except Exception as e:
    print("Connection error:", e)
  • Prompt 2: Creating a New Database and Collection

# Replace with your AstraDB API token
token = "YOUR_CLIENT_ID:YOUR_TOKEN"

from casdbops.cassandra_crud import cassandra_operation

# Create a casdbops object
obj = cassandra_operation(token)

# Create a new database (adjust name as needed)
try:
    con_obj = obj.create_database(
        database_name="my_new_database",
        passwd="your_strong_password",
        connect_to_same_database=True
        )
    print("Database created successfully!")
except Exception as e:
    print("Database creation error:", e)

# Create a collection within the new database
collection_obj = obj.create_collection("my_collection")
print("Collection created:", collection_obj.name)

Explanation:

  1. Replace placeholders : Update YOUR_CLIENT_ID:YOUR_TOKEN with your AstraDB API token and https://your-database-endpoint.apps.astra.datastax.com with your database's API endpoint.
  2. Import necessary modules : Include cassandra_operation from casdbops.
  3. Create a casdbops object : Pass your token as an argument.
  4. Connect to the database : Use connect_to_database with the API endpoint.
  5. Error handling : Wrap the connection logic in a try-except block for graceful error handling.

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

casdbops-0.0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

casdbops-0.0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file casdbops-0.0.2.tar.gz.

File metadata

  • Download URL: casdbops-0.0.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for casdbops-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e32ceb711e3521dc0788f56a8626db5527987649fe50c2e14add2f5fd972d013
MD5 f426fd06987469272a5318c7581893ef
BLAKE2b-256 6276ffe9402e2d20cee53c5f7d63c9f054147ecb4331cb1bf9e3d2bf66ffdc2b

See more details on using hashes here.

File details

Details for the file casdbops-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: casdbops-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for casdbops-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4358da4ac69a3985c25b2263aebc04f35473abfd00bc05c706c2322ab9cdf839
MD5 c92c44354d04d50865575906c92910f0
BLAKE2b-256 567b3f84c1367635035b399f2934fd8d47360ad18942b011966e74485ec3f818

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