我试图将cassandra行映射到使用spark-cassandra-connector
的参数化类型。我一直在尝试使用隐式定义的columnMapper定义映射,因此:
class Foo[T<:Bar:ClassTag:RowReaderFactory] {
implicit object Mapper extends JavaBeanColumnMapper[T](
Map("id" -> "id",
"timestamp" -> "ts"))
def doSomeStuff(operations: CassandraTableScanRDD[T]): Unit = {
println("do some stuff here")
}
}
然而,我遇到了以下错误,我认为这是由于我传入RowReaderFactory
而没有正确指定RowReaderFactory
的映射。知道如何为RowReaderFactory
指定映射信息吗?
Exception in thread "main" java.lang.IllegalArgumentException: Failed to map constructor parameter timestamp in Bar to a column of MyNamespace
at com.datastax.spark.connector.mapper.DefaultColumnMapper$$anonfun$4$$anonfun$apply$1.apply(DefaultColumnMapper.scala:78)
at com.datastax.spark.connector.mapper.DefaultColumnMapper$$anonfun$4$$anonfun$apply$1.apply(DefaultColumnMapper.scala:78)
at scala.Option.getOrElse(Option.scala:120)
at com.datastax.spark.connector.mapper.DefaultColumnMapper$$anonfun$4.apply(DefaultColumnMapper.scala:78)
at com.datastax.spark.connector.mapper.DefaultColumnMapper$$anonfun$4.apply(DefaultColumnMapper.scala:76)
at scala.collection.TraversableLike$WithFilter$$anonfun$map$2.apply(TraversableLike.scala:722)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:721)
at com.datastax.spark.connector.mapper.DefaultColumnMapper.columnMapForReading(DefaultColumnMapper.scala:76)
at com.datastax.spark.connector.rdd.reader.GettableDataToMappedTypeConverter.<init>(GettableDataToMappedTypeConverter.scala:56)
at com.datastax.spark.connector.rdd.reader.ClassBasedRowReader.<init>(ClassBasedRowReader.scala:23)
at com.datastax.spark.connector.rdd.reader.ClassBasedRowReaderFactory.rowReader(ClassBasedRowReader.scala:48)
at com.datastax.spark.connector.rdd.reader.ClassBasedRowReaderFactory.rowReader(ClassBasedRowReader.scala:43)
at com.datastax.spark.connector.rdd.CassandraTableRowReaderProvider$class.rowReader(CassandraTableRowReaderProvider.scala:48)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD.rowReader$lzycompute(CassandraTableScanRDD.scala:59)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD.rowReader(CassandraTableScanRDD.scala:59)
at com.datastax.spark.connector.rdd.CassandraTableRowReaderProvider$class.verify(CassandraTableRowReaderProvider.scala:147)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD.verify(CassandraTableScanRDD.scala:59)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD.getPartitions(CassandraTableScanRDD.scala:143)
可以在Foo的伴侣对象中隐式定义,如下所示:
object Foo {
implicit object Mapper extends JavaBeanColumnMapper[T](
Map("id" -> "id",
"timestamp" -> "ts"))
}
Scala在查找类的隐式实例时将查找类的伴侣对象。如果您愿意,可以在需要隐式的作用域中定义它,但您可能希望添加伴随对象,这样就不需要在必要时重复它。
证明columnMapper
必须在创建Foo
实例的范围内创建,而不是在Foo
本身中创建。