我想創建一個應該用案例類T實現的Scala特徵。特徵僅僅是加載數據並將其轉換爲Spark數據集類型T.我得到的錯誤,沒有編碼器可以存儲,我認爲這是因爲斯卡拉不知道T應該是一個案例類。我怎樣才能告訴編譯器?我曾經見過某處應該提及產品,但是沒有定義這樣的類。請隨意建議其他方法來執行此操作!如何使用在Scala中創建數據集的通用案例類實現特徵
我有以下代碼,但它不會編譯錯誤:42:錯誤:無法找到存儲在數據集中的類型的編碼器。原語類型(int,字符串等)和產品類型(case類)通過導入sqlContext.implicits._ [INFO]。如[T]
我使用火花1.6.1
支持代碼:
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")
.option("header", header) // Use first line of all files as header
.option("inferSchema", "true") // Automatically infer data types
.option("delimiter", "|") // Deloitte always expects pipe as a delimiter
.option("dateFormat","yyyy-MM-dd") // Deloitte always expects this kind of Date format
.load("/iacc/eandis/landing/raw/" + location + "/2016/10/01/")
.as[T]
}
}
http://stackoverflow.com/questions/34715611/why-is-the-error-unable-to-find-encoder-for-type-stored-in-a-dataset-when-enco和http://stackoverflow.com/questions/38664972/why-is-unable-to-find-encoder-for-type-stored-in-a-dataset-when-creating-a-dat – Shankar