def __init__(self, host, port, keyspace, user=None, password=None): """ Params: * host .........: hostname of Cassandra node. * port .........: port number to connect to. * keyspace .....: keyspace to connect to. * user .........: username used in authentication (optional). * password .....: password used in authentication (optional). """ self.host = host self.port = port self.keyspace = keyspace socket = TSocket.TSocket(host, port) self.transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(self.transport) self.client = Cassandra.Client(protocol) socket.open() self.open_socket = True if user and password: credentials = {"username": user, "password": password} self.client.login(AuthenticationRequest(credentials=credentials)) if keyspace: c = self.cursor() c.execute('USE %s;' % keyspace) c.close()
def get_client(host='127.0.0.1', port=9160): socket = TSocket.TSocket(host, port) if options.unframed: transport = TTransport.TBufferedTransport(socket) else: transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) client = Cassandra.Client(protocol) client.transport = transport return client
def __init__(self): host = '127.0.0.1' port = 9160 self.keyspace = 'Agitmemnon' self.revtree = {} socket = TSocket.TSocket(host, port) transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) self.client = Cassandra.Client(protocol) transport.open()
def _cassandraDeleteColumn(params): try: cassandra = _cassandraConnect(params) cfcol = Cassandra.ColumnPath(params["cf"], None, params['columnName']) cassandra['client'].remove(params['table'], params['row'], cfcol, _nowAsCassandraTimestamp(), 1) cassandra['transport'].close() s = "Column deleted. (You can delete this window, and refresh the launcher window contents)<ul><li>table: %s</li><li>Row: %s</li><li>CF:Col: %s</li>" %\ (params['table'], params['row'], cfcol) return s except Thrift.TException, tx: print '%s' % (tx.message) return 'Thrift Error: %s' % (tx.message)
def open(self, set_keyspace=False, login=False): if self.transport == None: # Create the client connection to the Cassandra daemon socket = TSocket.TSocket(self.host, int(self.port)) transport = TTransport.TFramedTransport(TTransport.TBufferedTransport(socket)) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) transport.open() self.transport = transport self.client = Cassandra.Client(protocol) if login: self.login() if set_keyspace: self.set_keyspace()
def _cassandraInsert(params, colName, columnValue): try: cassandra = _cassandraConnect(params) cassandra['client'].insert( params['table'], params['row'], Cassandra.ColumnPath(params["cf"], None, colName), columnValue, _nowAsCassandraTimestamp(), 1) cassandra['transport'].close() return "Column '%s' inserted" % escape(colName) except Thrift.TException, tx: print '%s' % (tx.message) return 'Error inserting column - Thrift Error: %s' % (tx.message)
def _cassandraConnect(params): # Make socket transport = TSocket.TSocket(params['host'], params['port']) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder #lient = Calculator.Client(protocol) client = Cassandra.Client(protocol) # Connect! transport.open() return {'client': client, 'transport': transport}
def _cassandraGetColumn(params): try: cassandra = _cassandraConnect(params) pp = pprint.PrettyPrinter(indent=2) result = cassandra['client'].get( params['table'], params['row'], Cassandra.ColumnPath(params["cf"], None, params['open']), 1) col = result.column # Close! cassandra['transport'].close() s = "" # Headers s += "Column: %s<br/>\nTimestamp: %s - <span class=\"parsedts\">%s</span>" % \ (col.name, col.timestamp, _cassandraTimestampToDatetime(col.timestamp)) jsonObj = None error = "" try: jsonObj = json.loads(col.value) except ValueError, exc: error = str(exc) # Colorful pretty printed if jsonObj != None: s += "<h3>Data is a json object!</h3>\n<div class=\"columnData\"><tt>\n" + cutejson.cutejson( jsonObj) + "\n</tt></div>" else: s += "<br/><br/>(Not a valid json string) - " + str(error) # Plain data s += "<h3>Data</h3><div class=\"columnData\">%s</div><br/>" % escape( col.value) if jsonObj != None: s += "<h3>By Python prettyprinter:</h3><div class=\"columnData\"><pre>%s</pre></div>" % json.dumps( json.loads(col.value), indent=4, sort_keys=True) # s += "Formatted json:<div class=\"columnData\"><pre>%s</pre></div>" % pp.pformat(json.loads(result.value)) return s
def create_client_transport(server, framed_transport, timeout, logins): host, port = server.split(":") socket = TSocket.TSocket(host, int(port)) if timeout is not None: socket.setTimeout(timeout * 1000.0) if framed_transport: transport = TTransport.TFramedTransport(socket) else: transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) client = Cassandra.Client(protocol) transport.open() if logins is not None: for keyspace, credentials in logins.iteritems(): request = AuthenticationRequest(credentials=credentials) client.login(keyspace, request) return client, transport
def __init__(self, host, port=9160, keyspace=None, username=None, password=None, decoder=None): """ Params: * host .........: hostname of Cassandra node. * port .........: port number to connect to (optional). * keyspace .....: keyspace name (optional). * username .....: username used in authentication (optional). * password .....: password used in authentication (optional). * decoder ......: result decoder instance (optional, defaults to none). """ socket = TSocket.TSocket(host, port) self.transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(self.transport) self.client = Cassandra.Client(protocol) socket.open() # XXX: "current" is probably a misnomer. self._cur_keyspace = None self._cur_column_family = None if username and password: credentials = {"username": username, "password": password} self.client.login(AuthenticationRequest(credentials=credentials)) if keyspace: self.execute('USE %s;' % keyspace) self._cur_keyspace = keyspace if not decoder: self.decoder = SchemaDecoder(self.__get_schema()) else: self.decoder = decoder
def __init__(self): self.isTextAvailable = False self.db = Cassandra() self.imagePages = [] self.totalPages = 0 self.Results = Results_generator()
def __init__( self, env, cassandra_sys, client_name, client_id, num_instances, think_time, monitor_interval, # basic attr req_sz, reply_sz, msg_pass_req_sz, msg_pass_reply_sz, # network size read_size, write_size, # io size replication_factor, write_consistency, read_consitency, work_consistency, # consistency level access_pn_list, access_vn_list, common_stg_cpu_time, proc_stg_name_list, key_distribution, key_args, load_balance_policy='round_robin', log_file=sys.stdout): super(CassandraClient, self).__init__(env, client_name, client_id, num_instances, think_time, monitor_interval, log_file) self.cassandra_sys = cassandra_sys self.req_sz = req_sz self.reply_sz = reply_sz self.msg_pass_req_sz = msg_pass_req_sz self.msg_pass_reply_sz = msg_pass_reply_sz self.read_sz = read_size self.write_sz = write_size self.replication_factor = replication_factor self.write_consistency = write_consistency self.read_consistency = read_consitency self.work_consistency = work_consistency self.consistency_access_sum = self.get_consistency_access_sum() self.access_pn_list = access_pn_list self.access_vn_list = access_vn_list self.proc_stg_name_list = proc_stg_name_list self.per_stg_cpu_time_dict = { key: common_stg_cpu_time for key in Cassandra.get_all_stage_name_list() } self._cassandra_req_attr_list = \ ['env', 'client_id', 'req_sz', 'reply_sz', 'msg_pass_req_sz', 'msg_pass_reply_sz', 'read_sz', 'write_sz', 'replication_factor', 'write_consistency', 'read_consistency', 'consistency_access_sum', 'access_vn_list', 'access_pn_list', 'per_stg_cpu_time_dict', 'proc_stg_name_list'] self.load_balance_policy = load_balance_policy self.load_balancer = LoadBalancePolicy.create_load_balance_policy( self.load_balance_policy, access_pn_list) self.key_distribution = key_distribution self.key_generator = KeyGenerator.create_key_generator( self.key_distribution, key_args)
def get_client(env, cassandra_sys, config, section, client_log=sys.stdout): assert section in config.sections() try: # basic attr client_id = config.getint(section, 'client_id') client_name = config.get(section, 'client_name') num_instances = config.getint(section, 'num_instances') think_time = config.getfloat(section, 'think_time') monitor_interval = config.getfloat(section, 'monitor_interval') # network usage req_sz = config.getfloat(section, 'req_sz') reply_sz = config.getfloat(section, 'reply_sz') msg_pass_req_sz = config.getfloat(section, 'msg_pass_req_sz') msg_pass_reply_sz = config.getfloat(section, 'msg_pass_reply_sz') # io usage read_sz = config.getfloat(section, 'read_sz') write_sz = config.getfloat(section, 'write_sz') # consistency level r_factor = config.getint(section, 'replication_factor') w_consistency = config.get(section, 'write_consistency') r_consistency = config.get(section, 'read_consistency') work_consistency = config.get( section, config.get(section, 'work_consistency')) # access list access_pn_list = ast.literal_eval( config.get(section, 'access_pn_list')) access_vn_list = ast.literal_eval( config.get(section, 'access_vn_list')) # access pattern load_balance_policy = config.get(section, 'load_balance_policy') # key distribution key_distribution = config.get(section, 'key_distribution') if 'key_distribution_param_list' in config.options(section): key_args = ast.literal_eval( config.get(section, 'key_distribution_param_list')) print 'key_distribution_param_list:' + str(key_args) else: key_args = [sys.maxint] # key_args = [sys.maxint] # TODO: read args from config file, now use default # cpu time common_stg_cpu_time = config.getfloat(section, 'common_stg_cpu_time') # proc stages proc_stg_name_list = ast.literal_eval( config.get(section, 'proc_stg_name_list')) except ConfigParser.NoOptionError as error: raise ConfigError("client " + error.section + " configuration not complete, no option: " + error.option) client = CassandraClient(env, cassandra_sys, client_name, client_id, num_instances, think_time, monitor_interval, req_sz, reply_sz, msg_pass_req_sz, msg_pass_reply_sz, read_sz, write_sz, r_factor, w_consistency, r_consistency, work_consistency, access_pn_list, access_vn_list, common_stg_cpu_time, proc_stg_name_list, key_distribution, key_args, load_balance_policy=load_balance_policy, log_file=client_log) # handle optional configuration options = config.options(section) for option in options: if 'cpu_time' in option and 'common' not in option: cur_stg_name = option.replace('_cpu_time', '') assert Cassandra.is_valid_stage_name(cur_stg_name) client.update_cpu_time(cur_stg_name, config.getfloat(section, option)) return client
from thrift import Thrift from thrift.transport import TTransport from thrift.transport import TSocket from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated from cassandra import Cassandra from cassandra.ttypes import * import time import pprint def main(): socket = TSocket.TSocket("192.168.10.2", 9160) transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) client = Cassandra.Client(protocol) pp = pprint.PrettyPrinter(indent=2) keyspace = "Keyspace1" column_path = ColumnPath(column_family="Standard1", column="age") key = "studentA" value = "18 " timestamp = time.time() try: #打开数据库连接 transport.open() #写入数据 client.insert(keyspace, key, column_path, value, timestamp, ConsistencyLevel.ZERO) #查询数据
def stop(self, env, rolling_restart=False): Cassandra().stop(env)
def start(self, env, rolling_restart=False): self.configure(env) Cassandra().start(env)
def status(self, env): Cassandra().status(env)
def _cassandraGetSliceFrom(params): try: cassandra = _cassandraConnect(params) s = "" range = Cassandra.SliceRange(params['search'], params['searchend'], params['descending'], params['count'] + params['offset']) predicate = Cassandra.SlicePredicate(None, range) parent = Cassandra.ColumnParent( params['cf'], None) # No supercolumn, we are not using them ATM result = cassandra['client'].get_slice(params['table'], params['row'], parent, predicate, 1) ## Close! cassandra['transport'].close() n = len(result) - params['offset'] if (n < 0): n = 0 s += "Obtained: " + str(n) + " columns (click on column data to open)" s += ' <span style="color:gray">- query predicate: ' + str( predicate) + "</span><br>\n" s += "%s<table class=\"main\">" % _navigationButtonsHtml(params) strcol = """\ <tr class=\"%s\"> <td class="rownum">%d</td> <td><tt>%s</tt></td> <td class=\"link\"><a href=\"%s\"><tt>%s</tt></a></td> <td>%s<br/><span class="parsedts">%s</span></td> <td><a title="Delete column" href=\"#\" onclick=\"javascript:if(confirm('Are you sure you want to delete this column?'))window.open('%s')\">×</a></td> </tr> """ i = 0 deleteParams = {\ "host":params["host"],\ "port":params["port"],\ "table":params["table"],\ "row":params["row"],\ "cf":params["cf"]\ } clazz = "even" for csp in result: col = csp.column # Assuming we are only querying columns - need to change this for supercolumns? s += "<!-- a column -- >\n" if i >= params['offset']: targetmap = copy(params) targetmap['open'] = col.name target = "view?%s" % urllib.urlencode(targetmap) deleteParams['columnName'] = col.name svalue = col.value if len(svalue) > 120: svalue = escape(svalue[0:120]) + _htmlEllipsis() else: svalue = escape(svalue) s += strcol % (clazz, i, col.name, target, svalue, str(col.timestamp), _cassandraTimestampToDatetime(col.timestamp), "delete?" + urllib.urlencode(deleteParams)) if clazz == "odd": clazz = "even" else: clazz = "odd" i = i + 1 s += "</table>\n" s += _navigationButtonsHtml(params) return s except Thrift.TException, tx: print '%s' % (tx.message) return 'TException: %s' % (tx.message)
def configure(self, env): import params env.set_params(params) Cassandra().configure(env)