Spark scala String Array转为String

val rddFromFile = spark.sparkContext.textFile("test.txt").collect().mkString("\n")

注:本地文件的话,这里用相对路径和绝对路径都可以,或直接传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("\n")
val string: String =
Hello
world
it's
me
posted @ 2021-06-30 20:09  船长博客  阅读(761)  评论(0编辑  收藏  举报
永远相信美好的事情即将发生!