Search code examples
javahadoopinputthriftapache-kafka

java.io.IOException: org.apache.thrift.protocol.TProtocolException: Cannot write a TUnion with no set value


I am using Thrift scheme to store Thrift bundles as pail file in hadoop cluster. Everything seems to be working correctly. Thrift bundle is being created without any errors.

Although,I am using kafka to send the bundle and while serializing,the serializer function converts the bundle in to byte array.I am getting the above mentioned error at this point. Why would kafka look in to bundle object for converting in to byte array. Or is there any way so that I can convert any object to byte array safely.If so can you please provide it.The error is :

java.io.IOException: org.apache.thrift.protocol.TProtocolException: Cannot write a TUnion with no set value!

following is the writeObject function that is throwing the error

private void writeObject(java.io.ObjectOutputStream out)
        throws java.io.IOException {
    try {
        write(new org.apache.thrift.protocol.TCompactProtocol(
                new org.apache.thrift.transport.TIOStreamTransport(out)));
    } catch (org.apache.thrift.TException te) {
        throw new java.io.IOException(te);
    }
}

Solution

  • The message is quite clear: There is somewhere an union object where no value is set. Exactly one value must be set for Thrift unions.

    Thrift union example, see your *.thrift file:

    union Foo {
      1: string bar
      2: bool baz
    }
    

    This is the condition that raised the error:

    public void write(TProtocol oprot, TUnion struct) throws TException {
      if (struct.getSetField() == null || struct.getFieldValue() == null) {
        throw new TProtocolException("Cannot write a TUnion with no set value!");
      }
    
      // ... more code ...
    }
    

    Can't tell what object it is from your given code, you are too deep in the stack. Look at the code that calls writeObject(), then walk upwards. At some place in your code some data are not set as they should, or the value is set to a null value - which is illegal with Thrift. If you do need an indicator for null values fopr your use case, consider an extra boolean flag.