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:
- Visit https://www.datastax.com/products/datastax-astra.
- Create a free account.
- Create a database and note down the API endpoint and keyspace name.
- Generate an API token (select "Organization Admin" from the dropdown).
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:
-
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)")
-
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:
- Replace placeholders : Update
YOUR_CLIENT_ID:YOUR_TOKENwith your AstraDB API token andhttps://your-database-endpoint.apps.astra.datastax.comwith your database's API endpoint. - Import necessary modules : Include
cassandra_operationfromcasdbops. - Create a casdbops object : Pass your
tokenas an argument. - Connect to the database : Use
connect_to_databasewith the API endpoint. - Error handling : Wrap the connection logic in a
try-exceptblock for graceful error handling.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e32ceb711e3521dc0788f56a8626db5527987649fe50c2e14add2f5fd972d013
|
|
| MD5 |
f426fd06987469272a5318c7581893ef
|
|
| BLAKE2b-256 |
6276ffe9402e2d20cee53c5f7d63c9f054147ecb4331cb1bf9e3d2bf66ffdc2b
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4358da4ac69a3985c25b2263aebc04f35473abfd00bc05c706c2322ab9cdf839
|
|
| MD5 |
c92c44354d04d50865575906c92910f0
|
|
| BLAKE2b-256 |
567b3f84c1367635035b399f2934fd8d47360ad18942b011966e74485ec3f818
|