1

I want to write a csv file on S3 which should be formed from a dataframe. I tried saving the dataframe to csv as in the normal api but unfortunately, that is not accessible later on while uploading the file to S3.

I then thought of saving the file directly to S3 which I have been able to do so in a normal spark but not in here.

I am using AWS EMR's spark cluster. I have tried multiple formats, each of them end up in a result.

One thing worked out, saving a file to HDFS and then reading it. But, I was not able to access the HDFS path and hence, was not able to upload the file.

>>> 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

I expect the csv file or any other file getting uploaded to the object storage.

Aviral Srivastava
  • 4,058
  • 8
  • 29
  • 81

2 Answers2

1

I faced the same kind issue some time back.

add the similar python related lines to the spark session and add s3-python depedency.


    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")
Ravi
  • 424
  • 3
  • 13
  • Could you tell me how to do that in pyspark? Because I am getting this error while running the first line: `sc.hadoopConfiguration.set("fs.s3n.impl", "org.apache.hadoop.fs.s3native.NativeS3FileSystem")`. The error is: `AttributeError: 'SparkContext' object has no attribute 'hadoopConfiguration'` – Aviral Srivastava Jul 12 '19 at 06:36
  • Also, I made the changes but now facing this error: `java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.amazon.ws.emr.hadoop.fs.EmrFileSystem not found` – Aviral Srivastava Jul 12 '19 at 06:44
1

Try this

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

If there any other dependencies that are missing add them too.

gorros
  • 1,411
  • 1
  • 18
  • 29