Skip to main content

AstraPy is a Pythonic SDK for DataStax Astra

Project description

AstraPy

Actions Status

AstraPy is a Pythonic SDK for DataStax Astra and Stargate

Resources

Getting Started

Install AstraPy

pip install astrapy

Setup your Astra client

from astrapy.client import create_astra_client

astra_client = create_client(astra_database_id=ASTRA_DB_ID,
                             astra_database_region=ASTRA_DB_REGION,
                             astra_application_token=ASTRA_DB_APPLICATION_TOKEN)

Take a look at the client tests and the collection tests for specific endpoint examples.

Using the Ops Client

You can use the Ops client to work the with Astra DevOps API. API Reference

# astra_client created above
# create a keyspace using the Ops API
astra_client.ops.create_keyspace(database=ASTRA_DB_ID, keyspace=KEYSPACE_NAME)

Using the REST Client

You can use the REST client to work with the Astra REST API. API Reference

# astra_client created above
# search a table
res = astra_client.rest.search_table(keyspace=ASTRA_DB_KEYSPACE,
                                     table=TABLE_NAME,
                                     query={"firstname": {"$eq": "Cliff"}})
print(res["count"]) # number of results
print(res["data"]) # list of rows

Using the Schemas Client

You can use the Schemas client to work with the Astra Schemas API. API Reference

# astra_client created above
# create a table
astra_client.schemas.create_table(keyspace=ASTRA_DB_KEYSPACE, table_definition={
    "name": "my_table",
    "columnDefinitions": [
        {
            "name": "firstname",
            "typeDefinition": "text"
        },
        {
            "name": "lastname",
            "typeDefinition": "text"
        },
        {
            "name": "favorite_color",
            "typeDefinition": "text",
        }
    ],
    "primaryKey": {
        "partitionKey": [
            "firstname"
        ],
        "clusteringKey": [
            "lastname"
        ]
    }
})

Using the Collections Client

You can use the Collections client to work with the Astra Document API. API Reference

# astra_client created above
# create multiple documents using the collections API
my_collection = astra_client.namespace(ASTRA_DB_KEYSPACE).collection(COLLECTION_NAME)
my_collection.batch(documents=[
    {
        "first_name": "Dang",
        "last_name": "Son",
    }, {
        "first_name": "Yep",
        "last_name": "Boss",
    }])

Using the GraphQL Client

You can use the GraphQL client to work with the Astra GraphQL API. API Reference

# astra_client created above
# create multiple documents using the GraphQL API
astra_client.gql.execute(keyspace=ASTRA_DB_KEYSPACE, query="""
        mutation insert2Books {
            moby: insertbook(value: {title:"Moby Dick", author:"Herman Melville"}) {
                value {
                    title
                }
            }
            catch22: insertbook(value: {title:"Catch-22", author:"Joseph Heller"}) {
                value {
                    title
                }
            }
        }
    """)

Using the HTTP Client

You can use the HTTP client to work with any Astra/Stargate endpoint directly. API Reference

# astra_client created above
# create a document on Astra using the Document API
astra_client._rest_client.request(
    method="PUT",
    path=f"/api/rest/v2/namespaces/my_namespace/collections/my_collection/user_1",
    json_data={
        "first_name": "Cliff",
        "last_name": "Wicklow",
        "emails": ["cliff.wicklow@example.com"],
    })

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.2.1.tar.gz (9.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: astrapy-0.2.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for astrapy-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c544b0445dfd11eb479c1ef2416015158b62bd0ffa1abff8d22444687cb4b3bb
MD5 60927fcb82a8133cddfe2d7dd65a786c
BLAKE2b-256 0305cc1bf6b1f0dec76a6641e515de7ef18eb9f850b095b3e640a9bacde45c41

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