title: How to implement a trait with a generic case class that creates a dataset in Scala
body: <p>I want to create a Scala trait that should be implemented with a case class T. The trait is simply to load data and transform it into a Spark Dataset of type T. I got the error that no encoder can be stored, which I think is because Scala does not know that T should be a case class. How can I tell the compiler that? I've seen somewhere that I should mention Product, but there is no such class defined.. Feel free to suggest other ways to do this!</p>
  
  <p>I have the following code but it is not compiling with the error: 42: error: Unable to find encoder for type stored in a Dataset.  Primitive types (Int, String, etc) and Product types (case classes) are supported by importing sqlContext.implicits._
  [INFO]       .as[T]</p>
  
  <p>I'm using Spark 1.6.1</p>
  
  <p>Code:</p>
  
  <pre><code>import org.apache.spark.{SparkConf, SparkContext}
  import org.apache.spark.sql.{Dataset, SQLContext}    
  
  /**
        * A trait that moves data on Hadoop with Spark based on the location and the granularity of the data.
        */
      trait Agent[T] {
        /**
          * Load a Dataframe from the location and convert into a Dataset
          * @return Dataset[T]
          */
        protected def load(): Dataset[T] = {
          // Read in the data
          SparkContextKeeper.sqlContext.read
            .format("com.databricks.spark.csv")
            .load("/myfolder/" + location + "/2016/10/01/")
            .as[T]
        }
      }
  </code></pre>