如何使用 FileWriter 将文件写入 google dataproc?
How to write a file using FileWriter to google dataproc?
我有一个 java spark 应用程序,需要收集 spark 作业的输出,然后将其保存到 csv 文件中。这是我的代码:
fileWriter = new FileWriter("gs://dataflow-exp1/google_storage_tests/20170524/outputfolder/Test.csv", true);
fileWriter.append("col1,col2,col3,col4");
当我在 google 数据过程中执行 spark 作业时,出现找不到文件异常。我也确实对该文件夹具有 read/write 权限。
java.io.FileNotFoundException: gs:/dataflow-exp1/google_storage_tests/20170524/outputfolder/Test.csv (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:133)
at java.io.FileWriter.<init>(FileWriter.java:78)
at com.src.main.MyApp.testWriteOutput(MyApp.java:72)
at com.src.main.MyApp.main(MyApp.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
at org.apache.spark.deploy.SparkSubmit$.doRunMain(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
看起来文件编写器在运行时使用单斜杠 /
而不是 gs:
之后的双斜杠 //
。我该如何解决这个问题?
我也愿意使用其他方式代替 FileWriter 将文件写入 google 数据进程。
fileWriter = new FileWriter("gs:/"+"/dataflowexp1/google_storage_tests/20170524/outputfolder/Test.csv", true);
fileWriter.append("col1,col2,col3,col4");
//试试这个
Dataproc 为可从 Spark 访问的 GCS 安装 Hadoop FileSystem 连接器;一般来说,Hadoop 或 Spark 中的东西应该建立在该接口之上,不 与基本 Java 文件接口自动兼容。你应该这样做:
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;
Path outputPath = new Path("gs://dataflow-exp1/google_storage_tests/20170524/outputfolder/Test.csv");
OutputStream out = outputPath.getFileSystem(new Configuration()).create(outputPath);
然后根据您需要的任何编写器界面对其进行调整。
我有一个 java spark 应用程序,需要收集 spark 作业的输出,然后将其保存到 csv 文件中。这是我的代码:
fileWriter = new FileWriter("gs://dataflow-exp1/google_storage_tests/20170524/outputfolder/Test.csv", true);
fileWriter.append("col1,col2,col3,col4");
当我在 google 数据过程中执行 spark 作业时,出现找不到文件异常。我也确实对该文件夹具有 read/write 权限。
java.io.FileNotFoundException: gs:/dataflow-exp1/google_storage_tests/20170524/outputfolder/Test.csv (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:133)
at java.io.FileWriter.<init>(FileWriter.java:78)
at com.src.main.MyApp.testWriteOutput(MyApp.java:72)
at com.src.main.MyApp.main(MyApp.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
at org.apache.spark.deploy.SparkSubmit$.doRunMain(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
看起来文件编写器在运行时使用单斜杠 /
而不是 gs:
之后的双斜杠 //
。我该如何解决这个问题?
我也愿意使用其他方式代替 FileWriter 将文件写入 google 数据进程。
fileWriter = new FileWriter("gs:/"+"/dataflowexp1/google_storage_tests/20170524/outputfolder/Test.csv", true); fileWriter.append("col1,col2,col3,col4");
//试试这个
Dataproc 为可从 Spark 访问的 GCS 安装 Hadoop FileSystem 连接器;一般来说,Hadoop 或 Spark 中的东西应该建立在该接口之上,不 与基本 Java 文件接口自动兼容。你应该这样做:
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;
Path outputPath = new Path("gs://dataflow-exp1/google_storage_tests/20170524/outputfolder/Test.csv");
OutputStream out = outputPath.getFileSystem(new Configuration()).create(outputPath);
然后根据您需要的任何编写器界面对其进行调整。