Skip to content

Instantly share code, notes, and snippets.

@iht
Last active January 27, 2020 21:52
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/4e1f03e505115c378145596ef9e47a1a to your computer and use it in GitHub Desktop.
Save iht/4e1f03e505115c378145596ef9e47a1a to your computer and use it in GitHub Desktop.
Sample deserialization function for the Kafka2Avro pipelines
/** Returns an object of type T from a base64 encoded string.
*
* Overwrite this function for your deserialization code. In this example, we
* assume that Kafka contains plain old Java objects, encoded as base64
* strings.
*
* @tparam T The type of the object to be recovered
* @param s String with a base64 encoded serialized object
*/
def string2Object[T](s: String): T = {
val data: Array[Byte] = Base64.getDecoder.decode(s)
val ois: ObjectInputStream = new ObjectInputStream(new ByteArrayInputStream(data))
val result: T = ois.readObject.asInstanceOf[T]
ois.close()
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment