/** * Serializes given object to byte array using Kryo instance in pool. * * @param obj Object to serialize * @param bufferSize maximum size of serialized bytes * @return serialized bytes */ public byte[] serialize(final Object obj, final int bufferSize) { ByteBufferOutput out = new ByteBufferOutput(bufferSize, MAX_BUFFER_SIZE); try { Kryo kryo = borrow(); try { kryo.writeClassAndObject(out, obj); out.flush(); return out.toBytes(); } finally { release(kryo); } } finally { out.release(); } }
@Test public void testSerialization() throws Exception { User user = new User(1, "admin", new Date()); Kryo kryo = new Kryo(); ByteBufferOutput output = new ByteBufferOutput(10240); kryo.writeObject(output, user); byte[] bytes = output.toBytes(); System.out.println("Saved bytes: " + Bytes.toString(bytes)); ssdbClient.set("kryo_user", bytes); ////////////////////////////////////////////////////////////// byte[] readBytes = ssdbClient.getBytes("kryo_user"); System.out.println("Read bytes: " + Bytes.toString(readBytes)); Assert.assertEquals(bytes.length, readBytes.length); }
@Override public byte[] serialize(String topic, Measurement record) { int byteBufferLength = 50; if (record instanceof HSMMeasurement) { byteBufferLength = 7600 * 2 * 100; // TODO: improve } ByteBufferOutput output = new ByteBufferOutput(byteBufferLength); kryos.get().writeObject(output, record); return output.toBytes(); }
/** * Serializes given object to byte buffer using Kryo instance in pool. * * @param obj Object to serialize * @param buffer to write to */ public void serialize(final Object obj, final ByteBuffer buffer) { ByteBufferOutput out = new ByteBufferOutput(buffer); Kryo kryo = borrow(); try { kryo.writeClassAndObject(out, obj); out.flush(); } finally { release(kryo); } }
/** * Serializes given object to OutputStream using Kryo instance in pool. * * @param obj Object to serialize * @param stream to write to * @param bufferSize size of the buffer in front of the stream */ public void serialize(final Object obj, final OutputStream stream, final int bufferSize) { ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize); Kryo kryo = borrow(); try { kryo.writeClassAndObject(out, obj); out.flush(); } finally { release(kryo); } }
/** * Serializes given object to byte array using Kryo instance in pool. * * @param obj Object to serialize * @param bufferSize maximum size of serialized bytes * @return serialized bytes */ public byte[] serialize(final Object obj, final int bufferSize) { ByteBufferOutput out = new ByteBufferOutput(bufferSize, MAX_BUFFER_SIZE); Kryo kryo = borrow(); try { kryo.writeClassAndObject(out, obj); out.flush(); return out.toBytes(); } finally { release(kryo); } }
public KryoSerialization (Kryo kryo) { this.kryo = kryo; kryo.register(RegisterTCP.class); kryo.register(RegisterUDP.class); kryo.register(KeepAlive.class); kryo.register(DiscoverHost.class); kryo.register(Ping.class); input = new ByteBufferInput(); output = new ByteBufferOutput(); }
@Override public void run() { Kryo kryoInstance = kryoPool.getKryo(); KafkaProducerPool pool = KafkaProducerPool.getInstance(); KafkaProducer producer = pool.getProducer(); try { for (WebTxnData dataItem : dataBatch) { ByteBufferOutput bufferOutput = new ByteBufferOutput(4096); kryoInstance.writeObject(bufferOutput, dataItem); byte[] msgBytes = bufferOutput.toBytes(); bufferOutput.clear(); //serialize item byte[] keyBytes = UUID.randomUUID().toString().getBytes(); KeyedMessage<byte[], byte[]> message = new KeyedMessage<byte[], byte[]>(MsgConstants.WEB_TXN_TOPIC_NAME, keyBytes, msgBytes); // message producer is Async , it will queue and send out the pipe //order doesn't matter producer.sendMsg(message); } dataBatch.clear(); } catch (Exception e) { logger.warn("Exception in sending message"); } finally { pool.returnToPool(producer); kryoPool.returnToPool(kryoInstance); } }
@Override public void run() { Kryo kryoInstance = kryoPool.getKryo(); KafkaProducerPool pool = KafkaProducerPool.getInstance(); KafkaProducer producer = pool.getProducer(); try { for (WebTxnData dataItem : dataBatch) { ByteBufferOutput bufferOutput = new ByteBufferOutput(4096); kryoInstance.writeObject(bufferOutput, dataItem); byte[] msgBytes = bufferOutput.toBytes(); bufferOutput.clear(); //serialize item byte[] keyBytes = String.valueOf(random.nextInt()).getBytes(); KeyedMessage<byte[], byte[]> message = new KeyedMessage<byte[], byte[]>(MsgConstants.WEB_TXN_TOPIC_NAME, keyBytes, msgBytes); // message producer is Async , it will queue and send out the pipe //order doesn't matter producer.sendMsg(message); } dataBatch.clear(); } catch (Exception e) { logger.warn("Exception in sending message"); } finally { pool.returnToPool(producer); kryoPool.returnToPool(kryoInstance); } }
@Override public byte[] toBytes(Object dataItem) { Kryo kryoInstance = pool.getKryo(); ByteBufferOutput bufferOutput = new ByteBufferOutput(4096); kryoInstance.writeObject(bufferOutput,dataItem); byte [] msg = bufferOutput.toBytes(); bufferOutput.clear(); pool.returnToPool(kryoInstance); return msg; }
public KryoSerialization(Kryo kryo) { this.kryo = kryo; kryo.register(RegisterTCP.class); kryo.register(RegisterUDP.class); kryo.register(KeepAlive.class); kryo.register(DiscoverHost.class); kryo.register(Ping.class); input = new ByteBufferInput(); output = new ByteBufferOutput(); }
@Override public byte[] serialize(String s, SensorReading sensorReading) { ByteBufferOutput output = new ByteBufferOutput(100); kryos.get().writeObject(output, sensorReading); return output.toBytes(); }
public static void main(String [] args){ String str = "India,Search,Asia"; System.out.println("string is " + str.getBytes()); Kryo kryo =KryoPool.getInstance().getKryo(); ByteBufferOutput bufferOutput = new ByteBufferOutput(20); kryo.writeObject(bufferOutput, str); byte[] msgBytes = bufferOutput.toBytes(); System.out.println(msgBytes.length); ByteBufferInput input = new ByteBufferInput(msgBytes.length); input.setBuffer(msgBytes); String str2 = kryo.readObject(input,String.class); System.out.println(str2); KryoPool.getInstance().returnToPool(kryo); }
protected Output initialValue() { return new ByteBufferOutput(EXPECTED_BUFFER_SIZE, MAX_BUFFER_SIZE); }
@Override protected ByteBufferOutput initialValue() { return new ByteBufferOutput(10240); }
public static void main(String[] args) { CacheStore.getInstance().init(); HashFunction hf = Hashing.murmur3_128(new Random().nextInt());; CacheStore store = CacheStore.getInstance(); String keyCacheId = "Key-"+GlobalIdGenerator.getInstance().getId(); Cache<Long,GenericGroupByKey> keyCache = store.makeKeyCache(keyCacheId); Kryo kryo = KryoPool.getInstance().getKryo(); int minOfDay =0; Random random = new Random(); long recordCount =0; for(int i =0;i < 10;i++){ minOfDay = random.nextInt(1020202); for(String userAgentStr : UserAgentDataSet.userAgentSet) { Hasher hasher = hf.newHasher().putInt(minOfDay); final String completeStr = userAgentStr + "," + minOfDay; hasher.putString(completeStr, Charset.defaultCharset()); ByteBufferOutput output = new ByteBufferOutput(completeStr.length() + 5); kryo.writeObject(output, completeStr); final long hashKey = hasher.hash().asLong(); GenericGroupByKey groupByKey = new GenericGroupByKey(hashKey, output.toBytes()); keyCache.put(hashKey, groupByKey); output.clear(); recordCount++; } } store.deleteCache(keyCacheId); System.out.println("completed record " + recordCount); store.close(); }