Example usage for org.apache.thrift TDeserializer deserialize

List of usage examples for org.apache.thrift TDeserializer deserialize

Introduction

In this page you can find the example usage for org.apache.thrift TDeserializer deserialize.

Prototype

public void deserialize(TBase base, byte[] bytes) throws TException 

Source Link

Document

Deserialize the Thrift object from a byte array.

Usage

From source file:com.cloudera.impala.common.JniUtil.java

License:Apache License

/**
     * Deserialize a serialized form of a Thrift data structure to its object form.
     *///from w  w w.  j  a va2  s . c o  m
    public static <T extends TBase<?, ?>, F extends TProtocolFactory> void deserializeThrift(F protocolFactory,
            T result, byte[] thriftData) throws ImpalaException {
        // TODO: avoid creating deserializer for each query?
        TDeserializer deserializer = new TDeserializer(protocolFactory);
        try {
            deserializer.deserialize(result, thriftData);
        } catch (TException e) {
            throw new InternalException(e.getMessage());
        }
    }

From source file:com.dse.pig.udfs.AbstractCassandraStorage.java

License:Apache License

/** convert string back to CfDef */
protected static CfDef cfdefFromString(String st) throws IOException {
    assert st != null;
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    CfDef cfDef = new CfDef();
    try {/* w  ww  .j  a v  a  2  s . c om*/
        deserializer.deserialize(cfDef, Hex.hexToBytes(st));
    } catch (TException e) {
        throw new IOException(e);
    }
    return cfDef;
}

From source file:com.facebook.buck.distributed.thrift.BuckJobTest.java

License:Apache License

@Test
public void testSerializeDeserialize() throws Exception {
    BuckJob jobToSerialize = new BuckJob();
    jobToSerialize.setId(JOB_ID);//  ww  w . j  a  v  a  2 s .c  o  m
    jobToSerialize.setTimestamp_ms(JOB_TS);

    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    byte[] serializedBytes = serializer.serialize(jobToSerialize);

    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    BuckJob deserializedJob = new BuckJob();
    deserializer.deserialize(deserializedJob, serializedBytes);

    assertEquals(JOB_ID, deserializedJob.getId());
    assertEquals(JOB_TS, deserializedJob.getTimestamp_ms());
}

From source file:com.facebook.buck.log.ThriftRuleKeyLoggerTest.java

License:Apache License

@Test
public void testWritesToGivenFileAndCreatesDirectories() throws Exception {
    Path logPath = logDir.resolve("logs").resolve("out.log").toAbsolutePath();
    try (ThriftRuleKeyLogger logger = ThriftRuleKeyLogger.create(logPath)) {
        Assert.assertNotNull(logger);// www.  ja v a2s.  co m
        logger.write(new FullRuleKey("rule_key", "name", "rule_type",
                ImmutableMap.of("key", Value.stringValue("value"))));
    }
    Assert.assertTrue(logPath.toFile().exists());

    ByteBuffer lengthBuf = ByteBuffer.allocate(4);
    try (FileInputStream logFileStream = new FileInputStream(logPath.toFile())) {
        Assert.assertTrue(logFileStream.available() > 4);

        logFileStream.read(lengthBuf.array());
        int length = lengthBuf.getInt();

        Assert.assertEquals(length, logFileStream.available()); // Only should have one object

        byte[] serialized = new byte[length];
        logFileStream.read(serialized);
        TDeserializer serializer = new TDeserializer(new TCompactProtocol.Factory());
        FullRuleKey ruleKey = new FullRuleKey();
        serializer.deserialize(ruleKey, serialized);

        Assert.assertEquals("rule_key", ruleKey.key);
        Assert.assertEquals("name", ruleKey.name);
        Assert.assertEquals("rule_type", ruleKey.type);
        Assert.assertEquals(1, ruleKey.values.size());
        Assert.assertEquals("value", ruleKey.values.get("key").getStringValue());
    }
}

From source file:com.facebook.buck.rules.keys.ThriftRuleKeyHasherTest.java

License:Apache License

private FullRuleKey getRuleKey() throws TException {
    byte[] outputArray = output.toByteArray();
    ByteBuffer lengthBuf = ByteBuffer.wrap(outputArray, 0, 4);
    int length = lengthBuf.getInt();

    Assert.assertEquals(outputArray.length - 4, length);

    TDeserializer serializer = new TDeserializer(new TCompactProtocol.Factory());
    FullRuleKey ruleKey = new FullRuleKey();
    serializer.deserialize(ruleKey, Arrays.copyOfRange(outputArray, 4, outputArray.length));
    return ruleKey;
}

From source file:com.facebook.buck.slb.ThriftUtil.java

License:Apache License

public static void deserialize(ThriftProtocol protocol, byte[] source, TBase<?, ?> dest)
        throws ThriftException {
    TDeserializer deserializer = new TDeserializer(getProtocolFactory(protocol));
    dest.clear();//from ww  w  . j  a  v a2 s .  com
    try {
        deserializer.deserialize(dest, source);
    } catch (TException e) {
        throw new ThriftException(e);
    }
}

From source file:com.facebook.buck.tools.consistency.RuleKeyLogFileReader.java

License:Apache License

/**
 * Reads a file in and runs a predicate on each deserialized rule key
 *
 * @param filename The name of the file to read
 * @param visitor Called for each rule key that gets deserialized. Return {@code true} if
 *     deserialization should halt, or false if it should proceed
 * @throws ParseException There was an error reading data, or invalid data was found in the file
 *///from www  . java 2  s. c  o  m
public void readFile(Path filename, Predicate<FullRuleKey> visitor) throws ParseException {
    ByteBuffer lengthBuf = ByteBuffer.allocate(4);
    try (FileInputStream fileInputStream = new FileInputStream(filename.toFile())) {
        while (fileInputStream.available() >= 4) {
            fileInputStream.read(lengthBuf.array());
            int length = lengthBuf.getInt();
            lengthBuf.rewind();

            byte[] serialized = new byte[length];
            int bytesRead = fileInputStream.read(serialized);
            if (bytesRead != length) {
                throw new ParseException(filename, "Invalid length specified. Expected %s bytes, only got %s",
                        length, bytesRead);
            }
            TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());
            FullRuleKey ruleKey = new FullRuleKey();
            deserializer.deserialize(ruleKey, serialized);
            // The thrift deserializer doesn't blow up on invalid data, it just sets all fields to
            // null. 'key' is required, so if it's null, we failed to deserialize. Yes, deserialize()
            // /should/ throw a TException, but it doesn't.
            if (ruleKey.key == null) {
                throw new ParseException(filename, "Could not deserialize array of size %s", serialized.length);
            }
            if (visitor.test(ruleKey)) {
                return;
            }
        }
    } catch (TException | IOException e) {
        throw new ParseException(e, filename, "Error reading file: %s", e.getMessage());
    }
}

From source file:com.facebook.buck.util.ThriftRuleKeyDeserializer.java

License:Apache License

/**
 * Reads in a list of rule keys from a file
 *
 * @param logPath The path to the file//from  w w w  .  j a va 2s. co  m
 * @return A list of FullRuleKey objects from the file
 * @throws IOException Could not read the file
 * @throws TException Could not deserialize an entry from the file
 */
public static List<FullRuleKey> readRuleKeys(Path logPath) throws IOException, TException {
    ByteBuffer lengthBuf = ByteBuffer.allocate(4);
    List<FullRuleKey> ret = new ArrayList<>();
    try (FileInputStream logFileStream = new FileInputStream(logPath.toAbsolutePath().toString())) {
        while (logFileStream.available() > 0) {
            logFileStream.read(lengthBuf.array());
            int length = lengthBuf.getInt();
            lengthBuf.rewind();

            byte[] serialized = new byte[length];
            logFileStream.read(serialized);
            TDeserializer serializer = new TDeserializer(new TCompactProtocol.Factory());
            FullRuleKey ruleKey = new FullRuleKey();
            serializer.deserialize(ruleKey, serialized);
            ret.add(ruleKey);
        }
        return ret;
    }
}

From source file:com.hopped.runner.rabbitmq.RPCClient.java

License:Open Source License

/**
 * @param request/*from  ww w . j a v a2 s  . co m*/
 * @return
 * @throws Exception
 */
public RunList getRunsByUser(RunRequest request) throws Exception {
    RunList response = new RunList();
    String corrId = java.util.UUID.randomUUID().toString();

    BasicProperties props = new BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName).build();
    TSerializer serializer = new TSerializer();
    channel.basicPublish("", requestQueueName, props, serializer.serialize(request));

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        if (delivery.getProperties().getCorrelationId().equals(corrId)) {
            TDeserializer deserializer = new TDeserializer();
            deserializer.deserialize(response, delivery.getBody());
            break;
        }
    }

    return response;
}

From source file:com.inmobi.messaging.publisher.TestPublisher.java

License:Apache License

@Test
public void testAuditMessage() throws IOException, InterruptedException, TException, EndOfStreamException {
    ClientConfig conf = new ClientConfig();
    conf.set("publisher.classname", "com.inmobi.messaging.publisher.MockInMemoryPublisher");
    conf.set(AuditService.WINDOW_SIZE_KEY, "60");
    conf.set(AuditService.AGGREGATE_WINDOW_KEY, "5");
    conf.set(AbstractMessagePublisher.AUDIT_ENABLED_KEY, "true");

    MessagePublisher publisher = MessagePublisherFactory.create(conf, MockInMemoryPublisher.class.getName());
    publisher.publish("topic", new Message("message".getBytes()));
    publisher.close();/*from   w  ww .jav a  2s  .com*/
    conf.set("topic.name", AuditUtil.AUDIT_STREAM_TOPIC_NAME);
    conf.set("consumer.name", "c1");
    MessageConsumer consumer = MessageConsumerFactory.create(conf, MockInMemoryConsumer.class.getName());
    ((MockInMemoryConsumer) consumer).setSource(((MockInMemoryPublisher) (publisher)).source);
    Message m = consumer.next();
    TDeserializer deserializer = new TDeserializer();
    AuditMessage audit = new AuditMessage();
    deserializer.deserialize(audit, m.getData().array());
    Collection<Long> values = audit.getReceived().values();
    assert (values.iterator().hasNext());
    assert (values.iterator().next() == 1);

}