Exemple #1
0
    def get(self, host, port):
        # if (host, port) in self.cache:
        #     entry = self.cache[(host, port)]
        #     return entry.transport, entry.protocol, entry.client
        socket = TSocket.TSocket(host, port)
        socket.setTimeout(self.timeout_ms)
        transport = TTransport.TFramedTransport(socket)
        protocol = TBinaryProtocolAccelerated(transport)
        client = block_request_service.Client(protocol)

        ex = None
        for i in range(3):
            try:
                transport.open()
            except TTransportException as e:
                ex = e
                continue
            except Exception:
                raise
            else:
                break
        else:
            raise TTransportException(
                ex.type,
                "Connection failed {}:{}: {}".format(host, port, ex.message))
        # self.cache[(host, port)] = ClientEntry(transport, protocol, client)
        return transport, protocol, client
def run_query(q):
    socket = TSocket.TSocket("ec2-107-20-75-29.compute-1.amazonaws.com", 10000)
    transport = TTransport.TBufferedTransport(socket)
    protocol = TBinaryProtocolAccelerated(transport)
    client = ThriftHive.Client(protocol)
    transport.open()
    client.execute(q)
    rows = client.fetchAll()
    transport.close()
    return [r.split('\t') for r in rows]
Exemple #3
0
    def reset_protocol(self):
        if self.__proto_id == self.trans.get_protocol_id():
            return

        proto_id = self.trans.get_protocol_id()

        if proto_id == self.T_BINARY_PROTOCOL:
            self.__proto = TBinaryProtocolAccelerated(self.trans,
                                                      self.strictRead, True)
        elif proto_id == self.T_COMPACT_PROTOCOL:
            self.__proto = TCompactProtocolAccelerated(self.trans)
        else:
            raise TApplicationException(TProtocolException.INVALID_PROTOCOL,
                                        "Unknown protocol requested")
        self.__proto_id = proto_id
Exemple #4
0
 def __init__(self, host='127.0.0.1', port=9090):
     self.socket_ = TSocket.TSocket(host, port)
     self.transport_ = TTransport.TBufferedTransport(self.socket_)
     self.protocol_ = TBinaryProtocolAccelerated(self.transport_)
     self.client_ = directory_service.Client(self.protocol_)
     ex = None
     for i in range(3):
         try:
             self.transport_.open()
         except TTransportException as e:
             ex = e
             continue
         except Exception:
             raise
         else:
             break
     else:
         raise TTransportException(
             ex.type,
             "Connection failed {}:{}: {}".format(host, port, ex.message))
Exemple #5
0
import sys

sys.path.append(
    os.path.abspath(os.path.dirname(os.path.abspath(
        os.path.dirname(__file__)))))
sys.path.append(
    os.path.abspath(
        os.path.abspath(os.path.dirname(__file__)) + os.sep + "gen-py"))

import time
from example.Example import Client
from thrift import Thrift
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated

try:
    transport = TSocket('127.0.0.1', 20000)
    transport = TBufferedTransport(transport)
    protocol = TBinaryProtocolAccelerated(transport)
    client = Client(protocol)

    transport.open()

    start = time.time()
    for i in range(10000):
        result = client.add(0, i)
    print(time.time() - start)

except Thrift.TException as ex:
    print("%s" % (ex.message))
from __future__ import absolute_import

from thrift.transport.THttpClient import THttpClient
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated

from ping import Ping

trans = THttpClient('http://localhost:8888/thrift')
proto = TBinaryProtocolAccelerated(trans)
client = Ping.Client(proto)

print client.ping('world')
 def connect(self):
     self.transport.open()
     protocol = TBinaryProtocolAccelerated(self.transport)
     service = ThriftClient(protocol)
     service = HS2Service(service, retries=3)
     return hs2.HiveServer2Connection(service, default_db=None)
Exemple #8
0
def main():
    args = parser.parse_args()

    INT_RE = re.compile("^\d+$")
    FLOAT_RE = re.compile("^\d+?\.\d+$")

    EXPS = [
        "==",
        "!=",
        ">=",
        "<=",
        ">",
        "<",
        "=regexp",
    ]
    FORMATS = ["int", "float"]

    exps = []
    for aexp in args.exps:
        for exp in EXPS:
            if exp in aexp:
                aexp = aexp.split(exp)
                if len(aexp) == 2:
                    if exp == "=regexp":
                        if aexp[1][0] == '(' and aexp[1][-1] == ')':
                            vtype = "string"
                            value = aexp[1][1:-1]
                            try:
                                re.compile(value)
                            except:
                                break
                            exp = "regexp"
                        else:
                            break
                    elif (aexp[1][0] == '"'
                          and aexp[1][-1] == '"') or (aexp[1][0] == "'"
                                                      and aexp[1][-1] == "'"):
                        vtype = "string"
                        value = aexp[1][1:-1]
                    elif FLOAT_RE.match(aexp[1]):
                        vtype = "float"
                        value = aexp[1]
                    elif INT_RE.match(aexp[1]):
                        vtype = "int"
                        value = aexp[1]
                    else:
                        vtype = "string"
                        value = aexp[1]
                    exps.append(FilterExpression(aexp[0], exp, vtype, value))
                break

    fields = {}
    for field in args.fields:
        fields[field] = 1

    formats = {}
    for format in args.formats:
        format = format.split(":")
        if len(format) != 2:
            continue

        if format[1] in FORMATS:
            formats[format[0]] = format[1]

    name = "tail_" + "".join([
        random.choice(string.digits + string.ascii_letters) for _ in range(16)
    ])
    filter = Filter(args.collection,
                    name,
                    exps=exps,
                    fields=fields,
                    formats=formats,
                    expried_time=5)

    transport = TSocket(args.host, args.port)
    transport = TBufferedTransport(transport)
    protocol = TBinaryProtocolAccelerated(transport)
    client = Client(protocol)
    transport.open()

    result = client.register_filter(filter)
    if result.result != 0:
        print("register error", name, result.msg)
        exit()

    print("register", name, filter)
    try:
        cursor = client.pull(name)
        while True:
            log = cursor.next()
            if not log:
                break
            if args.fields:
                flogs = []
                try:
                    log = json.loads(log)
                except:
                    print(log.encode("utf-8"))
                    continue

                for field in args.fields:
                    if field in args.timefields:
                        ts = log.get(field, 0)
                        try:
                            ts = int(ts)
                        except:
                            pass

                        if isinstance(ts, str):
                            flogs.append(ts)
                        else:
                            flogs.append(
                                datetime.datetime.fromtimestamp(
                                    ts).isoformat())
                    elif formats and field in formats:
                        if formats[field] not in ("int", "float"):
                            flogs.append("'%s'" % log.get(field, ""))
                        else:
                            flogs.append(str(log.get(field, 0)))
                    else:
                        flogs.append("'%s'" % log.get(field, ""))
                print(" ".join(flogs).encode("utf-8"))
            else:
                print(log.encode("utf-8"))
    except KeyboardInterrupt:
        pass
    finally:
        result = client.unregister_filter(name)
        print("unregister", name, result.msg)
        transport.close()