List of usage examples for org.apache.thrift.transport TMemoryBuffer read
@Override
public int read(byte[] buf, int off, int len)
From source file:com.qq.servlet.demo.thrift.netty.client.NettyThriftClient.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(2); try {/*from ww w . j a v a2 s.com*/ final Bootstrap b = new Bootstrap(); b.group(group); b.channel(NioSocketChannel.class); b.handler(new NettyThriftClientInitializer()); // Start the connection attempt. ChannelFuture future = b.connect(host, port); ChannelFuture sync = future.sync(); Channel ch = sync.channel(); AttributeKey<String> key = AttributeKey.valueOf("ASYNC_CONTEXT"); Attribute<String> attr = ch.attr(key); String andSet = attr.getAndSet(Thread.currentThread().getName()); TMemoryBuffer transportr = new TMemoryBuffer(1024); TProtocol protocol = new TBinaryProtocol(transportr); TMultiplexedProtocol multiProtocol3 = new TMultiplexedProtocol(protocol, IdGenService.class.getSimpleName()); IdGenService.Client aoClient = new IdGenService.Client(multiProtocol3); IdGenReq req = new IdGenReq(); req.setTableName("t_lottery_append_task-----------"); req.setSource(andSet); aoClient.send_idGen(req); int length = transportr.length(); byte[] buf = new byte[length]; transportr.read(buf, 0, length); System.out.println(Arrays.toString(buf)); ChannelFuture lastWriteFuture = ch.writeAndFlush(buf); // Wait until all messages are flushed before closing the channel. if (lastWriteFuture != null) { lastWriteFuture.sync(); } System.in.read(); } finally { group.shutdownGracefully(); } }
From source file:org.apache.hadoop.hive.ql.QueryPlan.java
License:Apache License
public String toBinaryString() throws IOException { org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan(); TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5); TBinaryProtocol oprot = new TBinaryProtocol(tmb); try {//from w ww . j ava 2s . c om q.write(oprot); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); return q.toString(); } byte[] buf = new byte[tmb.length()]; tmb.read(buf, 0, tmb.length()); return new String(buf); // return getQueryPlan().toString(); }
From source file:org.apache.hadoop.hive.serde2.TestTCTLSeparatedProtocol.java
License:Apache License
public void testWrites() throws Exception { TMemoryBuffer trans = new TMemoryBuffer(1024); TCTLSeparatedProtocol prot = new TCTLSeparatedProtocol(trans, 1024); prot.writeStructBegin(new TStruct()); prot.writeFieldBegin(new TField()); prot.writeI32(100);//ww w .j a va 2s . c o m prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeListBegin(new TList()); prot.writeDouble(348.55); prot.writeDouble(234.22); prot.writeListEnd(); prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeString("hello world!"); prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeMapBegin(new TMap()); prot.writeString("key1"); prot.writeString("val1"); prot.writeString("key2"); prot.writeString("val2"); prot.writeString("key3"); prot.writeString("val3"); prot.writeMapEnd(); prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeListBegin(new TList()); prot.writeString("elem1"); prot.writeString("elem2"); prot.writeListEnd(); prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeString("bye!"); prot.writeFieldEnd(); prot.writeStructEnd(); trans.flush(); byte[] b = new byte[3 * 1024]; int len = trans.read(b, 0, b.length); String test = new String(b, 0, len); String testRef = "100348.55234.22hello world!key1val1key2val2key3val3elem1elem2bye!"; assertTrue(test.equals(testRef)); trans = new TMemoryBuffer(1023); trans.write(b, 0, len); // // read back! // prot = new TCTLSeparatedProtocol(trans, 10); prot.initialize(new Configuration(), new Properties()); // 100 is the start prot.readStructBegin(); prot.readFieldBegin(); assertTrue(prot.readI32() == 100); prot.readFieldEnd(); // let's see if doubles work ok prot.readFieldBegin(); TList l = prot.readListBegin(); assertTrue(l.size == 2); assertTrue(prot.readDouble() == 348.55); assertTrue(prot.readDouble() == 234.22); prot.readListEnd(); prot.readFieldEnd(); // nice message prot.readFieldBegin(); assertTrue(prot.readString().equals("hello world!")); prot.readFieldEnd(); // 3 element map prot.readFieldBegin(); TMap m = prot.readMapBegin(); assertTrue(m.size == 3); assertTrue(prot.readString().equals("key1")); assertTrue(prot.readString().equals("val1")); assertTrue(prot.readString().equals("key2")); assertTrue(prot.readString().equals("val2")); assertTrue(prot.readString().equals("key3")); assertTrue(prot.readString().equals("val3")); prot.readMapEnd(); prot.readFieldEnd(); // the 2 element list prot.readFieldBegin(); l = prot.readListBegin(); assertTrue(l.size == 2); assertTrue(prot.readString().equals("elem1")); assertTrue(prot.readString().equals("elem2")); prot.readListEnd(); prot.readFieldEnd(); // final string prot.readFieldBegin(); assertTrue(prot.readString().equals("bye!")); prot.readFieldEnd(); // shouldl return nulls at end prot.readFieldBegin(); assertNull(prot.readString()); prot.readFieldEnd(); // shouldl return nulls at end prot.readFieldBegin(); assertNull(prot.readString()); prot.readFieldEnd(); prot.readStructEnd(); }
From source file:org.apache.hadoop.hive.serde2.TestTCTLSeparatedProtocol.java
License:Apache License
public void testQuotedWrites() throws Exception { TMemoryBuffer trans = new TMemoryBuffer(4096); TCTLSeparatedProtocol prot = new TCTLSeparatedProtocol(trans, 4096); Properties schema = new Properties(); schema.setProperty(serdeConstants.QUOTE_CHAR, "\""); schema.setProperty(serdeConstants.FIELD_DELIM, ","); prot.initialize(new Configuration(), schema); String testStr = "\"hello, world!\""; prot.writeStructBegin(new TStruct()); prot.writeFieldBegin(new TField()); prot.writeString(testStr);/*from www . j a va2s . c om*/ prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeListBegin(new TList()); prot.writeString("elem1"); prot.writeString("elem2"); prot.writeListEnd(); prot.writeFieldEnd(); prot.writeStructEnd(); prot.writeString("\n"); trans.flush(); byte b[] = new byte[4096]; int len = trans.read(b, 0, b.length); trans = new TMemoryBuffer(4096); trans.write(b, 0, len); prot = new TCTLSeparatedProtocol(trans, 1024); prot.initialize(new Configuration(), schema); prot.readStructBegin(); prot.readFieldBegin(); final String firstRead = prot.readString(); prot.readFieldEnd(); testStr = testStr.replace("\"", ""); assertEquals(testStr, firstRead); // the 2 element list prot.readFieldBegin(); TList l = prot.readListBegin(); assertTrue(l.size == 2); assertTrue(prot.readString().equals("elem1")); assertTrue(prot.readString().equals("elem2")); prot.readListEnd(); prot.readFieldEnd(); // shouldl return nulls at end prot.readFieldBegin(); assertNull(prot.readString()); prot.readFieldEnd(); // shouldl return nulls at end prot.readFieldBegin(); assertNull(prot.readString()); prot.readFieldEnd(); prot.readStructEnd(); }
From source file:org.apache.hadoop.hive.serde2.TestTCTLSeparatedProtocol.java
License:Apache License
public void testNulls() throws Exception { TMemoryBuffer trans = new TMemoryBuffer(1024); TCTLSeparatedProtocol prot = new TCTLSeparatedProtocol(trans, 10); prot.initialize(new Configuration(), new Properties()); prot.writeStructBegin(new TStruct()); prot.writeFieldBegin(new TField()); prot.writeString(null);//ww w. j av a 2 s .com prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeString(null); prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeI32(100); prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeString(null); prot.writeFieldEnd(); prot.writeFieldBegin(new TField()); prot.writeMapBegin(new TMap()); prot.writeString(null); prot.writeString(null); prot.writeString("key2"); prot.writeString(null); prot.writeString(null); prot.writeString("val3"); prot.writeMapEnd(); prot.writeFieldEnd(); prot.writeStructEnd(); byte b[] = new byte[3 * 1024]; int len = trans.read(b, 0, b.length); String written = new String(b, 0, len); String testRef = "\\N\\N100\\N\\N\\Nkey2\\N\\Nval3"; assertTrue(testRef.equals(written)); trans = new TMemoryBuffer(1023); trans.write(b, 0, len); prot = new TCTLSeparatedProtocol(trans, 3); prot.initialize(new Configuration(), new Properties()); prot.readStructBegin(); prot.readFieldBegin(); String ret = prot.readString(); prot.readFieldEnd(); assertNull(ret); prot.readFieldBegin(); ret = prot.readString(); prot.readFieldEnd(); assertNull(ret); prot.readFieldBegin(); int ret1 = prot.readI32(); prot.readFieldEnd(); assertTrue(ret1 == 100); prot.readFieldBegin(); ret1 = prot.readI32(); prot.readFieldEnd(); prot.readFieldBegin(); TMap map = prot.readMapBegin(); assertTrue(map.size == 3); assertNull(prot.readString()); assertNull(prot.readString()); assertTrue(prot.readString().equals("key2")); assertNull(prot.readString()); assertNull(prot.readString()); assertTrue(prot.readString().equals("val3")); prot.readMapEnd(); prot.readFieldEnd(); assertTrue(ret1 == 0); }