gpt4 book ai didi

python - Apache Spark 读取 CSV 文件 - ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-01 01:38:37 25 4
gpt4 key购买 nike

我编写了 Spark 程序,它读取 CSV 文件并将结果写入控制台。我在运行时收到错误。我使用的是 Spark 2.2.0。

示例文件:

EmployeeID,FirstName,LastName,DepartmentId,Salaray
1,Gowdhaman,Dhandapani,IT,10000
2,Shaara,Gowdhaman,IT,150000
3,Karthiga,Gowdhaman,IT,120000
4,Aravind,Gunasekaran,Mech,100000
5,Padma,Dhandapani,Home,10000

程序:

from pyspark.sql import SparkSession

def read_csv(spark, filename):
df = spark.read.load(filename, format='.csv', sep=',', header = 'true')
return df


def main():
spark = SparkSession \
.builder \
.appName('Python Spark SQL Basic example') \
.getOrCreate()

emp = read_csv(spark, 'Employee.csv')
emp.show()


if __name__ == '__main__':
main()

错误:

/usr/bin/python /Users/gowdhaman/GDIPythonNoteBook/PythonLearn/SparkGDLearn/EmployeeFileLoad.py
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).
18/09/03 18:48:48 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Traceback (most recent call last):
File "/Users/gowdhaman/GDIPythonNoteBook/PythonLearn/SparkGDLearn/EmployeeFileLoad.py", line 19, in <module>
main()
File "/Users/gowdhaman/GDIPythonNoteBook/PythonLearn/SparkGDLearn/EmployeeFileLoad.py", line 14, in main
emp = read_csv(spark, 'Employee.csv')
File "/Users/gowdhaman/GDIPythonNoteBook/PythonLearn/SparkGDLearn/EmployeeFileLoad.py", line 4, in read_csv
df = spark.read.load(filename, format='.csv', sep=',', header = 'true')
File "/Library/Python/2.7/site-packages/pyspark/sql/readwriter.py", line 159, in load
return self._df(self._jreader.load(path))
File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 1133, in __call__
answer, self.gateway_client, self.target_id, self.name)
File "/Library/Python/2.7/site-packages/pyspark/sql/utils.py", line 63, in deco
return f(*a, **kw)
File "/Library/Python/2.7/site-packages/py4j/protocol.py", line 319, in get_return_value
format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o28.load.
: java.lang.ClassNotFoundException: Failed to find data source: .csv. Please find packages at http://spark.apache.org/third-party-projects.html
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:549)
at org.apache.spark.sql.execution.datasources.DataSource.providingClass$lzycompute(DataSource.scala:86)
at org.apache.spark.sql.execution.datasources.DataSource.providingClass(DataSource.scala:86)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:301)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:178)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:156)
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:280)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: .csv.DefaultSource
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21$$anonfun$apply$12.apply(DataSource.scala:533)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21$$anonfun$apply$12.apply(DataSource.scala:533)
at scala.util.Try$.apply(Try.scala:192)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21.apply(DataSource.scala:533)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21.apply(DataSource.scala:533)
at scala.util.Try.orElse(Try.scala:84)
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:533)
... 16 more

最佳答案

您不需要格式中的点

format='csv'

这也应该有效

spark.read\
.option("header", "true")\
.csv("some_input_file.csv")

关于python - Apache Spark 读取 CSV 文件 - ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52150705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com