public static void doubleToBytes(double v, byte[] bytes) { doubleToBytes(v, bytes, 0); }
public static void intToBytes(int v, byte[] bytes) { intToBytes(v, bytes, 0); }
public static void longToBytes(long v, byte[] bytes) { longToBytes(v, bytes, 0); }
@Test public void testJavaSerialization() { PartitionedEvent partitionedEvent = new PartitionedEvent(); partitionedEvent.setPartitionKey(partitionedEvent.hashCode()); partitionedEvent.setPartition(createSampleStreamGroupbyPartition("sampleStream", Arrays.asList("name", "host"))); StreamEvent event = new StreamEvent(); event.setStreamId("sampleStream"); event.setTimestamp(System.currentTimeMillis()); event.setData(new Object[] {"CPU", "LOCALHOST", true, Long.MAX_VALUE, 60.0}); partitionedEvent.setEvent(event); int javaSerializationLength = SerializationUtils.serialize(partitionedEvent).length; LOG.info("Java serialization length: {}, event: {}", javaSerializationLength, partitionedEvent); int compactLength = 0; compactLength += "sampleStream".getBytes().length; compactLength += ByteUtils.intToBytes(partitionedEvent.getPartition().hashCode()).length; compactLength += ByteUtils.longToBytes(partitionedEvent.getTimestamp()).length; compactLength += "CPU".getBytes().length; compactLength += "LOCALHOST".getBytes().length; compactLength += 1; compactLength += ByteUtils.longToBytes(Long.MAX_VALUE).length; compactLength += ByteUtils.doubleToBytes(60.0).length; LOG.info("Compact serialization length: {}, event: {}", compactLength, partitionedEvent); Assert.assertTrue(compactLength * 20 < javaSerializationLength); }
public static long bytesToLong(byte[] bytes) { return bytesToLong(bytes, 0); }
public static int bytesToInt(byte[] bytes) { return bytesToInt(bytes, 0); }
public static short bytesToShort(byte[] bytes) { return bytesToShort(bytes, 0); }
public static void shortToBytes(short v, byte[] bytes) { shortToBytes(v, bytes, 0); }
public static double bytesToDouble(byte[] bytes) { return Double.longBitsToDouble(bytesToLong(bytes)); }
public static void doubleToBytes(double v, byte[] bytes, int offset) { longToBytes(Double.doubleToLongBits(v), bytes, offset); }
private void onMetricEvent(MetricEvent event) { try { if (additionalFields != null) { event.putAll(additionalFields); } // TODO: Support configurable partition key byte[] key = ByteUtils.intToBytes(event.hashCode()); ProducerRecord<byte[], String> record = new ProducerRecord<>(topic, key, OBJECT_MAPPER.writeValueAsString(event)); // TODO: Support configuration timeout this.producer.send(record).get(5, TimeUnit.SECONDS); } catch (JsonProcessingException e) { LOG.error("Failed to serialize {} as json", event, e); } catch (InterruptedException | ExecutionException | TimeoutException e) { LOG.error("Failed to produce message to topic {}", topic, e); } }
public static double bytesToDouble(byte[] bytes, int offset) { return Double.longBitsToDouble(bytesToLong(bytes, offset)); }
public static byte[] doubleToBytes(double v) { return longToBytes(Double.doubleToLongBits(v)); }