Java Code Examples for org.apache.thrift.protocol.TProtocolFactory#getProtocol()

The following examples show how to use org.apache.thrift.protocol.TProtocolFactory#getProtocol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThriftMessageEncoder.java    From nettythrift with Apache License 2.0 6 votes vote down vote up
@Override
protected void messageReceived(ChannelHandlerContext ctx, ThriftMessage message) throws Exception {
	ByteBuf buffer = message.getContent();
	logger.debug("msg.content:: {}", buffer);
	try {
		TNettyTransport transport = new TNettyTransport(ctx.channel(), buffer);
		TProtocolFactory protocolFactory = message.getProtocolFactory();
		TProtocol protocol = protocolFactory.getProtocol(transport);
		serverDef.nettyProcessor.process(ctx, protocol, protocol,
				new DefaultWriterListener(message, transport, ctx, serverDef));
	} catch (Throwable ex) {
		int refCount = buffer.refCnt();
		if (refCount > 0) {
			buffer.release(refCount);
		}
		throw ex;
	}
}
 
Example 2
Source File: TestThriftToParquetFileWriter.java    From parquet-mr with Apache License 2.0 6 votes vote down vote up
private <T extends TBase<?,?>> Path createFile(T... tObjs) throws IOException, InterruptedException, TException  {
  final Path fileToCreate = new Path("target/test/TestThriftToParquetFileWriter/"+tObjs[0].getClass()+".parquet");
  LOG.info("File created: {}", fileToCreate.toString());
  Configuration conf = new Configuration();
  final FileSystem fs = fileToCreate.getFileSystem(conf);
  if (fs.exists(fileToCreate)) {
    fs.delete(fileToCreate, true);

  }
  TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
  TaskAttemptID taskId = new TaskAttemptID("local", 0, true, 0, 0);
  ThriftToParquetFileWriter w = new ThriftToParquetFileWriter(fileToCreate, ContextUtil.newTaskAttemptContext(conf, taskId), protocolFactory, (Class<? extends TBase<?, ?>>) tObjs[0].getClass());

  for(T tObj:tObjs) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(baos));

    tObj.write(protocol);

    w.write(new BytesWritable(baos.toByteArray()));
  }
  w.close();

  return fileToCreate;
}
 
Example 3
Source File: DemoClientTraditionalTEST.java    From nettythrift with Apache License 2.0 5 votes vote down vote up
void test(TProtocolFactory fac) throws Exception {
	TProtocol prot = fac.getProtocol(socket());
	TCalculator.Client client = null;
	try {
		client = new TCalculator.Client(prot);
		org.junit.Assert.assertEquals(2, client.add(1, 1));
	} finally {
		if (client != null) {
			client.getInputProtocol().getTransport().close();
			client.getOutputProtocol().getTransport().close();
		}
	}
}
 
Example 4
Source File: ThriftFrameDecoder.java    From ikasoa with MIT License 5 votes vote down vote up
protected ChannelBuffer tryDecodeUnframedMessage(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer,
		TProtocolFactory inputProtocolFactory) throws TException {

	int messageLength = 0;
	int messageStartReaderIndex = buffer.readerIndex();

	try {
		TNettyTransport decodeAttemptTransport = new TNettyTransport(channel, buffer, TNettyTransportType.UNFRAMED);
		int initialReadBytes = decodeAttemptTransport.getReadByteCount();
		TProtocol inputProtocol = inputProtocolFactory.getProtocol(decodeAttemptTransport);
		inputProtocol.readMessageBegin();
		TProtocolUtil.skip(inputProtocol, TType.STRUCT);
		inputProtocol.readMessageEnd();
		messageLength = decodeAttemptTransport.getReadByteCount() - initialReadBytes;
	} catch (TTransportException | IndexOutOfBoundsException e) {
		return null;
	} finally {
		if (buffer.readerIndex() - messageStartReaderIndex > maxFrameSize)
			Channels.fireExceptionCaught(ctx,
					new TooLongFrameException(String.format("Maximum frame size of %d exceeded .", maxFrameSize)));
		buffer.readerIndex(messageStartReaderIndex);
	}

	if (messageLength <= 0)
		return null;

	ChannelBuffer messageBuffer = extractFrame(buffer, messageStartReaderIndex, messageLength);
	buffer.readerIndex(messageStartReaderIndex + messageLength);
	return messageBuffer;
}
 
Example 5
Source File: ThriftJacksonSerializers.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static String serializeTBaseLike(Consumer<TProtocol> writer, boolean useNamedEnums) {
    final TMemoryBuffer buffer = new TMemoryBuffer(1024);
    final TProtocolFactory factory = useNamedEnums ? ThriftProtocolFactories.TEXT_NAMED_ENUM
                                                   : ThriftProtocolFactories.TEXT;
    final TProtocol protocol = factory.getProtocol(buffer);
    writer.accept(protocol);
    return new String(buffer.getArray(), 0, buffer.length());
}
 
Example 6
Source File: TestParquetTBaseScheme.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private void createFileForRead() throws Exception {
  final Path fileToCreate = new Path(parquetInputPath+"/names.parquet");

  final Configuration conf = new Configuration();
  final FileSystem fs = fileToCreate.getFileSystem(conf);
  if (fs.exists(fileToCreate)) fs.delete(fileToCreate, true);

  TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
  TaskAttemptID taskId = new TaskAttemptID("local", 0, true, 0, 0);
  ThriftToParquetFileWriter w = new ThriftToParquetFileWriter(fileToCreate, ContextUtil.newTaskAttemptContext(conf, taskId), protocolFactory, Name.class);

  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(baos));

  Name n1 = new Name();
  n1.setFirst_name("Alice");
  n1.setLast_name("Practice");
  Name n2 = new Name();
  n2.setFirst_name("Bob");
  n2.setLast_name("Hope");
  Name n3 = new Name();
  n3.setFirst_name("Charlie");
  n3.setLast_name("Horse");

  n1.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  baos.reset();
  n2.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  baos.reset();
  n3.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  w.close();
}
 
Example 7
Source File: ParquetScroogeSchemeTest.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private void writeParquetFile(List<TBase> recordsToWrite, Configuration conf, Path parquetFile) throws IOException, InterruptedException, org.apache.thrift.TException {
  //create a test file
  final TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
  final TaskAttemptID taskId = new TaskAttemptID("local", 0, true, 0, 0);
  Class writeClass = recordsToWrite.get(0).getClass();
  final ThriftToParquetFileWriter w = new ThriftToParquetFileWriter(parquetFile, ContextUtil.newTaskAttemptContext(conf, taskId), protocolFactory, writeClass);
  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(baos));
  for (TBase recordToWrite : recordsToWrite) {
    recordToWrite.write(protocol);
  }
  w.write(new BytesWritable(baos.toByteArray()));
  w.close();
}
 
Example 8
Source File: TestParquetTBaseScheme.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private void createFileForRead() throws Exception {
  final Path fileToCreate = new Path(parquetInputPath+"/names.parquet");

  final Configuration conf = new Configuration();
  final FileSystem fs = fileToCreate.getFileSystem(conf);
  if (fs.exists(fileToCreate)) fs.delete(fileToCreate, true);

  TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
  TaskAttemptID taskId = new TaskAttemptID("local", 0, true, 0, 0);
  ThriftToParquetFileWriter w = new ThriftToParquetFileWriter(fileToCreate, ContextUtil.newTaskAttemptContext(conf, taskId), protocolFactory, Name.class);

  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(baos));

  Name n1 = new Name();
  n1.setFirst_name("Alice");
  n1.setLast_name("Practice");
  Name n2 = new Name();
  n2.setFirst_name("Bob");
  n2.setLast_name("Hope");
  Name n3 = new Name();
  n3.setFirst_name("Charlie");
  n3.setLast_name("Horse");

  n1.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  baos.reset();
  n2.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  baos.reset();
  n3.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  w.close();
}
 
Example 9
Source File: TestParquetToThriftReadWriteAndProjection.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private <T extends TBase<?, ?>> void shouldDoProjection(Configuration conf, T recordToWrite, T exptectedReadResult, Class<? extends TBase<?, ?>> thriftClass) throws Exception {
  final Path parquetFile = new Path("target/test/TestParquetToThriftReadWriteAndProjection/file.parquet");
  final FileSystem fs = parquetFile.getFileSystem(conf);
  if (fs.exists(parquetFile)) {
    fs.delete(parquetFile, true);
  }

  //create a test file
  final TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
  final TaskAttemptID taskId = new TaskAttemptID("local", 0, true, 0, 0);
  final ThriftToParquetFileWriter w = new ThriftToParquetFileWriter(parquetFile, ContextUtil.newTaskAttemptContext(conf, taskId), protocolFactory, thriftClass);
  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(baos));

  recordToWrite.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  w.close();


  final ParquetThriftInputFormat<T> parquetThriftInputFormat = new ParquetThriftInputFormat<T>();
  final Job job = new Job(conf, "read");
  job.setInputFormatClass(ParquetThriftInputFormat.class);
  ParquetThriftInputFormat.setInputPaths(job, parquetFile);
  final JobID jobID = new JobID("local", 1);
  List<InputSplit> splits = parquetThriftInputFormat.getSplits(ContextUtil.newJobContext(ContextUtil.getConfiguration(job), jobID));
  T readValue = null;
  for (InputSplit split : splits) {
    TaskAttemptContext taskAttemptContext = ContextUtil.newTaskAttemptContext(ContextUtil.getConfiguration(job), new TaskAttemptID(new TaskID(jobID, true, 1), 0));
    final RecordReader<Void, T> reader = parquetThriftInputFormat.createRecordReader(split, taskAttemptContext);
    reader.initialize(split, taskAttemptContext);
    if (reader.nextKeyValue()) {
      readValue = reader.getCurrentValue();
      LOG.info("{}", readValue);
    }
  }
  assertEquals(exptectedReadResult, readValue);

}
 
Example 10
Source File: TestParquetTupleScheme.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private void createFileForRead() throws Exception {
  final Path fileToCreate = new Path(parquetInputPath + "/names.parquet");

  final Configuration conf = new Configuration();
  final FileSystem fs = fileToCreate.getFileSystem(conf);
  if (fs.exists(fileToCreate)) fs.delete(fileToCreate, true);

  TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
  TaskAttemptID taskId = new TaskAttemptID("local", 0, true, 0, 0);
  ThriftToParquetFileWriter w = new ThriftToParquetFileWriter(fileToCreate, ContextUtil.newTaskAttemptContext(conf, taskId), protocolFactory, Name.class);

  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(baos));

  Name n1 = new Name();
  n1.setFirst_name("Alice");
  n1.setLast_name("Practice");
  Name n2 = new Name();
  n2.setFirst_name("Bob");
  n2.setLast_name("Hope");
  Name n3 = new Name();
  n3.setFirst_name("Charlie");
  n3.setLast_name("Horse");

  n1.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  baos.reset();
  n2.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  baos.reset();
  n3.write(protocol);
  w.write(new BytesWritable(baos.toByteArray()));
  w.close();
}
 
Example 11
Source File: FlinkHeaderTBaseSerializer.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new HeaderTBaseSerializer.
 *
 * @param bos
 * @param protocolFactory
 */
public FlinkHeaderTBaseSerializer(ResettableByteArrayOutputStream bos, TProtocolFactory protocolFactory, TypeLocator<TBase<?, ?>> locator) {
    this.baos = Assert.requireNonNull(bos, "ResettableByteArrayOutputStream");
    this.locator = Assert.requireNonNull(locator, "locator");

    Assert.requireNonNull(protocolFactory, "TProtocolFactory");
    TIOStreamTransport transport = new TIOStreamTransport(bos);
    this.protocol = protocolFactory.getProtocol(transport);


}
 
Example 12
Source File: HeaderTBaseSerializer.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new HeaderTBaseSerializer. 
 */
HeaderTBaseSerializer(ResettableByteArrayOutputStream bos, TProtocolFactory protocolFactory, TypeLocator<TBase<?, ?>> locator) {
    this.baos = bos;
    TIOStreamTransport transport = new TIOStreamTransport(bos);
    this.protocol = protocolFactory.getProtocol(transport);
    this.locator = locator;
}
 
Example 13
Source File: HeaderTBaseSerializer2.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
public HeaderTBaseSerializer2(TProtocolFactory protocolFactory, TypeLocator<TBase<?, ?>> tBaseLocator) {
    this.tOutputStreamTransport = new TOutputStreamTransport();
    this.protocol = protocolFactory.getProtocol(tOutputStreamTransport);
    this.tBaseLocator = tBaseLocator;
}
 
Example 14
Source File: ChunkHeaderTBaseDeserializer.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
ChunkHeaderTBaseDeserializer(TProtocolFactory protocolFactory, TypeLocator<TBase<?, ?>> locator) {
    this.trans = new TMemoryInputTransport();
    this.protocol = protocolFactory.getProtocol(trans);
    this.locator = locator;
}
 
Example 15
Source File: HeaderTBaseDeserializer.java    From pinpoint with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new TDeserializer. It will use the TProtocol specified by the
 * factory that is passed in.
 *
 * @param protocolFactory Factory to create a protocol
 */
HeaderTBaseDeserializer(TProtocolFactory protocolFactory, TypeLocator<TBase<?, ?>> locator) {
    this.trans = new TMemoryInputTransport();
    this.protocol = protocolFactory.getProtocol(trans);
    this.locator = locator;
}