Search in sources :

Example 6 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project zeppelin by apache.

the class ClientFactory method create.

@Override
public Client create() throws Exception {
    TSocket transport = new TSocket(host, port);
    try {
        transport.open();
    } catch (TTransportException e) {
        throw new InterpreterException(e);
    }
    TProtocol protocol = new TBinaryProtocol(transport);
    Client client = new RemoteInterpreterService.Client(protocol);
    synchronized (clientSocketMap) {
        clientSocketMap.put(client, transport);
    }
    return client;
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) TTransportException(org.apache.thrift.transport.TTransportException) Client(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterService.Client) TSocket(org.apache.thrift.transport.TSocket)

Example 7 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project lucida by claritylab.

the class QAClient method main.

public static void main(String[] args) {
    // Collect the port number.
    int port = 8083;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }
    // User.
    String LUCID = "Clinc";
    QuerySpec create_spec = new QuerySpec();
    // Knowledge.
    final QueryInput knowledge_text = createQueryInput("text", "Clinc is created by Jason and Lingjia.", "1234567");
    final QueryInput knowledge_url = createQueryInput("url", "https://en.wikipedia.org/wiki/Apple_Inc.", "abcdefg");
    final QuerySpec knowledge = createQuerySpec("knowledge", new ArrayList<QueryInput>() {

        {
            add(knowledge_text);
            add(knowledge_url);
        }
    });
    // Unlearn.
    final QueryInput knowledge_unlearn_input = createQueryInput("unlearn", "", "abcdefg");
    final QuerySpec knowledge_unlearn_spec = createQuerySpec("unlearn knowledge", new ArrayList<QueryInput>() {

        {
            add(knowledge_unlearn_input);
        }
    });
    // Query.
    final QueryInput query_input = createQueryInput("text", "Who created Clinc?", "");
    final QuerySpec query = createQuerySpec("query", new ArrayList<QueryInput>() {

        {
            add(query_input);
        }
    });
    // Initialize thrift objects.
    // TTransport transport = new TSocket("clarity08.eecs.umich.edu", port);
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(new TFramedTransport(transport));
    LucidaService.Client client = new LucidaService.Client(protocol);
    try {
        // Talk to the server.
        transport.open();
        System.out.println("///// Connecting to OpenEphyra at port " + port + " ... /////");
        // Learn and ask.
        client.create(LUCID, create_spec);
        client.learn(LUCID, knowledge);
        System.out.println("///// Query input: /////");
        System.out.println(query_input.data.get(0));
        String answer = client.infer(LUCID, query);
        // Print the answer.
        System.out.println("///// Answer: /////");
        System.out.println(answer);
        // Unlearn and ask again.
        client.learn(LUCID, knowledge_unlearn_spec);
        System.out.println("///// Query input: /////");
        System.out.println(query_input.data.get(0));
        answer = client.infer(LUCID, query);
        // Print the answer.
        System.out.println("///// Answer: /////");
        System.out.println(answer);
        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 8 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project buck by facebook.

the class ThriftCoordinatorClient method start.

public ThriftCoordinatorClient start() throws IOException {
    transport = new TFramedTransport(new TSocket(remoteHost, remotePort));
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (true) {
        try {
            transport.open();
            break;
        } catch (TTransportException e) {
            if (stopwatch.elapsed(TimeUnit.SECONDS) > MAX_CONNECT_TIMEOUT_SECONDS) {
                throw new IOException(String.format("Failed to connect. Coordinator is still not healthy after [%d] seconds.", MAX_CONNECT_TIMEOUT_SECONDS));
            }
            LOG.debug("Coordinator server currently not available. Retrying in a bit...");
            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(RETRY_TIMEOUT_SECONDS));
            } catch (InterruptedException innerException) {
                throw new RuntimeException(innerException);
            }
        }
    }
    TProtocol protocol = new TBinaryProtocol(transport);
    client = new CoordinatorService.Client(protocol);
    return this;
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) Stopwatch(com.google.common.base.Stopwatch) TTransportException(org.apache.thrift.transport.TTransportException) CoordinatorService(com.facebook.buck.distributed.thrift.CoordinatorService) IOException(java.io.IOException) TSocket(org.apache.thrift.transport.TSocket)

Example 9 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project buck by facebook.

the class ThriftUtil method deserialize.

public static void deserialize(ThriftProtocol protocol, InputStream source, TBase<?, ?> dest) throws ThriftException {
    try (TIOStreamTransport responseTransport = new TIOStreamTransport(source)) {
        TProtocol responseProtocol = newProtocolInstance(protocol, responseTransport);
        dest.read(responseProtocol);
    } catch (TException e) {
        throw new ThriftException(e);
    }
}
Also used : TException(org.apache.thrift.TException) TProtocol(org.apache.thrift.protocol.TProtocol) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport)

Example 10 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project dubbo by alibaba.

the class MultiServiceProcessor method process.

public boolean process(TProtocol in, TProtocol out) throws TException {
    short magic = in.readI16();
    if (magic != ThriftCodec.MAGIC) {
        logger.error(new StringBuilder(24).append("Unsupported magic ").append(magic).toString());
        return false;
    }
    in.readI32();
    in.readI16();
    byte version = in.readByte();
    String serviceName = in.readString();
    long id = in.readI64();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    TIOStreamTransport transport = new TIOStreamTransport(bos);
    TProtocol protocol = protocolFactory.getProtocol(transport);
    TProcessor processor = processorMap.get(serviceName);
    if (processor == null) {
        logger.error(new StringBuilder(32).append("Could not find processor for service ").append(serviceName).toString());
        return false;
    }
    // todo if exception
    boolean result = processor.process(in, protocol);
    ByteArrayOutputStream header = new ByteArrayOutputStream(512);
    TIOStreamTransport headerTransport = new TIOStreamTransport(header);
    TProtocol headerProtocol = protocolFactory.getProtocol(headerTransport);
    headerProtocol.writeI16(magic);
    headerProtocol.writeI32(Integer.MAX_VALUE);
    headerProtocol.writeI16(Short.MAX_VALUE);
    headerProtocol.writeByte(version);
    headerProtocol.writeString(serviceName);
    headerProtocol.writeI64(id);
    headerProtocol.getTransport().flush();
    out.writeI16(magic);
    out.writeI32(bos.size() + header.size());
    out.writeI16((short) (0xffff & header.size()));
    out.writeByte(version);
    out.writeString(serviceName);
    out.writeI64(id);
    out.getTransport().write(bos.toByteArray());
    out.getTransport().flush();
    return result;
}
Also used : TProcessor(org.apache.thrift.TProcessor) TProtocol(org.apache.thrift.protocol.TProtocol) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

TProtocol (org.apache.thrift.protocol.TProtocol)78 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)29 TTransport (org.apache.thrift.transport.TTransport)28 TSocket (org.apache.thrift.transport.TSocket)23 TException (org.apache.thrift.TException)22 TFramedTransport (org.apache.thrift.transport.TFramedTransport)17 IOException (java.io.IOException)15 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)10 THttpClient (org.apache.thrift.transport.THttpClient)9 TTransportException (org.apache.thrift.transport.TTransportException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)7 RDF_Term (org.apache.jena.riot.thrift.wire.RDF_Term)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 TProcessor (org.apache.thrift.TProcessor)5 Hbase (org.apache.hadoop.hbase.thrift.generated.Hbase)4 TTransportFactory (org.apache.thrift.transport.TTransportFactory)4 OutputStream (java.io.OutputStream)3