Scala要么[type1,type2]

问题描述 投票:1回答:1

以下是使用Either的一个工作示例:

val a: Either[Int, String] = {
if (true) 
    Left(42) // return an Int
else
    Right("Hello, world") // return a String
}

但是下面的代码不起作用:条件“text”只是确定输入文件是文本文件还是镶木地板文件

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
scala apache-spark
1个回答
2
投票

您的工作示例将准确地告诉您该做什么。只需将Spark返回的两个表达式包装到LeftRight中:

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
}

LeftRight分为两类,均来自Either。您可以使用new Left(expression)new Right(expression)创建实例。由于它们都是case类,因此可以省略new关键字,只需使用Left(expression)Right(expression)即可。

© www.soinside.com 2019 - 2024. All rights reserved.