Search in sources :

Example 1 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project hive by apache.

the class DynamicSerDe method initialize.

@Override
public void initialize(Configuration job, Properties tbl) throws SerDeException {
    try {
        String ddl = tbl.getProperty(serdeConstants.SERIALIZATION_DDL);
        // type_name used to be tbl.getProperty(META_TABLE_NAME).
        // However, now the value is DBName.TableName. To make it backward compatible,
        // we take the TableName part as type_name.
        //
        String tableName = tbl.getProperty(META_TABLE_NAME);
        int index = tableName.indexOf('.');
        if (index != -1) {
            type_name = tableName.substring(index + 1, tableName.length());
        } else {
            type_name = tableName;
        }
        String protoName = tbl.getProperty(serdeConstants.SERIALIZATION_FORMAT);
        if (protoName == null) {
            protoName = "org.apache.thrift.protocol.TBinaryProtocol";
        }
        // For backward compatibility
        protoName = protoName.replace("com.facebook.thrift.protocol", "org.apache.thrift.protocol");
        TProtocolFactory protFactory = TReflectionUtils.getProtocolFactoryByName(protoName);
        bos_ = new ByteStream.Output();
        bis_ = new ByteStream.Input();
        tios = new TIOStreamTransport(bis_, bos_);
        oprot_ = protFactory.getProtocol(tios);
        iprot_ = protFactory.getProtocol(tios);
        if (oprot_ instanceof org.apache.hadoop.hive.serde2.thrift.ConfigurableTProtocol) {
            ((ConfigurableTProtocol) oprot_).initialize(job, tbl);
        }
        if (iprot_ instanceof org.apache.hadoop.hive.serde2.thrift.ConfigurableTProtocol) {
            ((ConfigurableTProtocol) iprot_).initialize(job, tbl);
        }
        // in theory the include path should come from the configuration
        List<String> include_path = new ArrayList<String>();
        include_path.add(".");
        LOG.debug("ddl=" + ddl);
        parse_tree = new thrift_grammar(new ByteArrayInputStream(ddl.getBytes()), include_path, false);
        parse_tree.Start();
        bt = (DynamicSerDeStructBase) parse_tree.types.get(type_name);
        if (bt == null) {
            bt = (DynamicSerDeStructBase) parse_tree.tables.get(type_name);
        }
        if (bt == null) {
            throw new SerDeException("Could not lookup table type " + type_name + " in this ddl: " + ddl);
        }
        bt.initialize();
    } catch (Exception e) {
        System.err.println(StringUtils.stringifyException(e));
        throw new SerDeException(e);
    }
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) ArrayList(java.util.ArrayList) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) ConfigurableTProtocol(org.apache.hadoop.hive.serde2.thrift.ConfigurableTProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteStream(org.apache.hadoop.hive.serde2.ByteStream) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 2 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project hive by apache.

the class TestHCatHiveThriftCompatibility method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    if (setUpComplete) {
        return;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    TIOStreamTransport transport = new TIOStreamTransport(out);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    IntString intString = new IntString(1, "one", 1);
    intString.write(protocol);
    BytesWritable bytesWritable = new BytesWritable(out.toByteArray());
    intStringSeq = new Path(TEST_DATA_DIR + "/data/intString.seq");
    LOG.info("Creating data file: " + intStringSeq);
    SequenceFile.Writer seqFileWriter = SequenceFile.createWriter(intStringSeq.getFileSystem(hiveConf), hiveConf, intStringSeq, NullWritable.class, BytesWritable.class);
    seqFileWriter.append(NullWritable.get(), bytesWritable);
    seqFileWriter.close();
    setUpComplete = true;
}
Also used : Path(org.apache.hadoop.fs.Path) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) SequenceFile(org.apache.hadoop.io.SequenceFile) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) BytesWritable(org.apache.hadoop.io.BytesWritable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Before(org.junit.Before)

Example 3 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport 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 4 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project dubbo by alibaba.

the class ThriftCodec method encodeRequest.

private void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException {
    RpcInvocation inv = (RpcInvocation) request.getData();
    int seqId = nextSeqId();
    String serviceName = inv.getAttachment(Constants.INTERFACE_KEY);
    if (StringUtils.isEmpty(serviceName)) {
        throw new IllegalArgumentException(new StringBuilder(32).append("Could not find service name in attachment with key ").append(Constants.INTERFACE_KEY).toString());
    }
    TMessage message = new TMessage(inv.getMethodName(), TMessageType.CALL, seqId);
    String methodArgs = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class).getExtension(channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME)).generateArgsClassName(serviceName, inv.getMethodName());
    if (StringUtils.isEmpty(methodArgs)) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, new StringBuilder(32).append("Could not encode request, the specified interface may be incorrect.").toString());
    }
    Class<?> clazz = cachedClass.get(methodArgs);
    if (clazz == null) {
        try {
            clazz = ClassHelper.forNameWithThreadContextClassLoader(methodArgs);
            cachedClass.putIfAbsent(methodArgs, clazz);
        } catch (ClassNotFoundException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
    }
    TBase args;
    try {
        args = (TBase) clazz.newInstance();
    } catch (InstantiationException e) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
    } catch (IllegalAccessException e) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
    }
    for (int i = 0; i < inv.getArguments().length; i++) {
        Object obj = inv.getArguments()[i];
        if (obj == null) {
            continue;
        }
        TFieldIdEnum field = args.fieldForId(i + 1);
        String setMethodName = ThriftUtils.generateSetMethodName(field.getFieldName());
        Method method;
        try {
            method = clazz.getMethod(setMethodName, inv.getParameterTypes()[i]);
        } catch (NoSuchMethodException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
        try {
            method.invoke(args, obj);
        } catch (IllegalAccessException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        } catch (InvocationTargetException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
    }
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024);
    TIOStreamTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    int headerLength, messageLength;
    byte[] bytes = new byte[4];
    try {
        // magic
        protocol.writeI16(MAGIC);
        // message length placeholder
        protocol.writeI32(Integer.MAX_VALUE);
        // message header length placeholder
        protocol.writeI16(Short.MAX_VALUE);
        // version
        protocol.writeByte(VERSION);
        // service name
        protocol.writeString(serviceName);
        // dubbo request id
        protocol.writeI64(request.getId());
        protocol.getTransport().flush();
        // header size
        headerLength = bos.size();
        // message body
        protocol.writeMessageBegin(message);
        args.write(protocol);
        protocol.writeMessageEnd();
        protocol.getTransport().flush();
        int oldIndex = messageLength = bos.size();
        // fill in message length and header length
        try {
            TFramedTransport.encodeFrameSize(messageLength, bytes);
            bos.setWriteIndex(MESSAGE_LENGTH_INDEX);
            protocol.writeI32(messageLength);
            bos.setWriteIndex(MESSAGE_HEADER_LENGTH_INDEX);
            protocol.writeI16((short) (0xffff & headerLength));
        } finally {
            bos.setWriteIndex(oldIndex);
        }
    } catch (TException e) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
    }
    buffer.writeBytes(bytes);
    buffer.writeBytes(bos.toByteArray());
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) TException(org.apache.thrift.TException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) RandomAccessByteArrayOutputStream(com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) TFieldIdEnum(org.apache.thrift.TFieldIdEnum) RpcException(com.alibaba.dubbo.rpc.RpcException) TBase(org.apache.thrift.TBase)

Example 5 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport 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

TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)33 TException (org.apache.thrift.TException)13 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)11 TProtocol (org.apache.thrift.protocol.TProtocol)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Test (org.junit.Test)9 TMessage (org.apache.thrift.protocol.TMessage)8 TTransport (org.apache.thrift.transport.TTransport)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)7 Request (com.alibaba.dubbo.remoting.exchange.Request)6 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)6 IOException (java.io.IOException)6 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)5 RpcResult (com.alibaba.dubbo.rpc.RpcResult)5 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)5 URL (com.alibaba.dubbo.common.URL)4 Channel (com.alibaba.dubbo.remoting.Channel)4 Response (com.alibaba.dubbo.remoting.exchange.Response)4 RpcException (com.alibaba.dubbo.rpc.RpcException)3