Search in sources :

Example 6 with TMemoryBuffer

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();
}
Also used : TMemoryBuffer(org.apache.thrift.transport.TMemoryBuffer) Configuration(org.apache.hadoop.conf.Configuration) Properties(java.util.Properties) TCTLSeparatedProtocol(org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol)

Example 7 with TMemoryBuffer

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();
}
Also used : TList(org.apache.thrift.protocol.TList) TMemoryBuffer(org.apache.thrift.transport.TMemoryBuffer) Configuration(org.apache.hadoop.conf.Configuration) TField(org.apache.thrift.protocol.TField) Properties(java.util.Properties) TStruct(org.apache.thrift.protocol.TStruct) TCTLSeparatedProtocol(org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol)

Example 8 with TMemoryBuffer

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();
}
Also used : TException(org.apache.thrift.TException) TMemoryBuffer(org.apache.thrift.transport.TMemoryBuffer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol)

Example 9 with TMemoryBuffer

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");
}
Also used : TException(org.apache.thrift.TException) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) TMemoryBuffer(org.apache.thrift.transport.TMemoryBuffer)

Example 10 with TMemoryBuffer

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;
}
Also used : TMemoryBuffer(org.apache.thrift.transport.TMemoryBuffer) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol)

Aggregations

TMemoryBuffer (org.apache.thrift.transport.TMemoryBuffer)12 Properties (java.util.Properties)5 Configuration (org.apache.hadoop.conf.Configuration)5 TCTLSeparatedProtocol (org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol)5 TException (org.apache.thrift.TException)4 TField (org.apache.thrift.protocol.TField)3 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)3 TMap (org.apache.thrift.protocol.TMap)3 TStruct (org.apache.thrift.protocol.TStruct)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 TList (org.apache.thrift.protocol.TList)2 IOException (java.io.IOException)1 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)1 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)1 TProtocol (org.apache.thrift.protocol.TProtocol)1 TSimpleJSONProtocol (org.apache.thrift.protocol.TSimpleJSONProtocol)1 Test (org.junit.Test)1