Search in sources :

Example 1 with TStruct

use of org.apache.thrift.protocol.TStruct in project providence by morimekta.

the class TProtocolSerializer method writeMessage.

private void writeMessage(PMessage<?, ?> message, TProtocol protocol) throws TException, SerializerException {
    PMessageDescriptor<?, ?> type = message.descriptor();
    protocol.writeStructBegin(new TStruct(message.descriptor().getQualifiedName()));
    for (PField field : type.getFields()) {
        if (!message.has(field.getId())) {
            continue;
        }
        protocol.writeFieldBegin(new TField(field.getName(), forType(field.getDescriptor().getType()), (short) field.getId()));
        writeTypedValue(message.get(field.getId()), field.getDescriptor(), protocol);
        protocol.writeFieldEnd();
    }
    protocol.writeFieldStop();
    protocol.writeStructEnd();
}
Also used : TField(org.apache.thrift.protocol.TField) PField(net.morimekta.providence.descriptor.PField) TStruct(org.apache.thrift.protocol.TStruct)

Example 2 with TStruct

use of org.apache.thrift.protocol.TStruct in project parquet-mr by apache.

the class ProtocolReadToWrite method readOneStruct.

private void readOneStruct(TProtocol in, TProtocol out) throws TException {
    final TStruct struct = in.readStructBegin();
    out.writeStructBegin(struct);
    TField field;
    while ((field = in.readFieldBegin()).type != TType.STOP) {
        out.writeFieldBegin(field);
        readOneValue(in, out, field.type);
        in.readFieldEnd();
        out.writeFieldEnd();
    }
    out.writeFieldStop();
    in.readStructEnd();
    out.writeStructEnd();
}
Also used : TField(org.apache.thrift.protocol.TField) TStruct(org.apache.thrift.protocol.TStruct)

Example 3 with TStruct

use of org.apache.thrift.protocol.TStruct in project parquet-mr by apache.

the class BufferedProtocolReadToWrite method readOneStruct.

private boolean readOneStruct(TProtocol in, List<Action> buffer, StructType type) throws TException {
    final TStruct struct = in.readStructBegin();
    buffer.add(new Action() {

        @Override
        public void write(TProtocol out) throws TException {
            out.writeStructBegin(struct);
        }

        @Override
        public String toDebugString() {
            return "(";
        }
    });
    TField field;
    boolean hasFieldsIgnored = false;
    int childFieldsPresent = 0;
    while ((field = in.readFieldBegin()).type != TType.STOP) {
        final TField currentField = field;
        ThriftField expectedField;
        if ((expectedField = type.getChildById(field.id)) == null) {
            handleUnrecognizedField(field, type, in);
            hasFieldsIgnored |= true;
            continue;
        }
        childFieldsPresent++;
        buffer.add(new Action() {

            @Override
            public void write(TProtocol out) throws TException {
                out.writeFieldBegin(currentField);
            }

            @Override
            public String toDebugString() {
                return "f=" + currentField.id + "<t=" + typeName(currentField.type) + ">: ";
            }
        });
        hasFieldsIgnored |= readOneValue(in, field.type, buffer, expectedField.getType());
        in.readFieldEnd();
        buffer.add(FIELD_END);
    }
    // check that union had exactly 1 (no more no less) child fields.
    assertUnionHasExactlyOneChild(type, childFieldsPresent);
    in.readStructEnd();
    buffer.add(STRUCT_END);
    return hasFieldsIgnored;
}
Also used : TException(org.apache.thrift.TException) TField(org.apache.thrift.protocol.TField) TProtocol(org.apache.thrift.protocol.TProtocol) TStruct(org.apache.thrift.protocol.TStruct) ThriftField(org.apache.parquet.thrift.struct.ThriftField)

Example 4 with TStruct

use of org.apache.thrift.protocol.TStruct in project hive by apache.

the class TestTCTLSeparatedProtocol method testNulls.

@Test
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);
    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[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);
}
Also used : TMemoryBuffer(org.apache.thrift.transport.TMemoryBuffer) TConfiguration(org.apache.thrift.TConfiguration) 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) TMap(org.apache.thrift.protocol.TMap) Test(org.junit.Test)

Example 5 with TStruct

use of org.apache.thrift.protocol.TStruct in project hive by apache.

the class TestTCTLSeparatedProtocol method testWrites.

@Test
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);
    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[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();
    // should return nulls at end
    prot.readFieldBegin();
    assertNull(prot.readString());
    prot.readFieldEnd();
    // should 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) TField(org.apache.thrift.protocol.TField) TConfiguration(org.apache.thrift.TConfiguration) Configuration(org.apache.hadoop.conf.Configuration) TStruct(org.apache.thrift.protocol.TStruct) Properties(java.util.Properties) TCTLSeparatedProtocol(org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol) TMap(org.apache.thrift.protocol.TMap) Test(org.junit.Test)

Aggregations

TStruct (org.apache.thrift.protocol.TStruct)8 TField (org.apache.thrift.protocol.TField)6 Properties (java.util.Properties)3 Configuration (org.apache.hadoop.conf.Configuration)3 TCTLSeparatedProtocol (org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol)3 TConfiguration (org.apache.thrift.TConfiguration)3 TException (org.apache.thrift.TException)3 TProtocol (org.apache.thrift.protocol.TProtocol)3 TMemoryBuffer (org.apache.thrift.transport.TMemoryBuffer)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 TList (org.apache.thrift.protocol.TList)2 TMap (org.apache.thrift.protocol.TMap)2 TMessage (org.apache.thrift.protocol.TMessage)2 Invocation (com.alibaba.dubbo.rpc.Invocation)1 PField (net.morimekta.providence.descriptor.PField)1 Invocation (org.apache.dubbo.rpc.Invocation)1 ThriftField (org.apache.parquet.thrift.struct.ThriftField)1