use of org.apache.thrift.transport.TMemoryBuffer in project hive by apache.
the class TestTCTLSeparatedProtocol method test1ApacheLogFormat.
/**
* Tests a sample apache log format. This is actually better done in general
* with a more TRegexLike protocol, but for this case, TCTLSeparatedProtocol
* can do it.
*/
public void test1ApacheLogFormat() throws Exception {
final String sample = "127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326";
TMemoryBuffer trans = new TMemoryBuffer(4096);
trans.write(sample.getBytes(), 0, sample.getBytes().length);
trans.flush();
TCTLSeparatedProtocol prot = new TCTLSeparatedProtocol(trans, 4096);
Properties schema = new Properties();
// this is a hacky way of doing the quotes since it will match any 2 of
// these, so
// "[ hello this is something to split [" would be considered to be quoted.
schema.setProperty(serdeConstants.QUOTE_CHAR, "(\"|\\[|\\])");
schema.setProperty(serdeConstants.FIELD_DELIM, " ");
schema.setProperty(serdeConstants.SERIALIZATION_NULL_FORMAT, "-");
prot.initialize(new Configuration(), schema);
prot.readStructBegin();
// ip address
prot.readFieldBegin();
final String ip = prot.readString();
prot.readFieldEnd();
assertEquals("127.0.0.1", ip);
// identd
prot.readFieldBegin();
final String identd = prot.readString();
prot.readFieldEnd();
assertNull(identd);
// user
prot.readFieldBegin();
final String user = prot.readString();
prot.readFieldEnd();
assertEquals("frank", user);
// finishTime
prot.readFieldBegin();
final String finishTime = prot.readString();
prot.readFieldEnd();
assertEquals("10/Oct/2000:13:55:36 -0700", finishTime);
// requestLine
prot.readFieldBegin();
final String requestLine = prot.readString();
prot.readFieldEnd();
assertEquals("GET /apache_pb.gif HTTP/1.0", requestLine);
// returncode
prot.readFieldBegin();
final int returnCode = prot.readI32();
prot.readFieldEnd();
assertEquals(200, returnCode);
// return size
prot.readFieldBegin();
final int returnSize = prot.readI32();
prot.readFieldEnd();
assertEquals(2326, returnSize);
prot.readStructEnd();
}
use of org.apache.thrift.transport.TMemoryBuffer in project hive by apache.
the class TestTCTLSeparatedProtocol method testQuotedWrites.
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);
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();
}
use of org.apache.thrift.transport.TMemoryBuffer in project hive by apache.
the class QueryPlan method toBinaryString.
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 {
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();
}
use of org.apache.thrift.transport.TMemoryBuffer in project hive by apache.
the class QueryPlan method toThriftJSONString.
public String toThriftJSONString() throws IOException {
org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan();
TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5);
TJSONProtocol oprot = new TJSONProtocol(tmb);
try {
q.write(oprot);
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return q.toString();
}
return tmb.toString("UTF-8");
}
use of org.apache.thrift.transport.TMemoryBuffer in project pinpoint by naver.
the class BytesUtilsTest method writeVInt32.
private TMemoryBuffer writeVInt32(int i) throws TException {
TMemoryBuffer tMemoryBuffer = new TMemoryBuffer(10);
TCompactProtocol tCompactProtocol = new TCompactProtocol(tMemoryBuffer);
tCompactProtocol.writeI32(i);
return tMemoryBuffer;
}
Aggregations