def test_cursor(self): try: test_sdb1 = client(hostname,service) test_sdb2 = client(hostname,service) cs.create_collection('hello') cs.create_collection('world') try: cursor1 = test_sdb1.list_collections() except Exception as e: print 'list collections error' try: cursor2 = test_sdb2.list_collections() except Exception as e: print 'sdb2 list collections error' try: test_sdb1.close_all_cursors() record = cursor1.next() except Exception as e: print 'the cursor is closed ,so that can not to use the cursor.' record = cursor2.next() except Exception as e: print e finally: cl.delete()
def test_user(self): username = '******' password = '******' sdb = client(hostname, service) #deal with the SDBTypeError try: sdb.create_user(119, password) except SDBTypeError as e: print 'catch the SDBTpyeError' try: #create new user sdb.create_user(username, password) #use new user login sdb_new = client(hostname, service, username, password) if not sdb_new.is_valid(): print 'login failed by using new user!' return except Exception as e: print e finally: sdb.remove_user(username, password) sdb_new.disconnect() sdb.disconnect()
def test_cursor(self): try: test_sdb1 = client(hostname, service) test_sdb2 = client(hostname, service) cs.create_collection('hello') cs.create_collection('world') try: cursor1 = test_sdb1.list_collections() except Exception as e: print 'list collections error' try: cursor2 = test_sdb2.list_collections() except Exception as e: print 'sdb2 list collections error' try: test_sdb1.close_all_cursors() record = cursor1.next() except Exception as e: print 'the cursor is closed ,so that can not to use the cursor.' record = cursor2.next() except Exception as e: print e finally: cl.delete()
def test_user(self): username = '******' password = '******' sdb = client(hostname,service) #deal with the SDBTypeError try: sdb.create_user(119,password) except SDBTypeError as e: print 'catch the SDBTpyeError' try: #create new user sdb.create_user(username,password) #use new user login sdb_new = client(hostname,service,username,password) if not sdb_new.is_valid(): print 'login failed by using new user!' return except Exception as e: print e finally: sdb.remove_user(username,password) sdb_new.disconnect() sdb.disconnect()
def create_sync_managetbl(): try: db = client(hostname, svcport, username, password) cs_domain = {'Domain': sync_manage_domain['domain_name']} for sync_tbl in sdb_sync_tbls: # 1. create main collection space try: cs = db.create_collection_space(sync_tbl['cs_name'], cs_domain) except (SDBBaseError, SDBTypeError), e: print 'ERROR TO CREATE MAIN CS: %s' % e.code cs = db.get_collection_space(sync_tbl['cs_name']) # 2. create main collection maincl_options = sync_tbl_options['tbl_options'] try: maincl = cs.create_collection(sync_tbl['cl_name'], maincl_options) except (SDBBaseError, SDBTypeError), e: print 'ERROR TO CREATE MAIN CL: %s' % e.code maincl = cs.get_collection(sync_tbl['cl_name']) # 3. create sub collection space try: cs = db.create_collection_space(sync_tbl['subcs_name'], cs_domain) except (SDBBaseError, SDBTypeError), e: print 'ERROR TO CREATE SUB CS: %s' % e.code cs = db.get_collection_space(sync_tbl['subcs_name'])
def __init__(self, filename, dblog={}, connect=None, just_write_table=None, mode='a', encoding=None, delay=0): """ Open the specified file and use it as the stream for logging. """ #keep the absolute path, otherwise derived classes which use this #may come a cropper when the current directory changes if codecs is None: encoding = None self.baseFilename = os.path.abspath(filename) self.mode = mode self.encoding = encoding self.delay = delay self.dblog = dblog #self.connect = connect self.just_write_table = just_write_table print "------------------> mode: %s,encoding: %s,delay: %s" % ( mode, encoding, delay) if delay: #We don't open the stream, but we still need to call the #Handler constructor to set level, formatter, lock etc. logging.Handler.__init__(self) self.stream = None else: logging.StreamHandler.__init__(self, self._open()) # SDB collection connection #self.connect = connect self.hostname = connect['HostName'] self.serverport = connect['ServerPort'] self.username = connect['UserName'] self.password = connect['Password'] self.cs_name = connect['CsName'] self.cl_name = connect['ClName'] self.db_hosts = hosts self.connect_hosts = [] for db_host in self.db_hosts.split(','): host_info = db_host.split(':') connect_info = {'host': host_info[0], 'service': host_info[1]} self.connect_hosts.append(connect_info) # make sure the connect is correct self.db = client(self.hostname, self.serverport, self.username, base64.decodestring(self.password)) if '' != self.db_hosts: self.db.connect_to_hosts(self.connect_hosts, user=self.username, password=base64.decodestring( self.password)) # set read from primary attri_options = {'PreferedInstance': 'M'} self.db.set_session_attri(attri_options)
def __initialize(self, csname, host, port, user, password): try: self.__cc = client(host, port, user, password) self.__cs = self.__cc[csname] except SDBBaseError, e: LogError('Error: %s, program exit', e.detail) exit(1)
def __init__(self, cs_name, cl_name): try: db = client("localhost", 11810) cs = db.create_collection_space(cs_name) self.cl = cs.create_collection(cl_name) except (SDBBaseError, Exception) as e: msg = "MySequoiadb() init occure a Exception:{}".format(e) print msg return -10001
def connect(self): """Connect to the current node. Return values: client of current node Exceptions: pysequoiadb.error.SDBBaseError """ return pysequoiadb.client(self.get_hostname(), self.get_servicename())
def user( username, password ): print '---begin to create new user' db.create_user(username,password) print '---begin to login by new user' db_new = client(hostname,service,username,password) print '---begin to remove new user' db.remove_user(username,password)
def user(username, password): print '---begin to create new user' db.create_user(username, password) print '---begin to login by new user' db_new = client(hostname, service, username, password) print '---begin to remove new user' db.remove_user(username, password)
def __init__(self, hostname='localhost', srvport=11810, user='******', passwd='kfptSDB2016'): self.hostname = hostname self.srvport = srvport self.user = user self.passwd = passwd self.db = client(hostname, srvport, user, passwd) self.domain = 'mdm_domain'
def closeCursor(): print '---begin to close all cursor' dbTmp = client( hostname, service ) rc = dbTmp.list_collections() dbTmp.close_all_cursors() hasErr = False try: rc.current() except ( Exception ), e: hasErr = True
def update_sparksql(cs_name, cl_name, ruler, matcher): try: db = client(host_name, server_port, user_name, base64.decodestring(password)) db.set_session_attri({'PreferedInstance': 'M'}) cs = db.get_collection_space(cs_name) cl = cs.get_collection(cl_name) cl.update(rule=ruler, condition=matcher) except (SDBBaseError, SDBTypeError), e: print 'error info: %s, error code: %s' % (pysequoiadb.error, e.code) raise
def test_user(self): username = '******' password = '******' sdb = client(hostname,service) #deal with the SDBTypeError try: sdb.create_user("119",password) except SDBTypeError as e: print e except SDBBaseError, e: print e.detail
def test_user(self): username = '******' password = '******' sdb = client(hostname, service) #deal with the SDBTypeError try: sdb.create_user("119", password) except SDBTypeError as e: print e except SDBBaseError, e: print e.detail
def initialize(): #global object global sdb,cs,cl,range_cl,hash_cl,percent_cl,option try: sdb = client(hostname,service) #print 'connection' cs = sdb.create_collection_space(cs_global,0) cl = cs.create_collection(cl_global,option) #rc,cs = sdb.get_collection_space(cs_global) #rc,cl = cs.get_collection(cl_global) range_cl = cs.create_collection(range_name,range_option) hash_cl = cs.create_collection(hash_name,hash_option) percent_cl = cs.create_collection(percent_name,percent_option) except Exception as e: print 'Error' + e
def initialize(): #global object global sdb, cs, cl, range_cl, hash_cl, percent_cl, option try: sdb = client(hostname, service) #print 'connection' cs = sdb.create_collection_space(cs_global, 0) cl = cs.create_collection(cl_global, option) #rc,cs = sdb.get_collection_space(cs_global) #rc,cl = cs.get_collection(cl_global) range_cl = cs.create_collection(range_name, range_option) hash_cl = cs.create_collection(hash_name, hash_option) percent_cl = cs.create_collection(percent_name, percent_option) except SDBBaseError, e: print 'Error' + e.detail
def flush_main(): # 集合空间与集合 cs_name = 'nlsync' cl_name = 'config' # 1.根据配置文件sync_config.py内的信息将其刷入同步批次初始化配置表(sync.config) try: # create synchronize data create_sync_managetbl() db = client(hostname, svcport, username, password) try: cs = db.get_collection_space(cs_name) except (SDBBaseError, SDBTypeError), e: print 'error code: %s' % e.code raise try: cl = cs.get_collection(cl_name) except (SDBBaseError, SDBTypeError), e: print 'error code: %s' % e.code raise
def __init__(self, log_handler): self.hostname = host_name self.svcport = server_port self.username = user_name self.password = password self.config_cs = sync_config_cs self.config_cl = sync_config_cl self.mdm_metahis_cs = mdm_metahis_cs self.mdm_metahis_cl = mdm_metahis_cl self.log = log_handler self.prefetch_process_num = prefetch_process_num self.prefetch_one_day = prefetch_one_day self.config_rd = dict() self.total_times = 3 self.wait_sleep_time = 1 # SDB collection connection self.db_hosts = hosts self.connect_hosts = [] for db_host in self.db_hosts.split(','): host_info = db_host.split(':') connect_info = {'host': host_info[0], 'service': host_info[1]} self.connect_hosts.append(connect_info) # make sure the connect is correct self.db = client(self.hostname, self.svcport, self.username, base64.decodestring(self.password)) if '' != self.db_hosts: self.db.connect_to_hosts(self.connect_hosts, user=self.username, password=base64.decodestring( self.password)) # set read from primary attri_options = {'PreferedInstance': 'M'} self.db.set_session_attri(attri_options)
def __init__(self, hostname='localhost', svcport='11810', username='', password='', connect_hosts='', log_handler=None): self.hostname = hostname self.svcport = svcport self.username = username self.password = password self.db = client(self.hostname, self.svcport, self.username, base64.decodestring(self.password)) # make sure the connect is correct if '' != connect_hosts: self.db.connect_to_hosts(connect_hosts, user=self.username, password=base64.decodestring( self.password)) # set read from primary attri_options = {'PreferedInstance': 'M'} self.db.set_session_attri(attri_options) self.log = log_handler self.SDB_SNAP_CATALOG = 8
#! /usr/bin/python import pysequoiadb from pysequoiadb import client from pysequoiadb import const from pysequoiadb import lob from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) from bson.objectid import ObjectId if __name__ == "__main__": try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # insert lob bin = "1asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfsdfaasdf" obj = cl.create_lob() obj.write(bin, 30) oid = obj.get_oid() obj.close() cr = cl.list_lobs() while True:
import pysequoiadb from pysequoiadb import client from pysequoiadb import collectionspace from pysequoiadb import collection from pysequoiadb import cursor from pysequoiadb import const from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) if __name__ == "__main__": try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 50000) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize":0}) # insert single record for i in range(0, 10): basketball = {"Item":"basketball", "id":i} cl.insert(basketball) cr = cl.query_and_update({"$set":{"id":1000}}, {"id": 1}, return_new=True) while True: try: record = cr.next() pysequoiadb._print(record)
def test_partition(self): try: #range partition cursor = sdb.get_snapshot( 4, condition={'Name': { '$et': cs_global + '.' + range_name }}) #rc,record = cursor.next() while True: try: record = cursor.next() if (0 == cmp(record['Details'][0]['GroupName'], 'datagroup')): src = 'datagroup' dst = 'datagroup1' break else: src = 'datagroup1' dst = 'datagroup' break #rc,record = cursor.next() except SDBEndOfCursor: break except SDBBaseError: raise print 'src=' + src + ',dst=' + dst for i in range(0, 100): range_cl.insert({'age': i, 'name': 'mike' + str(i)}) range_cl.split_by_condition(src, dst, {'age': 10}, {'age': 60}) time.sleep(3) temp_sdb = client(hostname, data_port) temp_cs = temp_sdb.get_collection_space(cs_global) temp_cl = temp_cs.get_collection(range_name) count = temp_cl.get_count() print 'count=' + count.__str__() if count != 50: print 'range partion failed!' #hash partition cursor = sdb.get_snapshot( 4, condition={'Name': { '$et': cs_global + '.' + hash_name }}) #rc,record = cursor.next() while True: try: record = cursor.next() if (0 == cmp(record['Details'][0]['GroupName'], 'datagroup')): src = 'datagroup' dst = 'datagroup1' break else: src = 'datagroup1' dst = 'datagroup' break except SDBEndOfCursor: break except SDBBaseError: raise print 'src=' + src + ',dst=' + dst for i in range(0, 100): hash_cl.insert({'age': i, 'name': 'mike' + str(i)}) hash_cl.split_by_condition(src, dst, {'Partition': 4}, {'Partition': 8}) temp_cl = temp_cs.get_collection(hash_name) count = temp_cl.get_count() print 'count=' + count.__str__() if count != 57 and count != 43: print 'hash partion failed!' #precent cursor = sdb.get_snapshot( 4, condition={'Name': { '$et': cs_global + '.' + percent_name }}) #rc,record = cursor.next() while True: try: record = cursor.next() if (0 == cmp(record['Details'][0]['GroupName'], 'datagroup')): src = 'datagroup' dst = 'datagroup1' break else: src = 'datagroup1' dst = 'datagroup' break except SDBEndOfCursor: break except SDBBaseError: raise print 'src=' + src + ',dst=' + dst for i in range(0, 100): percent_cl.insert({'age': i, 'name': 'mike' + str(i)}) percent_cl.split_by_percent(src, dst, 50.0) temp_cl = temp_cs.get_collection(percent_name) count = temp_cl.get_count() print 'count=' + count.__str__() if count != 50: print 'percent partion failed!' except Exception as e: print e finally: range_cl.delete() hash_cl.delete() percent_cl.delete()
}, { "name": "f**k", "key": dmn }, { "name": "the", "key": dzero }, { "name": "decimal", "key": dn }, ] db = client("localhost", 50000) db.create_collection_space("foo") db.foo.create_collection("bar") db.foo.bar.insert(doc) print('insert a doc: %r' % doc) db.foo.bar.bulk_insert(0, docs) print('insert: %r' % docs) cr = db.foo.bar.query() try: while True: record = cr.next()
def test_connection(self): sdb = client(hostname, service) cs = None if not sdb.is_valid(): print 'create connection failed!' return hosts = [ { 'host': '192.168.10.30', 'service': 11200, }, { 'host': '192.168.10.30', 'service': 11200, }, { 'host': hostname, 'service': service, }, ] sdb.connect_to_hosts(hosts, user="", password="", policy="random") sdb.connect(hostname, service, user="", password="") try: cs = sdb.create_collection_space(cs_name, 0) test_cs = sdb.get_collection_space(cs_name) if (0 != cmp(cs.get_collection_space_name(), test_cs.get_collection_space_name())): print 'get_collection_space failed!' return cl = cs.create_collection(cl_name) test_cl = cs.get_collection(cl_name) if (0 != cmp(cl.get_collection_name(), test_cl.get_collection_name())): print 'get_collection failed!' return #insert data data1 = {'age': 1, 'name': 'tom'} cl.insert(data1) time.sleep(0.5) insert_sql = 'insert into ' + cs_name + '.' + cl_name + '(age,name) values(24,\'kate\')' sdb.exec_update(insert_sql) time.sleep(0.5) #update data cs = sdb.get_collection_space(cs_name) cl = cs.get_collection(cl_name) update_sql = 'update ' + cs_name + '.' + cl_name + ' set name = \'tom_new\' where age = 1' sdb.exec_update(update_sql) rule = {'$set': {'name': 'kate_new'}} cond = {'age': {'$et': 24}} cl.update(rule, condition=cond) time.sleep(0.5) #search data cursor = cl.query(condition=cond) #rc,record = cursor.next() while True: try: record = cursor.next() if (0 != cmp(record['name'], 'kate_new')): print 'search data failed1!' return except SDBEndOfCursor: break except SDBBaseError: raise select_sql = 'select name from ' + cs_name + '.' + cl_name + ' where age=1' cursor = sdb.exec_sql(select_sql) #rc,record = cursor.next() while True: try: record = cursor.next() if (0 != cmp(record['name'], 'tom_new')): print 'search data failed2!' return except SDBEndOfCursor: break except SDBBaseError: raise #delete data delete_sql = 'delete from ' + cs_name + '.' + cl_name + ' where age=1' sdb.exec_update(delete_sql) cl.delete(condition=cond) time.sleep(0.5) count = cl.get_count() if 0 != count: print 'delete data failed!' return except Exception as e: print e finally: sdb.drop_collection_space(cs_name) time.sleep(0.5) if sdb.is_valid(): sdb.disconnect() time.sleep(1) if sdb.is_valid(): print 'disconnect is failed!'
class TestPySequoiadb(object): """create connection,check the connection,create cs and create cl insert data,update data,search data and delete data,drop cl and drop cs,close connectiontion,check the connection again. """ def test_connection(self): sdb = client(hostname, service) cs = None if not sdb.is_valid(): print 'create connection failed!' return hosts = [ { 'host': '192.168.10.30', 'service': 11200, }, { 'host': '192.168.10.30', 'service': 11200, }, { 'host': hostname, 'service': service, }, ] sdb.connect_to_hosts(hosts, user="", password="", policy="random") sdb.connect(hostname, service, user="", password="") try: cs = sdb.create_collection_space(cs_name, 0) test_cs = sdb.get_collection_space(cs_name) if (0 != cmp(cs.get_collection_space_name(), test_cs.get_collection_space_name())): print 'get_collection_space failed!' return cl = cs.create_collection(cl_name) test_cl = cs.get_collection(cl_name) if (0 != cmp(cl.get_collection_name(), test_cl.get_collection_name())): print 'get_collection failed!' return #insert data data1 = {'age': 1, 'name': 'tom'} cl.insert(data1) time.sleep(0.5) insert_sql = 'insert into ' + cs_name + '.' + cl_name + '(age,name) values(24,\'kate\')' sdb.exec_update(insert_sql) time.sleep(0.5) #update data cs = sdb.get_collection_space(cs_name) cl = cs.get_collection(cl_name) update_sql = 'update ' + cs_name + '.' + cl_name + ' set name = \'tom_new\' where age = 1' sdb.exec_update(update_sql) rule = {'$set': {'name': 'kate_new'}} cond = {'age': {'$et': 24}} cl.update(rule, condition=cond) time.sleep(0.5) #search data cursor = cl.query(condition=cond) #rc,record = cursor.next() while True: try: record = cursor.next() if (0 != cmp(record['name'], 'kate_new')): print 'search data failed1!' return except SDBEndOfCursor: break except SDBBaseError: raise select_sql = 'select name from ' + cs_name + '.' + cl_name + ' where age=1' cursor = sdb.exec_sql(select_sql) #rc,record = cursor.next() while True: try: record = cursor.next() if (0 != cmp(record['name'], 'tom_new')): print 'search data failed2!' return except SDBEndOfCursor: break except SDBBaseError: raise #delete data delete_sql = 'delete from ' + cs_name + '.' + cl_name + ' where age=1' sdb.exec_update(delete_sql) cl.delete(condition=cond) time.sleep(0.5) count = cl.get_count() if 0 != count: print 'delete data failed!' return except Exception as e: print e finally: sdb.drop_collection_space(cs_name) time.sleep(0.5) if sdb.is_valid(): sdb.disconnect() time.sleep(1) if sdb.is_valid(): print 'disconnect is failed!' """create user and use the new user login then delete the user """ def test_user(self): username = '******' password = '******' sdb = client(hostname, service) #deal with the SDBTypeError try: sdb.create_user("119", password) except SDBTypeError as e: print e except SDBBaseError, e: print e.detail try: #create new user sdb.create_user(username, password) #use new user login sdb_new = client(hostname, service, username, password) if not sdb_new.is_valid(): print 'login failed by using new user!' return except Exception as e: print e finally: sdb.remove_user(username, password) sdb_new.disconnect() sdb.disconnect()
#! /usr/bin/python import pysequoiadb from pysequoiadb import client from pysequoiadb import const from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) if "__main__" == __name__: # connect to local db, using default args value. # host= 'localhost', port= 11810, user= '', password= '' try: db_default = client() pysequoiadb._print( db_default ) del db_default except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e) # connect to db, using default args value. # host= '192.168.20.111', port= 11810, user= '', password= '' # 192.168.20.111 is not a valid try: db_to_1 = client('192.168.20.48', 11810, '', '') del db_to_1 except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e) # connect to db, using default args value. # host= 'localhost', port= 11810, user= '', password= ''
#! /usr/bin/python import pysequoiadb from pysequoiadb import client from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) if "__main__" == __name__: # connect to local db, using default args value. # host= 'localhost', port= 11810, user= '', password= '' try: db_default = client() print(db_default) del db_default except (SDBTypeError, SDBBaseError) as e: print(e) # connect to db, using default args value. # host= '192.168.20.111', port= 11810, user= '', password= '' # 192.168.20.111 is not a valid try: db_to_1 = client('192.168.20.48', 11810, '', '') del db_to_1 except (SDBTypeError, SDBBaseError) as e: print(e) # connect to db, using default args value. # host= 'localhost', port= 11810, user= '', password= '' try: db = client("ubuntu-dev9", 11810)
try: db.drop_collection_space(cs_name) except SDBBaseError, e: if (-34 != e.code): pysequoiadb._print(e.detail) raise e if __name__ == "__main__": try: parse_option() cs_name = "pydriver_lob_cs" cl_name = "pydriver_lob_cl" db = client(hostname, service) cl = createCL(cs_name, cl_name) oid1 = bson.ObjectId("5448a5181c3eb9e00b000001") createLobWithOid(cl, oid1) oid2 = createLobWithoutOid(cl) listLob(cl) getLob(cl, oid1) removeLob(cl, oid1, oid2) clean(cs_name) except SDBBaseError, e: pysequoiadb._print(e.detail) raise e finally: if (locals().has_key('db')):
print '-p arg coord_port' def clean( cs_name ): print '---begin to drop cs in finally' try: db.drop_collection_space(cs_name) except SDBBaseError, e: if ( -34 != e.code ): pysequoiadb._print(e.detail) raise e if __name__ == "__main__": try: parse_option() db = client( hostname, service ) # main cs_name = "pydriver_cscltest_cs" cl_name = "pydriver_cscltest_cl" print '---begin to create cs and get cs' db.create_collection_space( cs_name ) cs = db.get_collection_space( cs_name ) print '---begin to create existed cs' hasErr33 = False try: db.create_collection_space( cs_name ) except SDBBaseError, e: if ( -33 == e.code ):
import pysequoiadb from pysequoiadb import client from pysequoiadb import const from pysequoiadb import lob from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) from bson.objectid import ObjectId if __name__ == "__main__": try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 11810) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize":0}) # insert lob bin = "1asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfsdfaasdf" obj = cl.create_lob() obj.write( bin, 30 ) oid = obj.get_oid() obj.close() cr = cl.list_lobs() while True:
import pysequoiadb from pysequoiadb import client from pysequoiadb import collectionspace from pysequoiadb import collection from pysequoiadb import cursor from pysequoiadb import const from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) if __name__ == "__main__": try: # connect to local db, using default args value. # host= '192.168.20.48', port= 11810, user= '', password= '' db = client("192.168.20.48", 50000) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # insert single record for i in range(0, 10): basketball = {"Item": "basketball", "id": i} cl.insert(basketball) cr = cl.query_and_update({"$set": { "id": 1000 }}, {"id": 1}, return_new=True) while True: try: record = cr.next()
def test_connection(self): sdb = client(hostname,service) cs = None if not sdb.is_valid(): print 'create connection failed!' return hosts = [{'host':'192.168.10.30','service':11200,}, {'host':'192.168.10.30','service':11200,}, {'host':hostname,'service':service,},] sdb.connect_to_hosts(hosts,user="",password="",policy="random") sdb.connect(hostname,service,user="",password="") try: cs = sdb.create_collection_space(cs_name,0) test_cs = sdb.get_collection_space(cs_name) if(0!=cmp(cs.get_collection_space_name(),test_cs.get_collection_space_name())): print 'get_collection_space failed!' return cl = cs.create_collection(cl_name) test_cl = cs.get_collection(cl_name) if(0 !=cmp(cl.get_collection_name(),test_cl.get_collection_name())): print 'get_collection failed!' return #insert data data1 = {'age':1,'name':'tom'} cl.insert(data1) time.sleep(0.5) insert_sql = 'insert into '+cs_name+'.'+cl_name+'(age,name) values(24,\'kate\')' sdb.exec_update(insert_sql) time.sleep(0.5) #update data cs = sdb.get_collection_space(cs_name) cl = cs.get_collection(cl_name) update_sql = 'update '+cs_name+'.'+cl_name+' set name = \'tom_new\' where age = 1' sdb.exec_update(update_sql) rule = {'$set':{'name':'kate_new'}} cond = {'age':{'$et':24}} cl.update(rule,condition=cond) time.sleep(0.5) #search data cursor = cl.query(condition=cond) #rc,record = cursor.next() while True: try: record = cursor.next() if (0!=cmp(record['name'],'kate_new')): print 'search data failed1!' return except SDBEndOfCursor : break except SDBBaseError : raise select_sql = 'select name from '+cs_name+'.'+cl_name+' where age=1' cursor = sdb.exec_sql(select_sql) #rc,record = cursor.next() while True: try: record = cursor.next() if (0!=cmp(record['name'],'tom_new')): print 'search data failed2!' return except SDBEndOfCursor : break except SDBBaseError : raise #delete data delete_sql = 'delete from '+cs_name+'.'+cl_name +' where age=1' sdb.exec_update(delete_sql) cl.delete(condition=cond) time.sleep(0.5) count = cl.get_count() if 0 != count: print 'delete data failed!' return except Exception as e: print e finally: sdb.drop_collection_space(cs_name) time.sleep(0.5) if sdb.is_valid(): sdb.disconnect() time.sleep(1) if sdb.is_valid(): print 'disconnect is failed!'
import logging import logging.config from spark_restart import * import time def query_sparksql(cs_name, cl_name, condition={}, selector={}, order_by={}, hint={}, skip=0L, num_to_return=-1L): try: db = client(host_name, server_port, user_name, base64.decodestring(password)) db.set_session_attri({'PreferedInstance': 'M'}) cs = db.get_collection_space(cs_name) cl = cs.get_collection(cl_name) cursor = cl.query(condition=condition, selector=selector, order_by=order_by, hint=hint, skip=skip, num_to_return=num_to_return) records = [] while True: try: record = cursor.next() records.append(record) except SDBEndOfCursor:
import pysequoiadb from pysequoiadb import client from pysequoiadb import lob from pysequoiadb.error import (SDBTypeError, SDBBaseError, SDBEndOfCursor) from bson.objectid import ObjectId if __name__ == "__main__": try: # connect to local db, using default args value. host = 'localhost' port = 11810 # user= '', password= '' db = client(host, port) cs_name = "gymnasium" cs = db.create_collection_space(cs_name) cl_name = "sports" cl = cs.create_collection(cl_name, {"ReplSize": 0}) # insert lob bin = "1asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfsdfaasdf" obj = cl.create_lob() obj.write(bin, 30) oid = obj.get_oid() obj.close() cr = cl.list_lobs() while True:
def connect(self): self.db = client(host=self.host, service=self.service, user=self.user, psw=self.password)
def test_partition(self): try: #range partition cursor = sdb.get_snapshot(4,condition={'Name':{'$et':cs_global+'.'+range_name}}) #rc,record = cursor.next() while True: try: record = cursor.next() if( 0 == cmp( record['Details'][0]['GroupName'],'datagroup')): src = 'datagroup' dst = 'datagroup1' break else: src = 'datagroup1' dst = 'datagroup' break #rc,record = cursor.next() except SDBEndOfCursor : break except SDBBaseError : raise print 'src='+src+',dst='+dst for i in range(0,100): range_cl.insert({'age':i,'name':'mike'+str(i)}) range_cl.split_by_condition(src,dst,{'age':10},{'age':60}) time.sleep(3) temp_sdb = client(hostname,data_port) temp_cs = temp_sdb.get_collection_space(cs_global) temp_cl = temp_cs.get_collection(range_name) count = temp_cl.get_count() print 'count='+count.__str__() if count != 50: print 'range partion failed!' #hash partition cursor = sdb.get_snapshot(4,condition={'Name':{'$et':cs_global+'.'+hash_name}}) #rc,record = cursor.next() while True: try: record = cursor.next() if( 0 == cmp( record['Details'][0]['GroupName'],'datagroup')): src = 'datagroup' dst = 'datagroup1' break else: src = 'datagroup1' dst = 'datagroup' break except SDBEndOfCursor : break except SDBBaseError : raise print 'src='+src+',dst='+dst for i in range(0,100): hash_cl.insert({'age':i,'name':'mike'+str(i)}) hash_cl.split_by_condition(src,dst,{'Partition':4},{'Partition':8}) temp_cl = temp_cs.get_collection(hash_name) count = temp_cl.get_count() print 'count='+count.__str__() if count != 57 and count != 43: print 'hash partion failed!' #precent cursor = sdb.get_snapshot(4,condition={'Name':{'$et':cs_global+'.'+percent_name}}) #rc,record = cursor.next() while True: try: record = cursor.next() if( 0 == cmp( record['Details'][0]['GroupName'],'datagroup')): src = 'datagroup' dst = 'datagroup1' break else: src = 'datagroup1' dst = 'datagroup' break except SDBEndOfCursor : break except SDBBaseError : raise print 'src='+src+',dst='+dst for i in range(0,100): percent_cl.insert({'age':i,'name':'mike'+str(i)}) percent_cl.split_by_percent(src,dst,50.0) temp_cl = temp_cs.get_collection(percent_name) count = temp_cl.get_count() print 'count='+count.__str__() if count != 50: print 'percent partion failed!' except Exception as e: print e finally: range_cl.delete() hash_cl.delete() percent_cl.delete()