在Spark中,如果没有RDD,如何在Hadoop上编写文件?
Spark RDD具有在Spark中,如果没有RDD,如何在Hadoop上编写文件?,hadoop,apache-spark,hdfs,Hadoop,Apache Spark,Hdfs,Spark RDD具有saveAsTxtFile功能。但是,如何打开文件并将简单字符串写入hadoop存储 val sparkConf: SparkConf = new SparkConf().setAppName("example") val sc: SparkContext = new SparkContext(sparkConf) sc.hadoopConfiguration.set("fs.s3n.awsAccessKeyId", "...") sc.hadoopConfigurati
saveAsTxtFile
功能。但是,如何打开文件并将简单字符串写入hadoop存储
val sparkConf: SparkConf = new SparkConf().setAppName("example")
val sc: SparkContext = new SparkContext(sparkConf)
sc.hadoopConfiguration.set("fs.s3n.awsAccessKeyId", "...")
sc.hadoopConfiguration.set("fs.s3n.awsSecretAccessKey", "...")
val lines: RDD[String] = sc.textFile("s3n://your-output-bucket/lines.txt")
val lengths: RDD[Int] = lines.map(_.length)
lengths.saveAsTextFile("s3n://your-output-bucket/lenths.txt")
val numLines: Long = lines.count
val resultString: String = s"numLines: $numLines"
// how to save resultString to "s3n://your-output-bucket/result.txt"
sc.stop()
为什么不这样做呢
val strings = sc.parallelize(Seq("hello", "there"), <numPartitions>)
strings.saveAsTextFile("<path-to-file>")
val strings=sc.parallelize(Seq(“hello”,“there”),)
strings.saveAsTextFile(“”)
否则,您可能需要查看hadoop API来编写文件并从驱动程序显式调用该代码。为什么不执行以下操作
val strings = sc.parallelize(Seq("hello", "there"), <numPartitions>)
strings.saveAsTextFile("<path-to-file>")
val strings=sc.parallelize(Seq(“hello”,“there”),)
strings.saveAsTextFile(“”)
否则,您可能需要查看hadoop API来编写文件并从驱动程序显式调用该代码。假设您有一个绑定到
sc
的SparkContext
:
import java.io.{BufferedWriter,OutputStreamWriter}
val hdfs=org.apache.hadoop.fs.FileSystem.get(sc.hadoopConfiguration)
val输出路径=
新的org.apache.hadoop.fs.Path(“hdfs://localhost:9000//tmp/hello.txt")
val overwrite=true
val bw=
新建BufferedWriter(新建OutputStreamWriter(hdfs.create(outputPath,overwrite)))
写(“你好,世界”)
bw.close()
注意:为了保持简单,没有代码在出现异常时关闭编写器。假设您有一个绑定到
sc
的SparkContext
:
import java.io.{BufferedWriter,OutputStreamWriter}
val hdfs=org.apache.hadoop.fs.FileSystem.get(sc.hadoopConfiguration)
val输出路径=
新的org.apache.hadoop.fs.Path(“hdfs://localhost:9000//tmp/hello.txt")
val overwrite=true
val bw=
新建BufferedWriter(新建OutputStreamWriter(hdfs.create(outputPath,overwrite)))
写(“你好,世界”)
bw.close()
注意:为了保持简单,没有代码在出现异常时关闭writer。thx。我可以使用“s3n://your output bucket/result.txt”url而不是hdfs://localhost:9000//tmp/hello.txt“?thx。我可以使用“s3n://your output bucket/result.txt”url而不是hdfs://localhost:9000//tmp/hello.txt"?