本文整理匯總了Java中org.apache.thrift.protocol.TProtocolFactory.getProtocol方法的典型用法代碼示例。如果您正苦於以下問題:Java TProtocolFactory.getProtocol方法的具體用法?Java TProtocolFactory.getProtocol怎麽用?Java TProtocolFactory.getProtocol使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.thrift.protocol.TProtocolFactory
的用法示例。
在下文中一共展示了TProtocolFactory.getProtocol方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: messageReceived
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
@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;
}
}
示例2: createFile
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
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;
}
示例3: M3Reporter
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
private M3Reporter(Builder builder) {
try {
// Builder verifies non-null, non-empty socketAddresses
SocketAddress[] socketAddresses = builder.socketAddresses;
TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
if (socketAddresses.length > 1) {
transport = new TMultiUdpClient(socketAddresses);
} else {
transport = new TUdpClient(socketAddresses[0]);
}
transport.open();
client = new M3.Client(protocolFactory.getProtocol(transport));
calcProtocol = new TCompactProtocol.Factory().getProtocol(new TCalcTransport());
calc = (TCalcTransport) calcProtocol.getTransport();
freeBytes = calculateFreeBytes(builder.maxPacketSizeBytes, builder.metricTagSet);
maxProcessorWaitUntilFlushMillis = builder.maxProcessorWaitUntilFlushMillis;
bucketIdTagName = builder.histogramBucketIdName;
bucketTagName = builder.histogramBucketName;
bucketValFmt = String.format("%%.%df", builder.histogramBucketTagPrecision);
metricQueue = new LinkedBlockingQueue<>(builder.maxQueueSize);
executor = builder.executor;
addAndRunProcessor(builder.metricTagSet);
} catch (TTransportException | SocketException e) {
throw new RuntimeException("Exception creating M3Reporter", e);
}
}
示例4: test
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
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();
}
}
}
示例5: HeaderTBaseSerializer
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
/**
* Create a new HeaderTBaseSerializer.
*/
HeaderTBaseSerializer(ByteArrayOutputStream bos, TProtocolFactory protocolFactory, TBaseLocator locator) {
this.baos = bos;
TIOStreamTransport transport = new TIOStreamTransport(bos);
this.protocol = protocolFactory.getProtocol(transport);
this.locator = locator;
}
示例6: createFileForRead
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
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();
}
示例7: writeParquetFile
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
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();
}
示例8: shouldDoProjection
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
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);
}
示例9: createFileForRead
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
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();
}
示例10: HeaderTBaseSerializer
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
/**
* Create a new HeaderTBaseSerializer.
*/
HeaderTBaseSerializer(ResettableByteArrayOutputStream bos, TProtocolFactory protocolFactory, TBaseLocator locator) {
this.baos = bos;
TIOStreamTransport transport = new TIOStreamTransport(bos);
this.protocol = protocolFactory.getProtocol(transport);
this.locator = locator;
}
示例11: testIt
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
public void testIt() throws Exception {
for (TProtocolFactory protoFactory : getProtocols()) {
TestHandler handler = new TestHandler();
ThriftTest.Processor processor = new ThriftTest.Processor(handler);
startServer(processor, protoFactory);
TSocket socket = new TSocket(HOST, PORT);
socket.setTimeout(SOCKET_TIMEOUT);
TTransport transport = getClientTransport(socket);
TProtocol protocol = protoFactory.getProtocol(transport);
ThriftTest.Client testClient = new ThriftTest.Client(protocol);
open(transport);
testVoid(testClient);
testString(testClient);
testByte(testClient);
testI32(testClient);
testI64(testClient);
testDouble(testClient);
testStruct(testClient);
testNestedStruct(testClient);
testMap(testClient);
testStringMap(testClient);
testSet(testClient);
testList(testClient);
testEnum(testClient);
testTypedef(testClient);
testNestedMap(testClient);
testInsanity(testClient);
testOneway(testClient);
testException(testClient);
transport.close();
stopServer();
}
}
示例12: testCleanupAllSelectionKeys
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
public void testCleanupAllSelectionKeys() throws Exception {
for (TProtocolFactory protoFactory : getProtocols()) {
TestHandler handler = new TestHandler();
ThriftTest.Processor processor = new ThriftTest.Processor(handler);
startServer(processor, protoFactory);
TSocket socket = new TSocket(HOST, PORT);
socket.setTimeout(SOCKET_TIMEOUT);
TTransport transport = getClientTransport(socket);
TProtocol protocol = protoFactory.getProtocol(transport);
ThriftTest.Client testClient = new ThriftTest.Client(protocol);
open(transport);
for (int i = 0; i < NUM_QUERIES; ++i) {
testClient.testI32(1);
}
server.stop();
for (int i = 0; i < NUM_QUERIES; ++i) {
try {
testClient.testI32(1);
} catch(TTransportException e) {
System.err.println(e);
e.printStackTrace();
if (e.getCause() instanceof java.net.SocketTimeoutException) {
fail("timed out when it should have thrown another kind of error!");
}
}
}
transport.close();
stopServer();
}
}
示例13: main
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("usage: java -cp build/classes org.apache.thrift.test.ReadStruct filename proto_factory_class");
System.out.println("Read in an instance of CompactProtocolTestStruct from 'file', making sure that it is equivalent to Fixtures.compactProtoTestStruct. Use a protocol from 'proto_factory_class'.");
}
TTransport trans = new TIOStreamTransport(new BufferedInputStream(new FileInputStream(args[0])));
TProtocolFactory factory = (TProtocolFactory)Class.forName(args[1]).newInstance();
TProtocol proto = factory.getProtocol(trans);
CompactProtoTestStruct cpts = new CompactProtoTestStruct();
for (CompactProtoTestStruct._Fields fid : CompactProtoTestStruct.metaDataMap.keySet()) {
cpts.setFieldValue(fid, null);
}
cpts.read(proto);
if (cpts.equals(Fixtures.compactProtoTestStruct)) {
System.out.println("Object verified successfully!");
} else {
System.out.println("Object failed verification!");
System.out.println("Expected: " + Fixtures.compactProtoTestStruct + " but got " + cpts);
}
}
示例14: main
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("usage: java -cp build/classes org.apache.thrift.test.WriteStruct filename proto_factory_class");
System.out.println("Write out an instance of Fixtures.compactProtocolTestStruct to 'file'. Use a protocol from 'proto_factory_class'.");
}
TTransport trans = new TIOStreamTransport(new BufferedOutputStream(new FileOutputStream(args[0])));
TProtocolFactory factory = (TProtocolFactory)Class.forName(args[1]).newInstance();
TProtocol proto = factory.getProtocol(trans);
Fixtures.compactProtoTestStruct.write(proto);
trans.flush();
}
示例15: tryDecodeUnframedMessage
import org.apache.thrift.protocol.TProtocolFactory; //導入方法依賴的package包/類
protected ByteBuf tryDecodeUnframedMessage(ChannelHandlerContext ctx,
Channel channel,
ByteBuf buffer,
TProtocolFactory inputProtocolFactory)
throws TException {
// Perform a trial decode, skipping through
// the fields, to see whether we have an entire message available.
int messageLength = 0;
int messageStartReaderIndex = buffer.readerIndex();
try {
TNiftyTransport decodeAttemptTransport =
new TNiftyTransport(channel, buffer, ThriftTransportType.UNFRAMED);
int initialReadBytes = decodeAttemptTransport.getReadByteCount();
TProtocol inputProtocol =
inputProtocolFactory.getProtocol(decodeAttemptTransport);
// Skip through the message
inputProtocol.readMessageBegin();
TProtocolUtil.skip(inputProtocol, TType.STRUCT);
inputProtocol.readMessageEnd();
messageLength = decodeAttemptTransport.getReadByteCount() - initialReadBytes;
} catch (TTransportException | IndexOutOfBoundsException e) {
// No complete message was decoded: ran out of bytes
return null;
} finally {
if (buffer.readerIndex() - messageStartReaderIndex > maxFrameSize) {
throw new TooLongFrameException("Maximum frame size of " + maxFrameSize + " exceeded");
}
buffer.readerIndex(messageStartReaderIndex);
}
if (messageLength <= 0) {
return null;
}
// We have a full message in the read buffer, slice it off
ByteBuf messageBuffer =
extractFrame(buffer, messageStartReaderIndex, messageLength);
buffer.readerIndex(messageStartReaderIndex + messageLength);
return messageBuffer;
}