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 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 _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 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
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) #查询数据