You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by pw...@apache.org on 2014/01/08 01:57:23 UTC
[5/9] git commit: Added serializing method for Rating object
Added serializing method for Rating object
Project: http://git-wip-us.apache.org/repos/asf/incubator-spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-spark/commit/11a93fb5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-spark/tree/11a93fb5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-spark/diff/11a93fb5
Branch: refs/heads/master
Commit: 11a93fb5a8fafa940db27b652e4c21f6713ed8d1
Parents: 8d0c2f7
Author: Hossein Falaki <fa...@gmail.com>
Authored: Mon Jan 6 12:18:03 2014 -0800
Committer: Hossein Falaki <fa...@gmail.com>
Committed: Mon Jan 6 12:18:03 2014 -0800
----------------------------------------------------------------------
.../spark/mllib/api/python/PythonMLLibAPI.scala | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-spark/blob/11a93fb5/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala
----------------------------------------------------------------------
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala b/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala
index be2628f..2d86233 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala
@@ -197,6 +197,7 @@ class PythonMLLibAPI extends Serializable {
return ret
}
+ /** Unpack a Rating object from an array of bytes */
private def unpackRating(ratingBytes: Array[Byte]): Rating = {
val bb = ByteBuffer.wrap(ratingBytes)
bb.order(ByteOrder.nativeOrder())
@@ -206,6 +207,7 @@ class PythonMLLibAPI extends Serializable {
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) = {
val bb = ByteBuffer.wrap(tupleBytes)
bb.order(ByteOrder.nativeOrder())
@@ -214,13 +216,23 @@ class PythonMLLibAPI extends Serializable {
(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] = {
- val bytes = new Array[Byte](24)
+ val len = 3
+ val bytes = new Array[Byte](4 + 8 * len)
val bb = ByteBuffer.wrap(bytes)
bb.order(ByteOrder.nativeOrder())
- bb.putDouble(rate.user.toDouble)
- bb.putDouble(rate.product.toDouble)
- bb.putDouble(rate.rating)
+ bb.putInt(len)
+ val db = bb.asDoubleBuffer()
+ db.put(rate.user.toDouble)
+ db.put(rate.product.toDouble)
+ db.put(rate.rating)
bytes
}