Skip to content

Instantly share code, notes, and snippets.



Created Jul 15, 2016
What would you like to do?
#!/usr/bin/env python
"""Sample Vitess client in Python.
This is a sample for using the Python Vitess client.
It's a script that inserts some random messages on random pages of the
guestbook sample app.
Before running this, start up a local example cluster as described in the file.
Then run, which sets up PYTHONPATH before running
vitess/examples/local$ ./
import argparse
import random
import time
import uuid
from vtdb import vtgate_client
# register the python gRPC client upon import
from vtdb import grpc_vtgate_client # pylint: disable=unused-import
# Parse args
parser = argparse.ArgumentParser()
parser.add_argument('--server', dest='server', default='vtgate-host:15991')
parser.add_argument('--timeout', dest='timeout', type=float, default='10.0')
args = parser.parse_args()
# Connect
conn = vtgate_client.connect('grpc', args.server, args.timeout)
# Insert some messages on random pages.
print 'Inserting into master...'
cursor = conn.cursor(tablet_type='master', writable=True, keyspace='directory')
for i in range(1000):
page = random.randint(1, 1000000)
tenant_uuid = uuid.uuid4()
tenant_id = tenant_uuid.bytes
# print tenant_uuid
external_user_id = 'a401308c47a511e6beb89e71128cae77'
sync_ts = 20
admin = 1
'insert into user(tenant_id,user_id,provider_id,better_cloud_user_id,external_user_id,external_user_hash,sync_ts,admin)'
' VALUES (:tenant_id, :user_id, :provider_id, :better_cloud_user_id, :external_user_id, :external_user_hash, :sync_ts, :admin)',
'tenant_id': tenant_id,
'user_id': tenant_id,
'provider_id': tenant_id,
'better_cloud_user_id': tenant_id,
'external_user_id': 'a401308c47a511e6beb89e71128cae77',
'external_user_hash': tenant_id,
'sync_ts': 20,
'admin': 1,
if (i % 1000) == 0:
print i
print time.time()
# cursor.close()
# Read it back from the master.
# print 'Reading from master...'
# cursor.execute('SELECT admin FROM user', {})
# for row in cursor.fetchall():
# print row
# Read from a replica.
# Note that this may be behind master due to replication lag.
print 'Reading from replica...'
cursor = conn.cursor(tablet_type='replica', keyspace='directory', shards=['80-','-80'])
cursor.execute('SELECT count(1) FROM user', {})
for row in cursor.fetchall():
print row
# Clean up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment