关于访问 Tuple2 中的字段时出错 [英] About an error accessing a field inside Tuple2

查看:24
本文介绍了关于访问 Tuple2 中的字段时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问 Tuple2 中的一个字段,编译器返回一个错误.该软件尝试在 kafka 主题中推送案例类,然后我想使用火花流恢复它,以便我可以提供机器学习算法并将结果保存在 mongo 实例中.

i am trying to access to a field within a Tuple2 and compiler is returning me an error. The software tries to push a case class within a kafka topic, then i want to recover it using spark streaming so i can feed a machine learning algorithm and save results within a mongo instance.

解决了!

我终于解决了我的问题,我将发布最终解决方案:

I finally solved my problem, i am going to post the final solution:

这是github项目:

This is the github project:

https://github.com/alonsoir/awesome-recommendation-engine/tree/develop

build.sbt

name := "my-recommendation-spark-engine"

version := "1.0-SNAPSHOT"

scalaVersion := "2.10.4"

val sparkVersion = "1.6.1"

val akkaVersion = "2.3.11" // override Akka to be this version to match the one in Spark

libraryDependencies ++= Seq(
"org.apache.kafka" % "kafka_2.10" % "0.8.1"
  exclude("javax.jms", "jms")
  exclude("com.sun.jdmk", "jmxtools")
  exclude("com.sun.jmx", "jmxri"),
 //not working play module!! check
 //jdbc,
 //anorm,
 //cache,
 // HTTP client
 "net.databinder.dispatch" %% "dispatch-core" % "0.11.1",
 // HTML parser
 "org.jodd" % "jodd-lagarto" % "3.5.2",
 "com.typesafe" % "config" % "1.2.1",
 "com.typesafe.play" % "play-json_2.10" % "2.4.0-M2",
 "org.scalatest" % "scalatest_2.10" % "2.2.1" % "test",
 "org.twitter4j" % "twitter4j-core" % "4.0.2",
 "org.twitter4j" % "twitter4j-stream" % "4.0.2",
 "org.codehaus.jackson" % "jackson-core-asl" % "1.6.1",
 "org.scala-tools.testing" % "specs_2.8.0" % "1.6.5" % "test",
 "org.apache.spark" % "spark-streaming-kafka_2.10" % "1.6.1" ,
 "org.apache.spark" % "spark-core_2.10" % "1.6.1" ,
 "org.apache.spark" % "spark-streaming_2.10" % "1.6.1",
 "org.apache.spark" % "spark-sql_2.10" % "1.6.1",
 "org.apache.spark" % "spark-mllib_2.10" % "1.6.1",
 "com.google.code.gson" % "gson" % "2.6.2",
 "commons-cli" % "commons-cli" % "1.3.1",
 "com.stratio.datasource" % "spark-mongodb_2.10" % "0.11.1",
 // Akka
 "com.typesafe.akka" %% "akka-actor" % akkaVersion,
 "com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
 // MongoDB
 "org.reactivemongo" %% "reactivemongo" % "0.10.0"
 )

 packAutoSettings

 //play.Project.playScalaSettings

卡夫卡生产者

package example.producer

import play.api.libs.json._
import example.utils._
import scala.concurrent.Future
import example.model.{AmazonProductAndRating,AmazonProduct,AmazonRating}
import example.utils.AmazonPageParser
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future


/**
args(0) : productId
args(1) : userdId

Usage: ./amazon-producer-example 0981531679 someUserId 3.0
*/
object AmazonProducerExample {

def main(args: Array[String]): Unit = {

val productId = args(0).toString
val userId = args(1).toString
val rating = args(2).toDouble
val topicName = "amazonRatingsTopic"

val producer = Producer[String](topicName)

//0981531679 is Scala Puzzlers...
AmazonPageParser.parse(productId,userId,rating).onSuccess { case amazonRating =>
  //Is this the correct way? the best performance? possibly not, what about using avro or parquet? How can i push data in avro or parquet format?
  //You can see that i am pushing json String to kafka topic, not raw String, but is there any difference? 
  //of course there are differences...
  producer.send(Json.toJson(amazonRating).toString)
  //producer.send(amazonRating.toString)
  println("amazon product with rating sent to kafka cluster..." + amazonRating.toString)
  System.exit(0)
}

}
}

这是必要案例类的定义(更新),文件名为models.scala:

This is the definition of necessary case classes (UPDATED), the file is named models.scala:

package example.model

import play.api.libs.json.Json
import reactivemongo.bson.Macros

case class AmazonProduct(itemId: String, title: String, url: String, img: String, description: String)
case class AmazonRating(userId: String, productId: String, rating: Double)

case class AmazonProductAndRating(product: AmazonProduct, rating: AmazonRating)

// For MongoDB
object AmazonRating {
implicit val amazonRatingHandler = Macros.handler[AmazonRating]
implicit val amazonRatingFormat = Json.format[AmazonRating]
//added using @Yuval tip
lazy val empty: AmazonRating = AmazonRating("-1", "-1", -1d)
}

这是spark流处理的完整代码:

This is the full code of the spark streaming process:

package example.spark

import java.io.File
import java.util.Date

import play.api.libs.json._
import com.google.gson.{Gson,GsonBuilder, JsonParser}
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.functions._

import com.mongodb.casbah.Imports._
import com.mongodb.QueryBuilder
import com.mongodb.casbah.MongoClient
import com.mongodb.casbah.commons.{MongoDBList, MongoDBObject}

import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.bson.BSONDocument

import org.apache.spark.streaming.kafka._
import kafka.serializer.StringDecoder
import example.model._

import example.utils.Recommender

/**
* Collect at least the specified number of json amazon products in order to feed recomedation system and feed mongo instance with results.

Usage: ./amazon-kafka-connector 127.0.0.1:9092 amazonRatingsTopic

on mongo shell:

use alonsodb;
db.amazonRatings.find();
*/
object AmazonKafkaConnector {

private var numAmazonProductCollected = 0L
private var partNum = 0
private val numAmazonProductToCollect = 10000000

//this settings must be in reference.conf
private val Database = "alonsodb"
private val ratingCollection = "amazonRatings"
private val MongoHost = "127.0.0.1"
private val MongoPort = 27017
private val MongoProvider = "com.stratio.datasource.mongodb"

private val jsonParser = new JsonParser()
private val gson = new GsonBuilder().setPrettyPrinting().create()

private def prepareMongoEnvironment(): MongoClient = {
  val mongoClient = MongoClient(MongoHost, MongoPort)
  mongoClient
}

private def closeMongoEnviroment(mongoClient : MongoClient) = {
  mongoClient.close()
  println("mongoclient closed!")
}

private def cleanMongoEnvironment(mongoClient: MongoClient) = {
  cleanMongoData(mongoClient)
  mongoClient.close()
}

private def cleanMongoData(client: MongoClient): Unit = {
  val collection = client(Database)(ratingCollection)
  collection.dropCollection()
}

def main(args: Array[String]) {
// Process program arguments and set properties

if (args.length < 2) {
  System.err.println("Usage: " + this.getClass.getSimpleName + " <brokers> <topics>")
  System.exit(1)
}

val Array(brokers, topics) = args

println("Initializing Streaming Spark Context and kafka connector...")
// Create context with 2 second batch interval
val sparkConf = new SparkConf().setAppName("AmazonKafkaConnector")
                               .setMaster("local[4]")
                                .set("spark.driver.allowMultipleContexts", "true")

val sc = new SparkContext(sparkConf)
val sqlContext = new SQLContext(sc)
sc.addJar("target/scala-2.10/blog-spark-recommendation_2.10-1.0-SNAPSHOT.jar")
val ssc = new StreamingContext(sparkConf, Seconds(2))
//this checkpointdir should be in a conf file, for now it is hardcoded!
val streamingCheckpointDir = "/Users/aironman/my-recommendation-spark-engine/checkpoint"
ssc.checkpoint(streamingCheckpointDir)

// Create direct kafka stream with brokers and topics
val topicsSet = topics.split(",").toSet
val kafkaParams = Map[String, String]("metadata.broker.list" -> brokers)
val messages = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, topicsSet)
println("Initialized Streaming Spark Context and kafka connector...")

//create recomendation module
println("Creating rating recommender module...")
val ratingFile= "ratings.csv"
val recommender = new Recommender(sc,ratingFile)
println("Initialized rating recommender module...")
//THIS IS THE MOST INTERESTING PART AND WHAT I NEED!
//THE SOLUTION IS NOT PROBABLY THE MOST EFFICIENT, BECAUSE I HAD TO 
//USE DATAFRAMES, ARRAYs and SEQs BUT IS FUNCTIONAL!
try{
messages.foreachRDD(rdd => {
 val count = rdd.count()
 if (count > 0){
   val json= rdd.map(_._2)
   val dataFrame = sqlContext.read.json(json) //converts json to DF
   val myRow = dataFrame.select(dataFrame("userId"),dataFrame("productId"),dataFrame("rating")).take(count.toInt)
   println("myRow is: " + myRow)

   val myAmazonRating = AmazonRating(myRow(0).getString(0), myRow(0).getString(1), myRow(0).getDouble(2))
   println("myAmazonRating is: " + myAmazonRating.toString)
   val arrayAmazonRating = Array(myAmazonRating)
   //this method needs Seq[AmazonRating]
   recommender.predictWithALS(arrayAmazonRating.toSeq)
   }//if
})      
}catch{
  case e: IllegalArgumentException => {println("illegal arg. exception")};
  case e: IllegalStateException    => {println("illegal state exception")};
  case e: ClassCastException       => {println("ClassCastException")};
  case e: Exception                => {println(" Generic Exception")};
}finally{

  println("Finished taking data from kafka topic...")
}

ssc.start()
ssc.awaitTermination()

println("Finished!")
}
}

谢谢大家,@Yuval、@Emecas 和 @Riccardo.cardin.

Thank you all, folks, @Yuval, @Emecas and @Riccardo.cardin.

Recommender.predict 签名方法如下:

Recommender.predict signature method looks like:

  def predict(ratings: Seq[AmazonRating]) = {
  // train model
  val myRatings = ratings.map(toSparkRating)
  val myRatingRDD = sc.parallelize(myRatings)

  val startAls = DateTime.now
  val model = ALS.train((sparkRatings ++ myRatingRDD).repartition(NumPartitions), 10, 20, 0.01)

  val myProducts = myRatings.map(_.product).toSet
  val candidates = sc.parallelize((0 until productDict.size).filterNot(myProducts.contains))

  // get ratings of all products not in my history ordered by rating (higher first) and only keep the first NumRecommendations
   val myUserId = userDict.getIndex(MyUsername)
   val recommendations = model.predict(candidates.map((myUserId, _))).collect
   val endAls = DateTime.now
   val result = recommendations.sortBy(-_.rating).take(NumRecommendations).map(toAmazonRating)
   val alsTime = Seconds.secondsBetween(startAls, endAls).getSeconds

   println(s"ALS Time: $alsTime seconds")
   result
   }

//我想我已经尽可能清楚了,如果您还需要什么,请告诉我,感谢您耐心教我@Yuval

//I think I've been as clear as possible, tell me if you need anything more and thanks for your patience teaching me @Yuval

推荐答案

诊断

IllegalStateException 表明您正在操作一个已经处于活动或停止状态的 StreamingContext.见此处的详细信息(第 218-231 行)

IllegalStateException suggests that you are operating over a StreamingContext that is already ACTIVE or STOPPED. see details here (lines 218-231)

java.lang.IllegalStateException: Adding new inputs, transformations, and output operations after starting a context is not supported

代码审查

通过观察您的代码 AmazonKafkaConnector ,您正在将 mapfilterforeachRDD 转换为另一个 foreachRDD 在同一个 DirectStream 对象上调用:messages

By observing your code AmazonKafkaConnector , you are doing map, filter and foreachRDD into another foreachRDD over the same DirectStream object called : messages

一般建议:

成为实用的我的朋友,将您的逻辑分解为您想要执行的每一项任务的小块:

Be functional my friend, by dividing your logic in small pieces for each one of the tasks you want to perform:

  • 流媒体
  • 机器学习推荐
  • 坚持

这将帮助您更轻松地理解和调试您想要实现的 Spark 管道.

That will help you to understand and debug easier the Spark pipeline that you want to implement.

这篇关于关于访问 Tuple2 中的字段时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆