Java Code Examples for org.apache.thrift.TDeserializer#deserialize()

The following examples show how to use org.apache.thrift.TDeserializer#deserialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColumnSerializationUtilTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeserializeColumn() throws Exception {
  Mutation mutation =
      new Mutation(
          MutationType.DELETE,
          TIMESTAMP,
          SOURCE_ID,
          DATA_SOURCE,
          BINLOG_HEADER,
          TABLE,
          getEntity());

  TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
  TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());

  byte[] serialized = serializer.serialize(mutation);

  Mutation deserialized = new Mutation();
  deserializer.deserialize(deserialized, serialized);

  assertEquals(mutation, deserialized);
}
 
Example 2
Source File: TOMVSTRING.java    From warp10-platform with Apache License 2.0 6 votes vote down vote up
private static StringBuilder bytesToString(StringBuilder sb, byte[] bytes) {
  TDeserializer deser = new TDeserializer(new TCompactProtocol.Factory());
  GTSWrapper wrapper = new GTSWrapper();

  try {
    deser.deserialize(wrapper, bytes);

    return wrapperToString(sb, wrapper);
  } catch (TException te) {
    if (null == sb) {
      sb = new StringBuilder();
    }
    sb.append("b64:");
    sb.append(Base64.encodeBase64URLSafeString(bytes));
    return sb;
  }    
}
 
Example 3
Source File: TOGTS.java    From warp10-platform with Apache License 2.0 6 votes vote down vote up
/**
 * try to decode an encoder from its opb64 string representation or its byte array representation.
 *
 * @param o string, encoder, or byte array
 * @return a GTSDecoder object
 * @throws WarpScriptException
 */
private GTSDecoder getDecoderFromObject(Object o) throws WarpScriptException {
  GTSDecoder decoder;
  if (o instanceof GTSEncoder) {
    decoder = ((GTSEncoder) o).getUnsafeDecoder(false);
  } else {
    try {
      byte[] bytes = o instanceof String ? OrderPreservingBase64.decode(o.toString().getBytes(StandardCharsets.US_ASCII)) : (byte[]) o;
      TDeserializer deser = new TDeserializer(new TCompactProtocol.Factory());
      GTSWrapper wrapper = new GTSWrapper();
      deser.deserialize(wrapper, bytes);
      decoder = GTSWrapperHelper.fromGTSWrapperToGTSDecoder(wrapper);
    } catch (TException te) {
      throw new WarpScriptException(getName() + " failed to unwrap encoder.", te);
    }
  }
  return decoder;
}
 
Example 4
Source File: UNWRAPENCODER.java    From warp10-platform with Apache License 2.0 6 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object top = stack.pop();
  
  if (!(top instanceof String) && !(top instanceof byte[])) {
    throw new WarpScriptException(getName() + " operates on a string or byte array.");
  }
  
  byte[] bytes = top instanceof String ? OrderPreservingBase64.decode(top.toString().getBytes(StandardCharsets.US_ASCII)) : (byte[]) top;
  
  TDeserializer deser = new TDeserializer(new TCompactProtocol.Factory());
  
  try {
    GTSWrapper wrapper = new GTSWrapper();      
    deser.deserialize(wrapper, bytes);

    stack.push(GTSWrapperHelper.fromGTSWrapperToGTSEncoder(wrapper));      
  } catch (TException te) {
    throw new WarpScriptException(getName() + " failed to unwrap encoder.", te);
  } catch (IOException ioe) {
    throw new WarpScriptException(getName() + " failed to unwrap encoder.", ioe);
  }      

  return stack;
}
 
Example 5
Source File: JStormUtils.java    From jstorm with Apache License 2.0 5 votes vote down vote up
public static <T> T thriftDeserialize(Class c, byte[] b, int offset, int length) {
    try {
        T ret = (T) c.newInstance();
        TDeserializer des = getDes();
        des.deserialize((TBase) ret, b, offset, length);
        return ret;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: ThriftSerializer.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public static TBase deserialize(TBase baseObject, byte[] serialized) throws IOException {
  TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory(maxMessageSize, maxMessageSize));
  try {
    deserializer.deserialize(baseObject, serialized);
  } catch (TException e) {
    throw new IOException("Error deserializing thrift object "
        + baseObject, e);
  }
  return baseObject;
}
 
Example 7
Source File: TransactionCodec.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
public Transaction decode(byte[] encoded) throws IOException {
  TTransaction thriftTx = new TTransaction();
  TDeserializer deserializer = new TDeserializer();
  try {
    deserializer.deserialize(thriftTx, encoded);
    return TransactionConverterUtils.unwrap(thriftTx);
  } catch (TException te) {
    throw new IOException(te);
  }
}
 
Example 8
Source File: ThriftJobDaoImpl.java    From plow with Apache License 2.0 5 votes vote down vote up
@Override
public JobSpecT getJobSpec(UUID jobId) {

    final String json = jdbc.queryForObject("SELECT str_thrift_spec FROM job_history WHERE pk_job=?",
            String.class, jobId);

    final TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
    final JobSpecT spec = new JobSpecT();
    try {
        deserializer.deserialize(spec, json.getBytes());
        return spec;
    } catch (TException e) {
        throw new JobSpecException("Failed to parse job spec " + e, e);
    }
}
 
Example 9
Source File: ThriftLogDumper.java    From singer with Apache License 2.0 5 votes vote down vote up
private static TBase deserializeBytesToThriftObj(byte[] bytes, String thriftSchema)
    throws Exception {
  TBase obj = (TBase) Class.forName(thriftSchema).getConstructor().newInstance();
  TDeserializer deserializer = new TDeserializer();
  deserializer.deserialize(obj, bytes);
  return obj;
}
 
Example 10
Source File: ThriftUtil.java    From buck with Apache License 2.0 5 votes vote down vote up
public static void deserialize(ThriftProtocol protocol, byte[] source, TBase<?, ?> dest)
    throws ThriftException {
  TDeserializer deserializer = new TDeserializer(getProtocolFactory(protocol));
  dest.clear();
  try {
    deserializer.deserialize(dest, source);
  } catch (TException e) {
    throw new ThriftException(e);
  }
}
 
Example 11
Source File: PlasmaBackEnd.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
  long count = 0L;
  
  byte[] clslbls = new byte[16];
  
  try {
    ConsumerIterator<byte[],byte[]> iter = this.stream.iterator();

    byte[] inSipHashKey = backend.keystore.getKey(KeyStore.SIPHASH_KAFKA_PLASMA_BACKEND_IN);
    byte[] inAESKey = backend.keystore.getKey(KeyStore.AES_KAFKA_PLASMA_BACKEND_IN);

    byte[] outSipHashKey = backend.keystore.getKey(KeyStore.SIPHASH_KAFKA_PLASMA_BACKEND_OUT);
    byte[] outAESKey = backend.keystore.getKey(KeyStore.AES_KAFKA_PLASMA_BACKEND_OUT);

    // Iterate on the messages
    TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());

    // TODO(hbs): allow setting of writeBufferSize

    while (iter.hasNext()) {
      //
      // Since the cal to 'next' may block, we need to first
      // check that there is a message available
      //
      
      boolean nonEmpty = iter.nonEmpty();
      
      if (nonEmpty) {
        count++;
        MessageAndMetadata<byte[], byte[]> msg = iter.next();
        counters.count(msg.partition(), msg.offset());
        
        // Do nothing if there are no subscriptions
        if (null == backend.subscriptions || backend.subscriptions.isEmpty()) {
          continue;
        }
        
        byte[] data = msg.message();

        Sensision.update(SensisionConstants.SENSISION_CLASS_PLASMA_BACKEND_KAFKA_IN_MESSAGES, Sensision.EMPTY_LABELS, 1);
        Sensision.update(SensisionConstants.SENSISION_CLASS_PLASMA_BACKEND_KAFKA_IN_BYTES, Sensision.EMPTY_LABELS, data.length);
        
        if (null != inSipHashKey) {
          data = CryptoUtils.removeMAC(inSipHashKey, data);
        }
        
        // Skip data whose MAC was not verified successfully
        if (null == data) {
          Sensision.update(SensisionConstants.SENSISION_CLASS_PLASMA_BACKEND_KAFKA_IN_INVALIDMACS, Sensision.EMPTY_LABELS, 1);
          continue;
        }
        
        // Unwrap data if need be
        if (null != inAESKey) {
          data = CryptoUtils.unwrap(inAESKey, data);
        }
        
        // Skip data that was not unwrapped successfully
        if (null == data) {
          Sensision.update(SensisionConstants.SENSISION_CLASS_PLASMA_BACKEND_KAFKA_IN_INVALIDCIPHERS, Sensision.EMPTY_LABELS, 1);
          continue;
        }
        
        //
        // Extract KafkaDataMessage
        //
        
        KafkaDataMessage tmsg = new KafkaDataMessage();
        deserializer.deserialize(tmsg, data);
        
        switch(tmsg.getType()) {
          case STORE:
            backend.dispatch(clslbls, msg, tmsg, outSipHashKey, outAESKey);              
            break;
          case DELETE:
            break;
          default:
            throw new RuntimeException("Invalid message type.");
        }            
      } else {
        // Sleep a tiny while
        try {
          Thread.sleep(1L);
        } catch (InterruptedException ie) {             
        }
      }          
    }        
  } catch (Throwable t) {
    t.printStackTrace(System.err);
  } finally {
    // Set abort to true in case we exit the 'run' method
    backend.abort.set(true);
  }
}
 
Example 12
Source File: StandaloneChunkedMemoryStore.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
private void load(String path) throws IOException {
  
  long nano = System.nanoTime();
  long chunks = 0;
  long datapoints = 0;
  long bytes = 0L;
  
  Configuration conf = new Configuration();
      
  conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
  conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
  
  BytesWritable key = new BytesWritable();
  BytesWritable value = new BytesWritable();
  
  TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());
  
  SequenceFile.Reader.Option optPath = SequenceFile.Reader.file(new Path(path));    
  
  SequenceFile.Reader reader = null;
  
  boolean failsafe = "true".equals(properties.getProperty(io.warp10.continuum.Configuration.STANDALONE_MEMORY_STORE_LOAD_FAILSAFE));
  
  try {
    reader = new SequenceFile.Reader(conf, optPath);

    System.out.println("Loading '" + path + "' back in memory.");

    while(reader.next(key, value)) {
      chunks++;
      GTSWrapper wrapper = new GTSWrapper();
      deserializer.deserialize(wrapper, key.copyBytes());
      GTSEncoder encoder = new GTSEncoder(wrapper.getBase(), null, value.copyBytes());
      encoder.setCount(wrapper.getCount());
      datapoints += wrapper.getCount();
      bytes += value.getLength() + key.getLength();
      if (wrapper.isSetMetadata()) {
        encoder.safeSetMetadata(wrapper.getMetadata());
      } else {
        encoder.safeSetMetadata(new Metadata());
      }
      store(encoder);
      if (null != this.directoryClient) {
        this.directoryClient.register(encoder.getMetadata());
      }
    }
  } catch (FileNotFoundException fnfe) {
    System.err.println("File '" + path + "' was not found, skipping.");
    return;
  } catch (IOException ioe) {
    if (!failsafe) {
      throw ioe;
    } else {
      System.err.println("Ignoring exception " + ioe.getMessage() + ".");
    }
  } catch (Exception e) {
    if (!failsafe) {
      throw new IOException(e);
    } else {
      System.err.println("Ignoring exception " + e.getMessage() + ".");
    }
  }
  
  reader.close();    
  
  nano = System.nanoTime() - nano;
  
  System.out.println("Loaded " + chunks + " chunks (" + datapoints + " datapoints, " + bytes + " bytes) in " + (nano / 1000000.0D) + " ms.");
}
 
Example 13
Source File: Directory.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
void handleStats(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 
  if (!Constants.API_ENDPOINT_DIRECTORY_STATS_INTERNAL.equals(target)) {
    return;
  }
  
  long nano = System.nanoTime();

  baseRequest.setHandled(true);

  //
  // Read DirectoryRequests from stdin
  //
  
  BufferedReader br = new BufferedReader(request.getReader());
  
  while (true) {
    String line = br.readLine();
    
    if (null == line) {
      break;
    }
    
    byte[] raw = OrderPreservingBase64.decode(line.getBytes(StandardCharsets.US_ASCII));

    // Extract DirectoryStatsRequest
    TDeserializer deser = new TDeserializer(new TCompactProtocol.Factory());
    DirectoryStatsRequest req = new DirectoryStatsRequest();
    
    try {
      deser.deserialize(req, raw);
      DirectoryStatsResponse resp = stats(req);

      response.setContentType("text/plain");
      OutputStream out = response.getOutputStream();
            
      TSerializer ser = new TSerializer(new TCompactProtocol.Factory());
      byte[] data = ser.serialize(resp);
      
      OrderPreservingBase64.encodeToStream(data, out);
      
      out.write('\r');
      out.write('\n');
    } catch (TException te) {
      throw new IOException(te);
    }            
  }
}
 
Example 14
Source File: UNWRAPSIZE.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object top = stack.pop();
  
  if (!(top instanceof String) && !(top instanceof byte[]) && !(top instanceof List)) {
    throw new WarpScriptException(getName() + " operates on a string or byte array or a list thereof.");
  }
  
  List<Object> inputs = new ArrayList<Object>();
  
  if (top instanceof String || top instanceof byte[]) {
    inputs.add(top);
  } else {
    for (Object o: (List) top) {
      if (!(o instanceof String) && !(o instanceof byte[])) {
        throw new WarpScriptException(getName() + " operates on a string or byte array or a list thereof.");
      }
      inputs.add(o);
    }
  }
  
  List<Object> outputs = new ArrayList<Object>();
  
  for (Object s: inputs) {
    byte[] bytes = s instanceof String ? OrderPreservingBase64.decode(s.toString().getBytes(StandardCharsets.US_ASCII)) : (byte[]) s;
    
    TDeserializer deser = new TDeserializer(new TCompactProtocol.Factory());
    
    try {
      GTSWrapper wrapper = new GTSWrapper();
      
      deser.deserialize(wrapper, bytes);

      outputs.add(wrapper.getCount());
    } catch (TException te) {
      throw new WarpScriptException(getName() + " failed to unwrap GTS.", te);
    }      
  }
  
  if (!(top instanceof List)) {
    stack.push(outputs.get(0));      
  } else {
    stack.push(outputs);
  }
  
  return stack;
}
 
Example 15
Source File: TOENCODERS.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object top = stack.pop();
  
  if (!(top instanceof String) && !(top instanceof byte[]) && !(top instanceof GTSEncoder)) {
    throw new WarpScriptException(getName() + " operates on a string, byte array or encoder.");
  }
  
  Map<String,GTSEncoder> encoders = new HashMap<String,GTSEncoder>();
  
  GTSDecoder decoder;
  
  if (top instanceof GTSEncoder) {
    decoder = ((GTSEncoder) top).getUnsafeDecoder(false);
  } else {
    try {
      byte[] bytes = top instanceof String ? OrderPreservingBase64.decode(top.toString().getBytes(StandardCharsets.US_ASCII)) : (byte[]) top;
      
      TDeserializer deser = new TDeserializer(new TCompactProtocol.Factory());
      
      GTSWrapper wrapper = new GTSWrapper();
      
      deser.deserialize(wrapper, bytes);

      decoder = GTSWrapperHelper.fromGTSWrapperToGTSDecoder(wrapper);        
    } catch (TException te) {
      throw new WarpScriptException(getName() + " failed to unwrap encoder.", te);
    }            
  }

  GTSEncoder enc;
  
  try {
    while(decoder.next()) {
      Object value = decoder.getBinaryValue();
      
      String type = "DOUBLE";
      
      if (value instanceof String) {
        type = "STRING";
      } else if (value instanceof Boolean) {
        type = "BOOLEAN";
      } else if (value instanceof Long) {
        type = "LONG";
      } else if (value instanceof Double || value instanceof BigDecimal) { 
        type = "DOUBLE";
      } else if (value instanceof byte[]) {
        type = "BINARY";
      }
      
      enc = encoders.get(type);
      
      if (null == enc) {
        enc = new GTSEncoder(0L);
        enc.setMetadata(decoder.getMetadata());
        encoders.put(type, enc);
      }

      enc.addValue(decoder.getTimestamp(), decoder.getLocation(), decoder.getElevation(), value);
    }      
  } catch (Exception e) {
    throw new WarpScriptException(getName() + " encountered an exception during conversion.");
  }
      
  stack.push(encoders);

  return stack;
}
 
Example 16
Source File: ENCODERTO.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object top = stack.pop();
  
  if (!(top instanceof String) && !(top instanceof byte[]) && !(top instanceof GTSEncoder)) {
    throw new WarpScriptException(getName() + " operates on a string, byte array or encoder.");
  }
  
  List<Object> elements = new ArrayList<Object>();
  
  GTSDecoder decoder;
  
  if (top instanceof GTSEncoder) {
    decoder = ((GTSEncoder) top).getDecoder(true);
  } else {
    try {
      byte[] bytes = top instanceof String ? OrderPreservingBase64.decode(top.toString().getBytes(StandardCharsets.US_ASCII)) : (byte[]) top;
      
      TDeserializer deser = new TDeserializer(new TCompactProtocol.Factory());
      
      GTSWrapper wrapper = new GTSWrapper();
      
      deser.deserialize(wrapper, bytes);

      decoder = GTSWrapperHelper.fromGTSWrapperToGTSDecoder(wrapper);
      
    } catch (TException te) {
      throw new WarpScriptException(getName() + " failed to unwrap encoder.", te);
    }            
  }

  while(decoder.next()) {
    List<Object> element = new ArrayList<Object>(5);
    element.add(decoder.getTimestamp());
    long location = decoder.getLocation();
    if (GeoTimeSerie.NO_LOCATION == location) {
      element.add(Double.NaN);
      element.add(Double.NaN);
    } else {
      double[] latlon = GeoXPLib.fromGeoXPPoint(location);
      element.add(latlon[0]);
      element.add(latlon[1]);
    }
    long elevation = decoder.getElevation();
    if (GeoTimeSerie.NO_ELEVATION == elevation) {
      element.add(Double.NaN);
    } else {
      element.add(elevation);
    }
    element.add(decoder.getBinaryValue());
    elements.add(element);
  }
  
  stack.push(decoder.getName());
  stack.push(decoder.getLabels());
  stack.push(decoder.getMetadata().getAttributes());
  stack.push(elements);

  return stack;
}
 
Example 17
Source File: GEOUNPACK.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  
  Object o = stack.pop();
  
  byte[] serialized;
  
  if (o instanceof String) {
    serialized = OrderPreservingBase64.decode(o.toString().getBytes(StandardCharsets.US_ASCII));
  } else if (o instanceof byte[]) {
    serialized = (byte[]) o;
  } else {
    throw new WarpScriptException(getName() + " expects a packed shape on top of the stack.");      
  }
  
  TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());
  
  GTSWrapper wrapper = new GTSWrapper();
  
  try {
    deserializer.deserialize(wrapper, serialized);
  } catch (TException te) {
    throw new WarpScriptException(te);
  }
  
  GTSDecoder decoder = GTSWrapperHelper.fromGTSWrapperToGTSDecoder(wrapper);
  
  long[] cells = new long[(int) wrapper.getCount()];
  
  int idx = 0;
  
  while(idx < cells.length && decoder.next()) {
    // We are only interested in the timestamp which is the cell
    long cell = decoder.getTimestamp();
    // Only add cells with valid resolution (1-15)
    if (0L != (cell & 0xf000000000000000L)) {
      cells[idx++] = cell;
    }
  }
  
  // Adjust the size if there were some invalid cells (timestamps).
  // This can happen when calling GEOUNPACK from a WRAPped GTS or ENCODER
  if (idx != cells.length) {
    cells = Arrays.copyOf(cells, idx);
  }
  
  GeoXPShape shape = GeoXPLib.fromCells(cells, false);
  
  stack.push(shape);
  
  return stack;
}
 
Example 18
Source File: HyperLogLogPlus.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
public static HyperLogLogPlus fromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
  
  TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());
  
  HyperLogLogPlusParameters params = new HyperLogLogPlusParameters();
  
  try {
    deserializer.deserialize(params, bytes);
  } catch (TException te) {
    throw new IOException(te);
  }
  
  HyperLogLogPlus hllp = new HyperLogLogPlus();
  
  hllp.setInitTime(params.getInitTime());
  
  if (params.isSetKey()) {
    hllp.setKey(params.getKey());
  }
  
  // Read p
  hllp.p = params.getP();
  hllp.m = 1 << hllp.p;
  // Read p'
  hllp.pprime= params.getPprime();
  hllp.mprime = 1 << hllp.pprime;
  
  hllp._64minusp = 64 - hllp.p;
  hllp.pmask = (1L << hllp._64minusp) - 1;
  hllp._64minuspprime = 64 - hllp.pprime;
  hllp.ptopprimemask = ((1L << hllp._64minusp) - 1) ^ ((1L << hllp._64minuspprime) - 1);
  hllp.pprimemask = ((1L << hllp._64minuspprime) - 1);

  // Read the current mode
  hllp.format = params.isSparse() ? Format.SPARSE : Format.NORMAL;
  
  if (Format.SPARSE == hllp.format) {
    hllp.sparse_list_len = params.getSparseListLen();
    hllp.sparse_list = params.getSparseList();
    // Allocate tmp_set
    hllp.tmp_set = new int[(int) Math.ceil((hllp.m * 6) / 8)];
    hllp.tmp_set_idx = 0;
  } else {
    // Read the registers
    if (params.isGzipped()) {
      ByteArrayInputStream bais = new ByteArrayInputStream(params.getRegisters());
      GZIPInputStream gzis = new GZIPInputStream(bais);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte[] buf = new byte[1024];
      while(true) {
        int len = gzis.read(buf);
        
        if (len < 0) {
          break;
        }
        baos.write(buf, 0, len);
      }
      gzis.close();
      hllp.M = baos.toByteArray();
    } else {
      hllp.M = params.getRegisters();
    }
  }
  
  return hllp;
}
 
Example 19
Source File: ThriftParquetFileReaderWriterFactoryTest.java    From secor with Apache License 2.0 4 votes vote down vote up
@Test
public void testThriftParquetReadWriteRoundTrip() throws Exception {
    Map<String, String> classPerTopic = new HashMap<String, String>();
    classPerTopic.put("test-pb-topic", UnitTestMessage.class.getName());
    Mockito.when(config.getThriftMessageClassPerTopic()).thenReturn(classPerTopic);
    Mockito.when(config.getFileReaderWriterFactory())
            .thenReturn(ThriftParquetFileReaderWriterFactory.class.getName());
    Mockito.when(config.getThriftProtocolClass())
    .thenReturn(TCompactProtocol.class.getName());
    Mockito.when(ParquetUtil.getParquetBlockSize(config))
            .thenReturn(ParquetWriter.DEFAULT_BLOCK_SIZE);
    Mockito.when(ParquetUtil.getParquetPageSize(config))
            .thenReturn(ParquetWriter.DEFAULT_PAGE_SIZE);
    Mockito.when(ParquetUtil.getParquetEnableDictionary(config))
            .thenReturn(ParquetWriter.DEFAULT_IS_DICTIONARY_ENABLED);
    Mockito.when(ParquetUtil.getParquetValidation(config))
            .thenReturn(ParquetWriter.DEFAULT_IS_VALIDATING_ENABLED);


    LogFilePath tempLogFilePath = new LogFilePath(Files.createTempDir().toString(), "test-pb-topic",
            new String[] { "part-1" }, 0, 1, 23232, ".log");

    FileWriter fileWriter = ReflectionUtil.createFileWriter(config.getFileReaderWriterFactory(), tempLogFilePath,
            null, config);

    UnitTestMessage msg1 = new UnitTestMessage().setRequiredField("abc").setTimestamp(1467176315L);
    UnitTestMessage msg2 = new UnitTestMessage().setRequiredField("XYZ").setTimestamp(1467176344L);

    TSerializer serializer = new TSerializer(new TCompactProtocol.Factory());
    KeyValue kv1 = new KeyValue(23232, serializer.serialize(msg1));
    KeyValue kv2 = new KeyValue(23233, serializer.serialize(msg2));
    fileWriter.write(kv1);
    fileWriter.write(kv2);
    fileWriter.close();

    FileReader fileReader = ReflectionUtil.createFileReader(config.getFileReaderWriterFactory(), tempLogFilePath,
            null, config);
    TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());
    
    KeyValue kvout = fileReader.next();
    assertEquals(kv1.getOffset(), kvout.getOffset());
    assertArrayEquals(kv1.getValue(), kvout.getValue());
    UnitTestMessage actual = new UnitTestMessage();
    deserializer.deserialize(actual, kvout.getValue());
    assertEquals(msg1.getRequiredField(), actual.getRequiredField());

    kvout = fileReader.next();
    assertEquals(kv2.getOffset(), kvout.getOffset());
    assertArrayEquals(kv2.getValue(), kvout.getValue());
    actual = new UnitTestMessage();
    deserializer.deserialize(actual, kvout.getValue());
    assertEquals(msg2.getRequiredField(), actual.getRequiredField());
}
 
Example 20
Source File: RandomLogGenerator.java    From bigqueue with Apache License 2.0 4 votes vote down vote up
public LogEvent getLogEventFromBytes(byte[] data) throws TException {
	LogEvent logEvent = new LogEvent();
	TDeserializer localDeserializer = tDeserializer.get();
	localDeserializer.deserialize(logEvent, data);
	return logEvent;
}