コード例 #1
0
    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()
コード例 #2
0
ファイル: stress.py プロジェクト: shengwenLeong/MyCassandra
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
コード例 #3
0
    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()
コード例 #4
0
 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()
コード例 #5
0
ファイル: index.py プロジェクト: jinsu/cass-fate
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}
コード例 #6
0
ファイル: connection.py プロジェクト: eruizgarcia/pycassa
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
コード例 #7
0
ファイル: connection.py プロジェクト: zanson/cassandra
    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
コード例 #8
0
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)
        #查询数据