val rddFromFile = spark.sparkContext.textFile("test.txt").collect().mkString(" ")
注:本地文件的话,这里用相对路径和绝对路径都可以,或直接传hdfs路径
取Array[String]的第一个元素:
val rddFromFile = spark.sparkContext.textFile("test.txt").collect()(0)
参考:https://sparkbyexamples.com/spark/spark-read-text-file-rdd-dataframe/
scala> val args = Array("Hello", "world", "it's", "me") val args: Array[String] = Array(Hello, world, it's, me) scala> val string = args.mkString(" ") val string: String = Hello world it's me scala> val string = args.mkString(" ") val string: String = Hello world it's me