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);
}
}
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;
}
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);
}
}
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());
}
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;
}
Aggregations