|
17 | 17 |
|
18 | 18 | package org.apache.spark.mllib.clustering |
19 | 19 |
|
| 20 | +import org.json4s._ |
| 21 | +import org.json4s.JsonDSL._ |
| 22 | +import org.json4s.jackson.JsonMethods._ |
| 23 | + |
| 24 | +import org.apache.spark.mllib.linalg._ |
| 25 | +import org.apache.spark.mllib.util.{Loader, Saveable} |
| 26 | +import org.apache.spark.mllib.util.Loader._ |
| 27 | +import org.apache.spark.sql.SQLContext |
| 28 | +import org.apache.spark.SparkContext |
20 | 29 | import org.apache.spark.api.java.JavaRDD |
21 | 30 | import org.apache.spark.rdd.RDD |
22 | | -import org.apache.spark.SparkContext._ |
23 | | -import org.apache.spark.mllib.linalg.Vector |
24 | 31 |
|
25 | 32 | /** |
26 | 33 | * A clustering model for K-means. Each point belongs to the cluster with the closest center. |
27 | 34 | */ |
28 | | -class KMeansModel (val clusterCenters: Array[Vector]) extends Serializable { |
| 35 | +class KMeansModel (val clusterCenters: Array[Vector]) extends Saveable with Serializable { |
29 | 36 |
|
30 | 37 | /** Total number of clusters. */ |
31 | 38 | def k: Int = clusterCenters.length |
@@ -58,4 +65,53 @@ class KMeansModel (val clusterCenters: Array[Vector]) extends Serializable { |
58 | 65 |
|
59 | 66 | private def clusterCentersWithNorm: Iterable[VectorWithNorm] = |
60 | 67 | clusterCenters.map(new VectorWithNorm(_)) |
| 68 | + |
| 69 | + override def save(sc: SparkContext, path: String): Unit = { |
| 70 | + KMeansModel.SaveLoadV1_0.save(sc, this, path) |
| 71 | + } |
| 72 | + |
| 73 | + override protected def formatVersion: String = "1.0" |
| 74 | +} |
| 75 | + |
| 76 | +object KMeansModel extends Loader[KMeansModel] { |
| 77 | + override def load(sc: SparkContext, path: String): KMeansModel = { |
| 78 | + KMeansModel.SaveLoadV1_0.load(sc, path) |
| 79 | + } |
| 80 | + |
| 81 | + private[clustering] |
| 82 | + object SaveLoadV1_0 { |
| 83 | + |
| 84 | + private val thisFormatVersion = "1.0" |
| 85 | + |
| 86 | + private[clustering] |
| 87 | + val thisClassName = "org.apache.spark.mllib.clustering.KMeansModel" |
| 88 | + |
| 89 | + /** |
| 90 | + * Saves a [[KMeansModel]], where user features are saved under `data/users` and |
| 91 | + * product features are saved under `data/products`. |
| 92 | + */ |
| 93 | + def save(sc: SparkContext, model: KMeansModel, path: String): Unit = { |
| 94 | + val sqlContext = new SQLContext(sc) |
| 95 | + val wrapper = new VectorUDT() |
| 96 | + val metadata = compact(render( |
| 97 | + ("class" -> thisClassName) ~ ("version" -> thisFormatVersion) ~ ("k" -> model.k))) |
| 98 | + sc.parallelize(Seq(metadata), 1).saveAsTextFile(Loader.metadataPath(path)) |
| 99 | + val dataRDD = sc.parallelize(model.clusterCenters).map(wrapper.serialize) |
| 100 | + sqlContext.createDataFrame(dataRDD, wrapper.sqlType).saveAsParquetFile(Loader.dataPath(path)) |
| 101 | + } |
| 102 | + |
| 103 | + def load(sc: SparkContext, path: String): KMeansModel = { |
| 104 | + implicit val formats = DefaultFormats |
| 105 | + val sqlContext = new SQLContext(sc) |
| 106 | + val wrapper = new VectorUDT() |
| 107 | + val (className, formatVersion, metadata) = loadMetadata(sc, path) |
| 108 | + assert(className == thisClassName) |
| 109 | + assert(formatVersion == thisFormatVersion) |
| 110 | + val k = (metadata \ "k").extract[Int] |
| 111 | + val centriods = sqlContext.parquetFile(dataPath(path)) |
| 112 | + val localCentriods = centriods.collect() |
| 113 | + assert(k == localCentriods.size) |
| 114 | + new KMeansModel(localCentriods.map(wrapper.deserialize)) |
| 115 | + } |
| 116 | + } |
61 | 117 | } |
0 commit comments