简体   繁体   中英

How to build a trait for generic RDD operations

I am trying to build a trait which would implement generic helping method for a type of RDD I have.

For instance:

abstract class MyClass(name: String) {
   final def getName: String = name
}

trait MyTrait[T <: MyClass] {
     def myMethod(
        input: RDD[T],
        something: String
     ): RDD[T] = {
        input.filter(_.getName != something)
     }

     def otherExample(
         inputA: RDD[(T, String)],
         inputB: RDD[(T, Int)]
     ): RDD[(T, (String, Int)] = {
         inputA
            .keyBy(_._1.getName)
            .join(inputB....)
            ...
     }
}

But I am getting weird errors such as

 value mapValues is not a member of org.apache.spark.rdd.RDD[(T, String)]
[error] possible cause: maybe a semicolon is missing before `value mapValues'?

I tried to add ClassTag in the generic class T but I couldn't find the right syntax as [T <: MyClass : ClassTag] or [T : MyClass : ClassTag] did not work.

Thanks !

So I was able to fix my problem.

I did not realize but the issue was only coming from mapValues .

It was caused by the fact that T was not a ClassTag .

To solve the issue, I added (implicit ct: ClassTag[T]) to the method where I was using mapValues and that solved it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM