Skip to content
Snippets Groups Projects
Commit 11a93fb5 authored by Hossein Falaki's avatar Hossein Falaki
Browse files

Added serializing method for Rating object

parent 8d0c2f73
No related branches found
No related tags found
No related merge requests found
...@@ -197,6 +197,7 @@ class PythonMLLibAPI extends Serializable { ...@@ -197,6 +197,7 @@ class PythonMLLibAPI extends Serializable {
return ret return ret
} }
/** Unpack a Rating object from an array of bytes */
private def unpackRating(ratingBytes: Array[Byte]): Rating = { private def unpackRating(ratingBytes: Array[Byte]): Rating = {
val bb = ByteBuffer.wrap(ratingBytes) val bb = ByteBuffer.wrap(ratingBytes)
bb.order(ByteOrder.nativeOrder()) bb.order(ByteOrder.nativeOrder())
...@@ -206,6 +207,7 @@ class PythonMLLibAPI extends Serializable { ...@@ -206,6 +207,7 @@ class PythonMLLibAPI extends Serializable {
return new Rating(user, product, rating) return new Rating(user, product, rating)
} }
/** Unpack a tuple of Ints from an array of bytes */
private[spark] def unpackTuple(tupleBytes: Array[Byte]): (Int, Int) = { private[spark] def unpackTuple(tupleBytes: Array[Byte]): (Int, Int) = {
val bb = ByteBuffer.wrap(tupleBytes) val bb = ByteBuffer.wrap(tupleBytes)
bb.order(ByteOrder.nativeOrder()) bb.order(ByteOrder.nativeOrder())
...@@ -214,13 +216,23 @@ class PythonMLLibAPI extends Serializable { ...@@ -214,13 +216,23 @@ class PythonMLLibAPI extends Serializable {
(v1, v2) (v1, v2)
} }
/**
* Serialize a Rating object into an array of bytes.
* It can be deserialized using RatingDeserializer().
*
* @param rate
* @return
*/
private[spark] def serializeRating(rate: Rating): Array[Byte] = { private[spark] def serializeRating(rate: Rating): Array[Byte] = {
val bytes = new Array[Byte](24) val len = 3
val bytes = new Array[Byte](4 + 8 * len)
val bb = ByteBuffer.wrap(bytes) val bb = ByteBuffer.wrap(bytes)
bb.order(ByteOrder.nativeOrder()) bb.order(ByteOrder.nativeOrder())
bb.putDouble(rate.user.toDouble) bb.putInt(len)
bb.putDouble(rate.product.toDouble) val db = bb.asDoubleBuffer()
bb.putDouble(rate.rating) db.put(rate.user.toDouble)
db.put(rate.product.toDouble)
db.put(rate.rating)
bytes bytes
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment