Skip to content

Instantly share code, notes, and snippets.

@iht
Last active January 27, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iht/64f3fba4aec1002e7ac040da86dd5105 to your computer and use it in GitHub Desktop.
Save iht/64f3fba4aec1002e7ac040da86dd5105 to your computer and use it in GitHub Desktop.
Sample serialization function for the Kafka2Avro pipelines
/** Returns a string representation of an object.
*
* Overwrite this function with your serialization code. In this example, we
* serialize the object and transform that into a base64 encoded string,
* which will be later on written to Kafka.
*
* @tparam T The type of the object ot be serialized. The type must be Serializable
* @param obj The object to be serialized
*/
def object2String[T](obj: T): String = {
val bos = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(bos)
oos.writeObject(obj)
oos.close()
val s: String = Base64.getEncoder.encodeToString(bos.toByteArray)
s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment