如何从pyspark数据框中保存文件,以便以后可以将其上传到S3?

时间:2019-07-11 12:41:56

标签: python python-3.x apache-spark pyspark amazon-emr

我想在S3上写一个csv文件,该文件应该由dataframe组成。我曾尝试像普通api中一样将数据帧保存到csv,但是不幸的是,稍后在将文件上传到S3时无法访问。

然后我想到了将文件直接保存到S3的功能,这是我能够正常执行的操作,但是不能在这里保存。

我正在使用AWS EMR的spark集群。我尝试了多种格式,每种格式最终都会产生结果。

解决了一个问题,将文件保存到HDFS,然后读取它。但是,我无法访问HDFS路径,因此无法上传文件。

>>> df.write.parquet("s3a://demo-atlan-lake/shri/test.parquet",mode="overwrite")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/spark/python/pyspark/sql/readwriter.py", line 839, in parquet
    self._jwrite.parquet(path)
  File "/usr/lib/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in __call__
  File "/usr/lib/spark/python/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/usr/lib/spark/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o101.parquet.
: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.fs.s3a.S3AFileSystem not found
    at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2369)
    at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2840)
    at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2857)
    at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:99)
    at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2896)
    at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2878)
    at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:392)
    at org.apache.hadoop.fs.Path.getFileSystem(Path.java:356)
    at org.apache.spark.sql.execution.datasources.DataSource.planForWritingFileFormat(DataSource.scala:424)
    at org.apache.spark.sql.execution.datasources.DataSource.planForWriting(DataSource.scala:524)
    at org.apache.spark.sql.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:290)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:271)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:229)
    at org.apache.spark.sql.DataFrameWriter.parquet(DataFrameWriter.scala:566)
    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 py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: Class org.apache.hadoop.fs.s3a.S3AFileSystem not found
    at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:2273)
    at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2367)
    ... 24 more

我希望将csv文件或任何其他文件上传到对象存储。

2 个答案:

答案 0 :(得分:1)

前段时间我遇到了同样的问题。

将相似的与python相关的行添加到spark会话,并添加s3-python depency。


    sparkSession.sparkContext.hadoopConfiguration.set("fs.s3n.impl", "org.apache.hadoop.fs.s3native.NativeS3FileSystem")
    sparkSession.sparkContext.hadoopConfiguration.set("fs.s3a.access.key", "access key")
    sparkSession.sparkContext.hadoopConfiguration.set("fs.s3a.secret.key", "secret key")

答案 1 :(得分:1)

尝试一下

pyspark --packages com.amazonaws:aws-java-sdk-s3:1.11.461,org.apache.hadoop:hadoop-aws:2.8.5

如果还有其他依赖项缺失,请添加它们。