我一直试图在Jupyter notebooks/google colab中使用pyspark从Azure Blob Storage读取JSON数据,并不断遇到相同的错误-java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.fs.azure.NativeAzureFileSystem$Secure not found
下面的代码将是我如何在谷歌colab中设置的,但在Jupyter中的差异是最小的。
我的设置:
我使用的是以前版本的spark,因为我在最新版本3.4.0中遇到了这个问题
!apt-get install openjdk-8-jdk-headless -qq > /dev/null
!wget -q http://archive.apache.org/dist/spark/spark-3.1.1/spark-3.1.1-bin-hadoop3.2.tgz
!tar xf spark-3.1.1-bin-hadoop3.2.tgz
!pip install -q findspark
import os
os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"
os.environ["SPARK_HOME"] = "/content/spark-3.1.1-bin-hadoop3.2"
在spark目录-“/content/spark-3.1.1-bin-hadoop 3.2/jars”中-我放置了以下jar:
- azure-storage-8.6.6.jar
- hadoop-azure-3.5.5.jar
- jetty-util-11.0.15.jar
- jetty-util-ajax-11.0.15.jar
import findspark
findspark.init()
import pyspark
from pyspark.sql import SparkSession
conf = pyspark.SparkConf()
conf.set(
"spark.jars.packages",
"org.apache.hadoop:hadoop-azure-3.3.5,com.microsoft.azure:azure-storage-8.6.6"
# I have also tried hadoop-azure:3.3.5 and azure-storage:8.6.6 (: instead of -)
)
conf.set(
"fs.azure.account.key.<STORAGE_ACCOUNT>.blob.core.windows.net", "<TOKEN>")
spark = SparkSession.builder.master("local[*]")\
.config(conf=conf)\
.getOrCreate()
spark.conf.set("spark.sql.repl.eagerEval.enabled", True)
spark
然后尝试以几种不同的格式读取json和csv数据:
df1 = spark.read.format('json').\
load("wasbs://{CONTAINER}@{ACCOUNT}.blob.core.windows.net/{FILE_PATH}.json")
df2 = spark.read.json("wasbs://{CONTAINER}@{ACCOUNT}.blob.core.windows.net/{FILE_PATH}.json")```
df3 = spark.read.csv("wasbs://{CONTAINER}@{ACCOUNT}.blob.core.windows.net/{FILE_PATH}.csv")```
上面的每一个都抛出错误:
Py4JJavaError: An error occurred while calling o233.load.
: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.fs.azure.NativeAzureFileSystem$Secure not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2595)
at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:3269)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:3301)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:124)
at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:3352)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:3320)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:479)
at org.apache.hadoop.fs.Path.getFileSystem(Path.java:361)
at org.apache.spark.sql.execution.streaming.FileStreamSink$.hasMetadata(FileStreamSink.scala:46)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:376)
at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:326)
at org.apache.spark.sql.DataFrameReader.$anonfun$load$3(DataFrameReader.scala:308)
at scala.Option.getOrElse(Option.scala:189)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:308)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:240)
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:750)
Caused by: java.lang.ClassNotFoundException: Class org.apache.hadoop.fs.azure.NativeAzureFileSystem$Secure not found
at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:2499)
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2593)
... 25 more
我已确保我的Azure帐户信息,密钥和文件路径正确,并且通常读取pyspark的工作-通过以下方式验证:
df = spark.read.csv('sample_data/california_housing_test.csv', header=True, sep=";")
df.show(5)
2条答案
按热度按时间vulvrdjw1#
我尝试使用以下代码从Google Collab中的Azure Blob存储中读取数据:
我得到了以下错误:
我尝试了下面的代码,我能够从blob存储中读取数据:
im9ewurl2#
在尝试了配置设置以及如何将jar带入环境之后,我能够让它工作:
注意“spark.jars.packages”更改为“spark.jars”,然后指向上面定义的文件路径。
**请注意,此修复在Colab中有效,我仍然没有完全让Jupyter notebook正常工作。