CompanySeptember 13, 2016

Python Driver 3.7.0 Released

Alan Boudreault
Alan Boudreault
Python Driver 3.7.0 Released
from cassandra.cluster import Cluster, ExecutionProfile
from cassandra.policies import ConstantSpeculativeExecutionPolicy
from cassandra.query import SimpleStatement
cluster = Cluster()
# send a new request every 100ms for a maximum of 10 attempts
ep = ExecutionProfile(speculative_execution_policy=ConstantSpeculativeExecutionPolicy(.1, 10))
cluster.add_execution_profile('my_app_ep', ep)
session = cluster.connect('test')
statement = SimpleStatement("SELECT i FROM d WHERE k = 0", is_idempotent=True)
result = session.execute(statement, execution_profile='my_app_ep')
query = "SELECT * FROM users"
statement = SimpleStatement(query, fetch_size=10)
results = session.execute(statement)
# save the paging_state somewhere...
session['paging_state'] = results.paging_state
# and use it later to resume the pagination
query = "SELECT * FROM users"
statement = SimpleStatement(query, fetch_size=10)
paging_state = session['paging_state']
results = session.execute(statement, paging_state=paging_state)
from cassandra.cluster import Cluster
from cassandra.policies import EC2MultiRegionTranslator
cluster = Cluster([''], address_translator=EC2MultiRegionTranslator())
session = cluster.connect()
# do stuff...
from cassandra.cqlengine import connection
# ...
CONNS = ['cluster1', 'cluster2']
KEYSPACES = ('client1', 'client2', 'client3', 'client4')
connection.register_connection('cluster1', [''], default=True)
connection.register_connection('cluster2', [''], lazy_connect=True)
for keyspace in KEYSPACES:
    keyspace_simple(keyspace, 3, connections=CONNS)
class Automobile(Model):
    __connection__ = 'cluster2'  # default connection per model
    manufacturer = columns.Text(primary_key=True)
    year = columns.Integer(primary_key=True)
    model = columns.Text()
# sync the table for all connections and keyspaces
sync_table(Automobile, KEYSPACES, CONNS)
# Select the connection and keyspace via the ContextQuery
with ContextQuery(Automobile, connection='cluster1' keyspace='client2') as A:
    A.objects.create(manufacturer='honda', year=2004, model='civic')
# Read from the default model connection 'cluster2'
print len(Automobile.objects.using(keyspace='client2').get(manufacturer='honda', year=2004))  # 0
# Select the connection and keyspace on the fly
print len(Automobile.objects.using(connection='cluster1', keyspace='client2').all())  # 1
# Select on the model instance
a = Automobile.objects.using(connection='cluster1',keyspace='client2').get(manufacturer='honda', year=2004)
a.using('cluster2').save()  # save on cluster2 rather than cluster1
# Connection select with a BatchQuery
with BatchQuery(connection='cluster1' keyspace='client4') as b:
    A.objects.batch(b).create(manufacturer='honda', year=2004, model='civic')
    A.objects.batch(b).create(manufacturer='honda', year=2005, model='civic')
    A.objects.batch(b).create(manufacturer='honda', year=2006, model='civic')
Discover more

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.