以下是使用Either的一个有效示例:
val a: Either[Int, String] = {
if (true)
Left(42) // return an Int
else
Right("Hello, world") // return a String
}
但是以下方法不起作用: 条件“文本”仅用于确定输入文件是文本文件还是镶木地板文件
val a: Either[org.apache.spark.rdd.RDD[String], org.apache.spark.rdd.RDD[org.apache.spark.sql.Row]] = {
if (text)
spark.sparkContext.textFile(input_path + "/lineitem.tbl") // read in text file as rdd
else
sparkSession.read.parquet(input_path + "/lineitem").rdd //read in parquet file as df, convert to rdd
}
它给了我类型不匹配的错误:
<console>:33: error: type mismatch;
found : org.apache.spark.rdd.RDD[String]
required: scala.util.Either[org.apache.spark.rdd.RDD[String],org.apache.spark.rdd.RDD[org.apache.spark.sql.Row]]
spark.sparkContext.textFile(input_path + "/lineitem.tbl") // read in text file as rdd
^
<console>:35: error: type mismatch;
found : org.apache.spark.rdd.RDD[org.apache.spark.sql.Row]
required: scala.util.Either[org.apache.spark.rdd.RDD[String],org.apache.spark.rdd.RDD[org.apache.spark.sql.Row]]
sparkSession.read.parquet(input_path + "/lineitem").rdd //read in parquet file as df, convert to rdd
答案 0 :(得分:2)
您的工作示例准确地告诉您该怎么做。只需将Spark返回的两个表达式包装成Left
和Right
:
val a: Either[org.apache.spark.rdd.RDD[String], org.apache.spark.rdd.RDD[org.apache.spark.sql.Row]] = {
if (text)
Left(spark.sparkContext.textFile(input_path + "/lineitem.tbl")) // read in text file as rdd
else
Right(sparkSession.read.parquet(input_path + "/lineitem").rdd) //read in parquet file as df, convert to rdd
}
Left
和Right
是两个类,都从Either
扩展。您可以使用new Left(expression)
和new Right(expression)
创建实例。由于它们都是案例类,因此可以省略new
关键字,而只需使用Left(expression)
和Right(expression)
。