Example usage for org.apache.thrift TSerializer serialize

List of usage examples for org.apache.thrift TSerializer serialize

Introduction

In this page you can find the example usage for org.apache.thrift TSerializer serialize.

Prototype

public byte[] serialize(TBase base) throws TException 

Source Link

Document

Serialize the Thrift object into a byte array.

Usage

From source file:adept.mappers.thrift.MappingTests.java

License:Apache License

private void SerializationTests(final adept.common.HltContentContainer hltAdept)
        throws TException, IOException {
    final XMLSerializer xmlSerializer = new XMLSerializer(SerializationType.XML);
    final TSerializer thriftSerializer = new TSerializer(new TBinaryProtocol.Factory());
    final TDeserializer tdeserializer = new TDeserializer(new TBinaryProtocol.Factory());
    final ThriftAdeptMapper mapper = ThriftAdeptMapper.getInstance();

    // thrift round tripping through serialization
    final thrift.adept.common.HltContentContainer hltThrift = mapper.convert(hltAdept);
    final byte[] thriftBytes = thriftSerializer.serialize(hltThrift);
    final File thriftFile = File.createTempFile("thriftBytes", "thrift");
    thriftFile.deleteOnExit();/*w w  w . ja  va2 s  .c o m*/
    Files.asByteSink(thriftFile).write(thriftBytes);
    final thrift.adept.common.HltContentContainer serializationTrippedThrift = new thrift.adept.common.HltContentContainer();
    tdeserializer.deserialize(serializationTrippedThrift, Files.asByteSource(thriftFile).read());

    // adept fun times
    final String adeptString = xmlSerializer.serializeAsString(hltAdept);
    final File adeptFile = File.createTempFile("adeptString", "xml");
    adeptFile.deleteOnExit();
    Files.asCharSink(adeptFile, Charsets.UTF_8).write(adeptString);
    final adept.common.HltContentContainer serializedTrippedAdept = (adept.common.HltContentContainer) xmlSerializer
            .deserializeString(Files.asCharSource(adeptFile, Charsets.UTF_8).read(),
                    adept.common.HltContentContainer.class);

    assertTrue(areTheSameByLimitedThriftScope(hltAdept, hltThrift));
    // check that to/from serialization does the right thing
    assertTrue(areTheSameByLimitedThriftScope(serializedTrippedAdept, hltThrift));
    assertTrue(areTheSameByLimitedThriftScope(hltAdept, serializationTrippedThrift));
    assertTrue(areTheSameByLimitedThriftScope(serializedTrippedAdept, serializationTrippedThrift));
}

From source file:andromache.config.CassandraConfigHelper.java

License:Apache License

private static String thriftToString(TBase object) {
    assert object != null;
    // this is so awful it's kind of cool!
    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    try {//from   ww  w .  ja v  a 2  s.co m
        return Hex.bytesToHex(serializer.serialize(object));
    } catch (TException e) {
        throw new RuntimeException(e);
    }
}

From source file:backtype.storm.utils.LocalState.java

License:Apache License

private void persistInternal(Map<String, ThriftSerializedObject> serialized, TSerializer ser, boolean cleanup) {
    try {// w ww .java 2  s .c  o  m
        if (ser == null) {
            ser = new TSerializer();
        }
        byte[] toWrite = ser.serialize(new LocalStateData(serialized));

        String newPath = _vs.createVersion();
        File file = new File(newPath);
        FileUtils.writeByteArrayToFile(file, toWrite);
        if (toWrite.length != file.length()) {
            throw new IOException("Tried to serialize " + toWrite.length + " bytes to "
                    + file.getCanonicalPath() + ", but " + file.length() + " bytes were written.");
        }
        _vs.succeedVersion(newPath);
        if (cleanup)
            _vs.cleanup(4);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:backtype.storm.utils.LocalState.java

License:Apache License

private ThriftSerializedObject serialize(TBase o, TSerializer ser) {
    try {//from  ww w  .jav  a2s.  c  o m
        return new ThriftSerializedObject(o.getClass().getName(), ByteBuffer.wrap(ser.serialize((TBase) o)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.usi.da.dlog.message.Command.java

License:Open Source License

public static byte[] toByteArray(Command c) {
    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    Cmd cmd = toCmd(c);//from w w w  . ja v  a 2  s  .  c o m
    try {
        return serializer.serialize(cmd);
    } catch (TException e) {
        return new byte[0];
    }
}

From source file:ch.usi.da.dlog.message.Message.java

License:Open Source License

public static byte[] toByteArray(Message m) {
    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    ch.usi.da.dlog.thrift.gen.Message msg = new ch.usi.da.dlog.thrift.gen.Message();
    msg.setId(m.getID());//from  w  ww  .j a v a 2  s .c  om
    msg.setFrom(m.getFrom());
    msg.setTo(m.getTo());
    List<Cmd> cmds = new ArrayList<Cmd>();
    for (Command c : m.getCommands()) {
        cmds.add(Command.toCmd(c));
    }
    msg.setCommands(cmds);
    try {
        return serializer.serialize(msg);
    } catch (TException e) {
        return new byte[0];
    }
}

From source file:ch.usi.da.smr.message.Message.java

License:Open Source License

public static byte[] toByteArray(Message m) {
    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    ch.usi.da.smr.thrift.gen.Message msg = new ch.usi.da.smr.thrift.gen.Message();
    msg.setId(m.getID());//w  w w .  j a  v  a2 s.c o m
    msg.setFrom(m.getFrom());
    msg.setTo(m.getTo());
    List<Cmd> cmds = new ArrayList<Cmd>();
    for (Command c : m.getCommands()) {
        cmds.add(Command.toCmd(c));
    }
    msg.setCommands(cmds);
    try {
        return serializer.serialize(msg);
    } catch (TException e) {
        return new byte[0];
    }
}

From source file:co.cask.tephra.TransactionCodec.java

License:Apache License

public byte[] encode(Transaction tx) throws IOException {
    TTransaction thriftTx = TransactionConverterUtils.wrap(tx);
    TSerializer serializer = new TSerializer();
    try {//  ww w  .j  av  a 2 s.c o m
        return serializer.serialize(thriftTx);
    } catch (TException te) {
        throw new IOException(te);
    }
}

From source file:com.bigdata.dastor.utils.FBUtilities.java

License:Apache License

public static void serialize(TSerializer serializer, TBase struct, DataOutput out) throws IOException {
    assert serializer != null;
    assert struct != null;
    assert out != null;
    byte[] bytes;
    try {/*from  w  w w .j  a  v  a2s.c  o m*/
        bytes = serializer.serialize(struct);
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    out.writeInt(bytes.length);
    out.write(bytes);
}

From source file:com.cloudera.impala.catalog.Db.java

License:Apache License

/**
 * Adds the user defined function fn to metastore DB params. fn is
 * serialized to thrift using TBinaryProtocol and then base64-encoded
 * to be compatible with the HMS' representation of params.
 *//* w  w w. j a v a 2 s.  c  om*/
private boolean addFunctionToDbParams(Function fn) {
    Preconditions.checkState(fn.getBinaryType() != TFunctionBinaryType.BUILTIN
            && fn.getBinaryType() != TFunctionBinaryType.JAVA);
    try {
        TSerializer serializer = new TSerializer(new TCompactProtocol.Factory());
        byte[] serializedFn = serializer.serialize(fn.toThrift());
        String base64Fn = Base64.encodeBase64String(serializedFn);
        String fnKey = FUNCTION_INDEX_PREFIX + fn.signatureString();
        if (base64Fn.length() > HIVE_METASTORE_DB_PARAM_LIMIT_BYTES) {
            throw new ImpalaRuntimeException("Serialized function size exceeded HMS 4K byte limit");
        }
        putToHmsParameters(fnKey, base64Fn);
    } catch (ImpalaException | TException e) {
        LOG.error("Error adding function " + fn.getName() + " to DB params", e);
        return false;
    }
    return true;
}