3

I'm running python code via ssh/PyCharm on a remote host, using a conda environment.
When trying to import a csv file into a PySpark data frame, like this

from pyspark.sql import SparkSession
url = "https://github.com/BigDaMa/COCOA/raw/master/dataset/movie.csv"
self.spark = SparkSession.builder.getOrCreate() 
df = self.spark.read.format("csv").load(url)

I get the following error message:

Traceback (most recent call last):
  File "/home/meike/anaconda3/envs/py3/lib/python3.9/site-packages/pyspark/sql/utils.py", line 111, in deco
    return f(*a, **kw)
  File "/home/meike/anaconda3/envs/py3/lib/python3.9/site-packages/py4j/protocol.py", line 326, in get_return_value
    raise Py4JJavaError(
py4j.protocol.Py4JJavaError: An error occurred while calling o28.load.
: java.lang.UnsupportedOperationException
    at org.apache.hadoop.fs.http.AbstractHttpFileSystem.listStatus(AbstractHttpFileSystem.java:91)
    at org.apache.hadoop.fs.http.HttpsFileSystem.listStatus(HttpsFileSystem.java:23)
    at org.apache.spark.util.HadoopFSUtils$.listLeafFiles(HadoopFSUtils.scala:225)
    at org.apache.spark.util.HadoopFSUtils$.$anonfun$parallelListLeafFilesInternal$1(HadoopFSUtils.scala:95)
    at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:238)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
    at scala.collection.TraversableLike.map(TraversableLike.scala:238)
    at scala.collection.TraversableLike.map$(TraversableLike.scala:231)
    at scala.collection.AbstractTraversable.map(Traversable.scala:108)
    at org.apache.spark.util.HadoopFSUtils$.parallelListLeafFilesInternal(HadoopFSUtils.scala:85)
    at org.apache.spark.util.HadoopFSUtils$.parallelListLeafFiles(HadoopFSUtils.scala:69)
    at org.apache.spark.sql.execution.datasources.InMemoryFileIndex$.bulkListLeafFiles(InMemoryFileIndex.scala:158)
    at org.apache.spark.sql.execution.datasources.InMemoryFileIndex.listLeafFiles(InMemoryFileIndex.scala:131)
    at org.apache.spark.sql.execution.datasources.InMemoryFileIndex.refresh0(InMemoryFileIndex.scala:94)
    at org.apache.spark.sql.execution.datasources.InMemoryFileIndex.<init>(InMemoryFileIndex.scala:66)
    at org.apache.spark.sql.execution.datasources.DataSource.createInMemoryFileIndex(DataSource.scala:581)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:417)
    at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:325)
    at org.apache.spark.sql.DataFrameReader.$anonfun$load$3(DataFrameReader.scala:307)
    at scala.Option.getOrElse(Option.scala:189)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:307)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:239)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    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.base/java.lang.Thread.run(Thread.java:829)

I have successfully imported the same csv into a pandas data frame, no problem here.
I am also able to create an empty data frame and fill it manually.

I have found this on StackOverflow, but as one of the commentators, I need to be able to use PySpark for debugging. I cannot simply run the code in the Terminal, using spark-submit.

I've also tried importing findspark and adding a MySQL package, but this doesn't solve the problem.

any ideas? if any more information is necessary, I'll be happy to add it!

PS: These are some warnings I'm getting, but they don't prevent my code from running so far.

Connected to pydev debugger (build 212.4746.96)
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.spark.unsafe.Platform (file:/home/meike/anaconda3/envs/py3/lib/python3.9/site-packages/pyspark/jars/spark-unsafe_2.12-3.1.2.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of org.apache.spark.unsafe.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
21/08/23 23:07:06 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Using Spark´s default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).

PPS: I've also managed to import the csv by copying it into the same directory as the main.py and reading it 'from local'. But the Script is meant to be executed with a URL given as input. Why isn't that working??

Meike
  • 171
  • 13

1 Answers1

3

You can't load csv directly into pyspark from url. Try this:

url = "https://github.com/BigDaMa/COCOA/raw/master/dataset/movie.csv"
from pyspark import SparkFiles
spark.sparkContext.addFile(url)
df = spark.read.csv("file://"+SparkFiles.get("movie.csv"), header=True, inferSchema= True)

Other approach would be to read from url via pandas and then create spark dataframe:

import pandas as pd
df = spark.createDataFrame(pd.read_csv(url)))
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • I successfully used your approach on the ubuntu server with my condo environment. now I'm trying the same from an AWS EMR Jupiter Notebook with spark kernel and I get the following error message: --- java.io.FileNotFoundException: File file:/mnt/tmp/spark-c9b0fb0d-5f1b-4bf5-930c-1cdab0ec58d1/userFiles-7f5e270a-688c-43c8-8312-121399b4cd15/movie.csv does not exist It is possible the underlying files have been updated. You can explicitly invalidate the cache in Spark by running 'REFRESH TABLE tableName' command in SQL or by recreating the Dataset/DataFrame involved. --- any ideas? – Meike Aug 28 '21 at 16:22
  • Could be permission issues that you dont have write permissions to write to the folder. – Waqar Ahmed Aug 29 '21 at 16:25
  • It is not working for me: https://ibb.co/tJ96QCN – Akash Kumar Feb 24 '22 at 15:17