【发布时间】:2016-03-31 09:41:08
【问题描述】:
问题
这可能是由于我缺乏 Scala 知识,但似乎在 for 理解 中添加另一个级别应该可以正常工作。如果第一行 for comprehension 被注释掉,代码就可以工作。我最终想要一个 Set[Int] 而不是“1 到 2”,但它可以说明问题。 for 的前两行不需要类型说明符,但我包含它以表明我已经尝试了显而易见的方法。
工具/罐子
- IntelliJ 2016.1
- Java 8
- Scala 2.10.5
- 卡桑德拉 3.x
- spark-assembly-1.6.0-hadoop2.6.0.jar(预建)
- spark-cassandra-connector_2.10-1.6.0-M1-SNAPSHOT.jar(预建)
- spark-cassandra-connector-assembly-1.6.0-M1-SNAPSHOT.jar(我构建)
代码
case class NotifHist(intnotifhistid:Int, eventhistids:Seq[Int], yosemiteid:String, initiatorname:String)
case class NotifHistSingle(intnotifhistid:Int, inteventhistid:Int, dataCenter:String, initiatorname:String)
object SparkCassandraConnectorJoins {
def joinQueryAfterMakingExpandedRdd(sc:SparkContext, orgNodeId:Int) {
val notifHist:RDD[NotifHistSingle] = for {
orgNodeId:Int <- 1 to 2 // comment out this line and it works
notifHist:NotifHist <- sc.cassandraTable[NotifHist](keyspace, "notifhist").where("intorgnodeid = ?", orgNodeId)
eventHistId <- notifHist.eventhistids
} yield NotifHistSingle(notifHist.intnotifhistid, eventHistId, notifHist.yosemiteid, notifHist.initiatorname)
...etc...
}
编译输出
Information:3/29/16 8:52 AM - Compilation completed with 1 error and 0 warnings in 1s 507ms
/home/jpowell/Projects/SparkCassandraConnector/src/com/mir3/spark/SparkCassandraConnectorJoins.scala
**Error:(88, 21) type mismatch;
found : scala.collection.immutable.IndexedSeq[Nothing]
required: org.apache.spark.rdd.RDD[com.mir3.spark.NotifHistSingle]
orgNodeId:Int <- 1 to 2
^**
稍后
@slouc 感谢您的全面回答。我正在使用 for comprehension 的 语法糖来保持第二条语句的状态以填充 NotifHistSingle ctor 中的元素,所以我看不到如何让等效的地图/平面图工作。因此,我采用了以下解决方案:
def joinQueryAfterMakingExpandedRdd(sc:SparkContext, orgNodeIds:Set[Int]) {
def notifHistForOrg(orgNodeId:Int): RDD[NotifHistSingle] = {
for {
notifHist <- sc.cassandraTable[NotifHist](keyspace, "notifhist").where("intorgnodeid = ?", orgNodeId)
eventHistId <- notifHist.eventhistids
} yield NotifHistSingle(notifHist.intnotifhistid, eventHistId, notifHist.yosemiteid, notifHist.initiatorname)
}
val emptyTable:RDD[NotifHistSingle] = sc.emptyRDD[NotifHistSingle]
val notifHistForAllOrgs:RDD[NotifHistSingle] = orgNodeIds.foldLeft(emptyTable)((accum, oid) => accum ++ notifHistForOrg(oid))
}
【问题讨论】:
标签: scala apache-spark spark-cassandra-connector