saveTocassandra找不到参数rwf的隐含值

时间:2015-04-21 09:02:08

标签: scala cassandra apache-spark

我尝试使用spark scala在Cassandra数据库中保存数据集但是我在运行代码时遇到异常: 使用的链接:http://rustyrazorblade.com/2015/01/introduction-to-spark-cassandra/

error:

could not find implicit value for parameter rwf: com.datastax.spark.connector.writer.RowWriterFctory[FoodToUserIndex]
 food_index.saveToCassandra("tutorial", "food_to_user_index")
                          ^

.scala

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

val conf = new SparkConf(true)
  .set("spark.cassandra.connection.host", "localhost")
  .set("spark.executor.memory", "1g")
  .set("spark.cassandra.connection.native.port", "9042")
val sc = new SparkContext(conf)


case class FoodToUserIndex(food: String, user: String)

val user_table = sc.cassandraTable[CassandraRow]("tutorial",   "user").select("favorite_food","name")

val food_index = user_table.map(r => new   FoodToUserIndex(r.getString("favorite_food"), r.getString("name")))
food_index.saveToCassandra("tutorial", "food_to_user_index")}

build.sbt

name := "intro_to_spark"

version := "1.0"

scalaVersion := "2.11.2"

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.2.0"

libraryDependencies += "com.datastax.spark" %% "spark-cassandra-connector" %  "1.2.0-rc3"

如果将scala和cassandra连接器的版本更改为2.10,1.1.0它的工作。但我需要使用scala 2.11:

scalaVersion := "2.10.4"

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.2.0"

libraryDependencies += "com.datastax.spark" %% "spark-cassandra-connector" %   "1.1.0" withSources() withJavadoc()

2 个答案:

答案 0 :(得分:15)

case class FoodToUserIndex(food: String, user: String)移到函数之外可以解决问题。

答案 1 :(得分:1)

它与“datastax spark-cassandra-connector”版本有关,而与Scala版本无关。

到目前为止,版本1.2.x缺少自定义类的保存。

使用Scala 2.11尝试“datastax spark-cassandra-connector”1.1.1版,它应该可以工作

注意:确保Spark也针对Scala 2.11编译。