我正在尝试将Spark MLLib中的协作算法与H2o Ai集成,使用苏打水进行产品推荐。我按照这个链接
http://spark.apache.org/docs/latest/mllib-collaborative-filtering.html
并更新了代码,如下所示
System.setProperty("hadoop.home.dir", "D:\\backup\\lib\\winutils")
val conf = new SparkConf()
.setAppName("Spark-InputFile processor")
.setMaster("local")
val sc = new SparkContext(conf)
val inputFile = "src/main/resources/test.data"
val data = sc.textFile(inputFile)
val ratings = data.map(x=>{
val mapper = x.split(",")
Rating(mapper(0).toInt,mapper(1).toInt,mapper(2).toDouble)
})
// Build the recommendation model using ALS
val rank = 10
val numIterations = 10
val model = ALS.train(ratings, rank, numIterations, 0.01)
// Save and load model
model.save(sc, "target/tmp/myCollaborativeFilter")
val sameModel = MatrixFactorizationModel.load(sc, "target/tmp/myCollaborativeFilter")
val modelRdd = sameModel.recommendProductsForUsers(100)
implicit val sqlContext = SparkSession.builder().getOrCreate().sqlContext
import sqlContext.implicits._
val modelDf = modelRdd.toDF("Rdd","Rdd1")
@transient val hc = H2OContext.getOrCreate(sc)
val h2oframe:H2OFrame = hc.asH2OFrame(modelDf)
当我在Intellij中运行代码时,我收到以下错误
Exception in thread "main" java.util.NoSuchElementException: key not found: StructType(StructField(user,IntegerType,false), StructField(product,IntegerType,false), StructField(rating,DoubleType,false))
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:59)
at org.apache.spark.h2o.utils.ReflectionUtils$.vecTypeFor(ReflectionUtils.scala:132)
at org.apache.spark.h2o.converters.SparkDataFrameConverter$$anonfun$3.apply(SparkDataFrameConverter.scala:68)
at org.apache.spark.h2o.converters.SparkDataFrameConverter$$anonfun$3.apply(SparkDataFrameConverter.scala:68)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
at scala.collection.AbstractTraversable.map(Traversable.scala:104)
at org.apache.spark.h2o.converters.SparkDataFrameConverter$.toH2OFrame(SparkDataFrameConverter.scala:68)
at org.apache.spark.h2o.H2OContext.asH2OFrame(H2OContext.scala:132)
at org.apache.spark.h2o.H2OContext.asH2OFrame(H2OContext.scala:130)
at com.poc.sample.RecommendataionAlgo$.main(RecommendataionAlgo.scala:54)
at com.poc.sample.RecommendataionAlgo.main(RecommendataionAlgo.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
如何解决此错误?
提前致谢。
答案 0 :(得分:1)
modelRdd
将是Tuple2<Object, Rating>
类型(或Scala中的等效类型),Rating
不是类型我们(Sparkling Water)提供自动转换(it&#39; s不是String, Double, Float etc.
也不是Product
)。我们肯定需要在那里抛出更有意义的错误信息。
要解决此问题而不是使用Object, Rating
与modelRdd.toDF("Rdd","Rdd1")
制作数据框,您可以将其映射到包含4列Object, user, product, rating
的DF,然后使用hc.asH2OFrame()
。