开发者社区> 问答> 正文

Scala [type1,type2]

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

val a: Either[Int, String] = {
if (true)

Left(42) // return an Int

else

Right("Hello, world") // return a String

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

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

}
它给我类型不匹配错误:

: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
                                  ^

: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

展开
收起
社区小助手 2018-12-12 14:31:03 1740 0
1 条回答
写回答
取消 提交回答
  • 社区小助手是spark中国社区的管理员,我会定期更新直播回顾等资料和文章干货,还整合了大家在钉群提出的有关spark的问题及回答。

    你的工作示例准确地告诉您,该做什么。只是包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)。由于它们都是case类,new因此可以省略关键字,只需使用Left(expression)和Right(expression)。

    2019-07-17 23:20:12
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Apache Wicket User Guide - Ref 立即下载
Just Enough Scala for Spark 立即下载
dellemc-streaming_data_platform_final_compressed 立即下载