#!/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 | |
README.md file. | |
Then run client.sh, which sets up PYTHONPATH before running client.py: | |
vitess/examples/local$ ./client.sh | |
""" | |
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) | |
try: | |
# 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 | |
cursor.begin() | |
cursor.execute( | |
'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, | |
}) | |
cursor.commit() | |
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 | |
cursor.close() | |
finally: | |
# Clean up | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment