Skip to main content

AstraPy is a Pythonic SDK for DataStax Astra

Project description

AstraPy

AstraPy is a Pythonic SDK for DataStax Astra and Stargate

Resources

Getting Started

Install AstraPy

pip install astrapy

Setup your Astra client

Create a .env file with the appropriate values, or use the 'astra' cli to do the same.

ASTRA_DB_KEYSPACE="<keyspace>"
ASTRA_DB_APPLICATION_TOKEN="<AstraCS:...>"
ASTRA_DB_REGION="<region>"
ASTRA_DB_ID=<db_id>

Load the variables in and then create the client. This collections client can make non-vector and vector calls, depending on the call configuration.

import os
import sys

from dotenv import load_dotenv

from astrapy.db import AstraDB, AstraDBCollection
from astrapy.ops import AstraDBOps

sys.path.append("../")

load_dotenv()

# First, we work with devops
token = os.getenv("ASTRA_DB_APPLICATION_TOKEN")
astra_ops = AstraDBOps(token=token)

# Define a database to create
database_definition = {
    "name": "vector_test",
    "tier": "serverless",
    "cloudProvider": "GCP",
    "keyspace": os.getenv("ASTRA_DB_KEYSPACE", "default_keyspace"),
    "region": os.getenv("ASTRA_DB_REGION", None),
    "capacityUnits": 1,
    "user": "example",
    "password": token,
    "dbType": "vector",
}

# Create the database
create_result = astra_ops.create_database(database_definition=database_definition)

# Grab the new information from the database
# NOTE: Your database will take some time to initialize!
database_id = create_result["id"]
database_region = astra_ops.get_database()[0]["info"]["region"]
database_base_url = "apps.astra.datastax.com"

# Build the endpoint URL:
api_endpoint = f"https://{database_id}-{database_region}.{database_base_url}"

# Initialize our vector db
astra_db = AstraDB(token=token, api_endpoint=api_endpoint)

# Possible Operations
astra_db.create_collection(collection_name="collection_test_delete", dimension=5)
astra_db.delete_collection(collection_name="collection_test_delete")
astra_db.create_collection(collection_name="collection_test", dimension=5)

# Collections
astra_db_collection = AstraDBCollection(
    collection_name="collection_test", astra_db=astra_db
)
# Or...
astra_db_collection = AstraDBCollection(
    collection_name="collection_test", token=token, api_endpoint=api_endpoint
)

astra_db_collection.insert_one(
    {
        "_id": "5",
        "name": "Coded Cleats Copy",
        "description": "ChatGPT integrated sneakers that talk to you",
        "$vector": [0.25, 0.25, 0.25, 0.25, 0.25],
    }
)

astra_db_collection.find_one({"name": "potato"})  # Not found
astra_db_collection.find_one({"name": "Coded Cleats Copy"})

More Information

Check out the notebook which has examples for finding and inserting information into the database, including vector commands.

Take a look at the astra db tests for specific endpoint examples.

Using the Ops Client

You can use the Ops client to work with the Astra DevOps API. Check the devops tests

For Developers

Testing

Ensure you provide all required environment variables:

export ASTRA_DB_ID="..."
export ASTRA_DB_REGION="..."
export ASTRA_DB_APPLICATION_TOKEN="..."
export ASTRA_DB_KEYSPACE="..."
export ASTRA_CLIENT_ID="..."
export ASTRA_CLIENT_SECRET="..."

then you can run:

PYTHONPATH=. pytest

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

astrapy-0.5.3.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

astrapy-0.5.3-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file astrapy-0.5.3.tar.gz.

File metadata

  • Download URL: astrapy-0.5.3.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for astrapy-0.5.3.tar.gz
Algorithm Hash digest
SHA256 b89e6f8fb1ec25bf08c53a6e4ab604083e66c8d527c4ad7d0f032690d329e2f8
MD5 e0576e1c5795f997ea57fa88cdb574b1
BLAKE2b-256 0ee13c1cf18f6b6407b6fb5f134f30f5ead0bcf1ed8588c9e4853dc2177e89ba

See more details on using hashes here.

File details

Details for the file astrapy-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: astrapy-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for astrapy-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 559cafce14c9a19a7337fa29b8f6b111fd8e9d07a13fd1f63ecaab70296e6167
MD5 0b28fa735675cea51c10425ccaacdd3a
BLAKE2b-256 5b967e044859f2ac5c9c870177ff0f7dbab390fc98c516c221bd1c031ec285c0

See more details on using hashes here.

Supported by

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