如何在pyspark中添加spark excel

2jcobegt  于 2021-07-12  发布在  Spark
关注(0)|答案(1)|浏览(463)

我正在尝试将xlsx读取到pyspark,并尝试用多种方法导入sparkexcel库,但在读取xlsx文件时仍然会出错。我在mac上使用独立模式的spark。
我的代码:


# spark configuration

spark_path = "/spark/spark-3.0.1-bin-hadoop2.7"
findspark.init(spark_path)
spark = SparkSession.builder.master("local").appName("Word Count").config("--packages com.crealytics:spark-excel_2.12:0.13.7").getOrCreate()

data_location = "bank_transactions.xlsx"
df = spark.read.format("com.crealytics.spark.excel").load(data_location)

我得到以下错误:

Py4JJavaError: An error occurred while calling o37.load.
: java.lang.NoClassDefFoundError: scala/Product$class
    at com.crealytics.spark.excel.Utils$MapIncluding.<init>(Utils.scala:9)
    at com.crealytics.spark.excel.WorkbookReader$.<init>(WorkbookReader.scala:31)
    at com.crealytics.spark.excel.WorkbookReader$.<clinit>(WorkbookReader.scala)
    at com.crealytics.spark.excel.DefaultSource.createRelation(DefaultSource.scala:28)
    at com.crealytics.spark.excel.DefaultSource.createRelation(DefaultSource.scala:18)
    at com.crealytics.spark.excel.DefaultSource.createRelation(DefaultSource.scala:12)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:344)
    at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:297)
    at org.apache.spark.sql.DataFrameReader.$anonfun$load$2(DataFrameReader.scala:286)
    at scala.Option.getOrElse(Option.scala:189)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:286)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:232)
    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:564)
    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:832)
Caused by: java.lang.ClassNotFoundException: scala.Product$class
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 23 more
bvk5enib

bvk5enib1#

解决:
创建目录 spark_jars 在你的家里
导入库的更新代码:

findspark.init(spark_path)
spark = SparkSession.builder.master("local") \
    .appName("Word Count") \
    .config("spark.jars.packages","com.crealytics:spark-excel_2.12:0.13.7") \
    .getOrCreate()
spark

相关问题