Search in sources :

Example 1 with TSet

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

the class DynamicSerDeTypeSet method serialize.

@Override
public void serialize(Object o, ObjectInspector oi, TProtocol oprot) throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
    ListObjectInspector loi = (ListObjectInspector) oi;
    Set<Object> set = (Set<Object>) o;
    DynamicSerDeTypeBase mt = getElementType();
    tset = new TSet(mt.getType(), set.size());
    oprot.writeSetBegin(tset);
    for (Object element : set) {
        mt.serialize(element, loi.getListElementObjectInspector(), oprot);
    }
    // in theory, the below call isn't needed in non thrift_mode, but let's not
    // get too crazy
    oprot.writeSetEnd();
}
Also used : HashSet(java.util.HashSet) TSet(org.apache.thrift.protocol.TSet) Set(java.util.Set) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) TSet(org.apache.thrift.protocol.TSet)

Example 2 with TSet

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

the class DynamicSerDeTypeSet method deserialize.

/**
 * NOTE: Set is not supported by Hive yet.
 */
@Override
public Object deserialize(Object reuse, TProtocol iprot) throws SerDeException, TException, IllegalAccessException {
    TSet theset = iprot.readSetBegin();
    if (theset == null) {
        return null;
    }
    Set<Object> result;
    if (reuse != null) {
        result = (Set<Object>) reuse;
        result.clear();
    } else {
        result = new HashSet<Object>();
    }
    for (int i = 0; i < theset.size; i++) {
        Object elem = getElementType().deserialize(null, iprot);
        result.add(elem);
    }
    // in theory, the below call isn't needed in non thrift_mode, but let's not
    // get too crazy
    iprot.readSetEnd();
    return result;
}
Also used : TSet(org.apache.thrift.protocol.TSet)

Example 3 with TSet

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

the class TProtocolSerializer method readTypedValue.

private Object readTypedValue(byte tType, PDescriptor type, TProtocol protocol, boolean allowNull) throws TException, SerializerException {
    if (tType != forType(type.getType())) {
        throw new SerializerException("Expected type " + asString(forType(type.getType())) + " but found " + asString(tType));
    }
    switch(tType) {
        case BinaryType.BOOL:
            return protocol.readBool();
        case BinaryType.BYTE:
            return protocol.readByte();
        case BinaryType.I16:
            return protocol.readI16();
        case BinaryType.I32:
            if (PType.ENUM == type.getType()) {
                PEnumDescriptor<?> et = (PEnumDescriptor<?>) type;
                PEnumBuilder<?> eb = et.builder();
                int value = protocol.readI32();
                eb.setById(value);
                if (!eb.valid() && !allowNull) {
                    throw new SerializerException("Invalid enum value " + value + " for " + et.getQualifiedName());
                }
                return eb.build();
            } else {
                return protocol.readI32();
            }
        case BinaryType.I64:
            return protocol.readI64();
        case BinaryType.DOUBLE:
            return protocol.readDouble();
        case BinaryType.STRING:
            if (type == PPrimitive.BINARY) {
                ByteBuffer buffer = protocol.readBinary();
                return Binary.wrap(buffer.array());
            }
            return protocol.readString();
        case BinaryType.STRUCT:
            return readMessage(protocol, (PMessageDescriptor<?, ?>) type);
        case BinaryType.LIST:
            TList listInfo = protocol.readListBegin();
            PList<Object> lDesc = (PList<Object>) type;
            PDescriptor liDesc = lDesc.itemDescriptor();
            PList.Builder<Object> list = lDesc.builder();
            for (int i = 0; i < listInfo.size; ++i) {
                list.add(readTypedValue(listInfo.elemType, liDesc, protocol, false));
            }
            protocol.readListEnd();
            return list.build();
        case BinaryType.SET:
            TSet setInfo = protocol.readSetBegin();
            PSet<Object> sDesc = (PSet<Object>) type;
            PDescriptor siDesc = sDesc.itemDescriptor();
            PSet.Builder<Object> set = sDesc.builder();
            for (int i = 0; i < setInfo.size; ++i) {
                set.add(readTypedValue(setInfo.elemType, siDesc, protocol, false));
            }
            protocol.readSetEnd();
            return set.build();
        case BinaryType.MAP:
            TMap mapInfo = protocol.readMapBegin();
            PMap<Object, Object> mDesc = (PMap<Object, Object>) type;
            PDescriptor mkDesc = mDesc.keyDescriptor();
            PDescriptor miDesc = mDesc.itemDescriptor();
            PMap.Builder<Object, Object> map = mDesc.builder();
            for (int i = 0; i < mapInfo.size; ++i) {
                Object key = readTypedValue(mapInfo.keyType, mkDesc, protocol, false);
                Object val = readTypedValue(mapInfo.valueType, miDesc, protocol, false);
                map.put(key, val);
            }
            protocol.readMapEnd();
            return map.build();
        default:
            throw new SerializerException("Unsupported protocol field type: " + tType);
    }
}
Also used : PList(net.morimekta.providence.descriptor.PList) TSet(org.apache.thrift.protocol.TSet) PMap(net.morimekta.providence.descriptor.PMap) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) SerializerException(net.morimekta.providence.serializer.SerializerException) ByteBuffer(java.nio.ByteBuffer) TMap(org.apache.thrift.protocol.TMap) TList(org.apache.thrift.protocol.TList) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PSet(net.morimekta.providence.descriptor.PSet)

Example 4 with TSet

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

the class TProtocolSerializer method writeTypedValue.

private void writeTypedValue(Object item, PDescriptor type, TProtocol protocol) throws TException, SerializerException {
    switch(type.getType()) {
        case BOOL:
            protocol.writeBool((Boolean) item);
            break;
        case BYTE:
            protocol.writeByte((Byte) item);
            break;
        case I16:
            protocol.writeI16((Short) item);
            break;
        case I32:
            protocol.writeI32((Integer) item);
            break;
        case I64:
            protocol.writeI64((Long) item);
            break;
        case DOUBLE:
            protocol.writeDouble((Double) item);
            break;
        case STRING:
            protocol.writeString((String) item);
            break;
        case BINARY:
            protocol.writeBinary(((Binary) item).getByteBuffer());
            break;
        case ENUM:
            PEnumValue<?> value = (PEnumValue<?>) item;
            protocol.writeI32(value.asInteger());
            break;
        case MESSAGE:
            writeMessage((PMessage<?, ?>) item, protocol);
            break;
        case LIST:
            PList<?> lType = (PList<?>) type;
            List<?> list = (List<?>) item;
            TList listInfo = new TList(forType(lType.itemDescriptor().getType()), list.size());
            protocol.writeListBegin(listInfo);
            for (Object i : list) {
                writeTypedValue(i, lType.itemDescriptor(), protocol);
            }
            protocol.writeListEnd();
            break;
        case SET:
            PSet<?> sType = (PSet<?>) type;
            Set<?> set = (Set<?>) item;
            TSet setInfo = new TSet(forType(sType.itemDescriptor().getType()), set.size());
            protocol.writeSetBegin(setInfo);
            for (Object i : set) {
                writeTypedValue(i, sType.itemDescriptor(), protocol);
            }
            protocol.writeSetEnd();
            break;
        case MAP:
            PMap<?, ?> mType = (PMap<?, ?>) type;
            Map<?, ?> map = (Map<?, ?>) item;
            protocol.writeMapBegin(new TMap(forType(mType.keyDescriptor().getType()), forType(mType.itemDescriptor().getType()), map.size()));
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                writeTypedValue(entry.getKey(), mType.keyDescriptor(), protocol);
                writeTypedValue(entry.getValue(), mType.itemDescriptor(), protocol);
            }
            protocol.writeMapEnd();
            break;
        default:
            break;
    }
}
Also used : TSet(org.apache.thrift.protocol.TSet) Set(java.util.Set) PSet(net.morimekta.providence.descriptor.PSet) PList(net.morimekta.providence.descriptor.PList) PEnumValue(net.morimekta.providence.PEnumValue) TSet(org.apache.thrift.protocol.TSet) PMap(net.morimekta.providence.descriptor.PMap) TMap(org.apache.thrift.protocol.TMap) TList(org.apache.thrift.protocol.TList) PSet(net.morimekta.providence.descriptor.PSet) TList(org.apache.thrift.protocol.TList) PList(net.morimekta.providence.descriptor.PList) List(java.util.List) Map(java.util.Map) PMap(net.morimekta.providence.descriptor.PMap) TMap(org.apache.thrift.protocol.TMap)

Example 5 with TSet

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

the class DefaultEventsVisitor method visit.

@Override
public Void visit(final ThriftType.SetType setType, Void v) {
    dummyEvents.add(new ParquetProtocol("readSetBegin()") {

        @Override
        public TSet readSetBegin() throws TException {
            return new TSet();
        }
    });
    dummyEvents.add(new ParquetProtocol("readSetEnd()") {

        @Override
        public void readSetEnd() throws TException {
        }
    });
    return null;
}
Also used : TException(org.apache.thrift.TException) ParquetProtocol(org.apache.parquet.thrift.ParquetProtocol) TSet(org.apache.thrift.protocol.TSet)

Aggregations

TSet (org.apache.thrift.protocol.TSet)13 TList (org.apache.thrift.protocol.TList)4 TMap (org.apache.thrift.protocol.TMap)4 Map (java.util.Map)3 EnumMap (java.util.EnumMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 PList (net.morimekta.providence.descriptor.PList)2 PMap (net.morimekta.providence.descriptor.PMap)2 PSet (net.morimekta.providence.descriptor.PSet)2 TException (org.apache.thrift.TException)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 PEnumValue (net.morimekta.providence.PEnumValue)1 PDescriptor (net.morimekta.providence.descriptor.PDescriptor)1 PEnumDescriptor (net.morimekta.providence.descriptor.PEnumDescriptor)1 SerializerException (net.morimekta.providence.serializer.SerializerException)1 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)1