Py4JJavaError运行Pyspark程序

时间:2019-03-21 14:22:45

标签: python apache-spark pyspark

我正在做一个简短的应用程序,根据它们的出现次数,它会告诉您最大的Marvel电影数据集中哪个是最受欢迎的英雄。

我已经从Anaconda环境和控制台安装了Pyspark,以尝试解决此错误而没有结果。我还为conda安装了Java-jdk,但也没有结果。

我得到的错误如下:

    py4j.protocol.Py4JJavaError: An error occurred while calling o24.partitions.
: java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: 2018:19
    at org.apache.hadoop.fs.Path.initialize(Path.java:205)
    at org.apache.hadoop.fs.Path.<init>(Path.java:171)
    at org.apache.hadoop.fs.Path.<init>(Path.java:93)
    at org.apache.hadoop.fs.Globber.glob(Globber.java:211)
    at org.apache.hadoop.fs.FileSystem.globStatus(FileSystem.java:1676)
    at org.apache.hadoop.mapred.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:259)
    at org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:229)
    at org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:315)
    at org.apache.spark.rdd.HadoopRDD.getPartitions(HadoopRDD.scala:204)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:253)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:251)
    at scala.Option.getOrElse(Option.scala:121)
    at org.apache.spark.rdd.RDD.partitions(RDD.scala:251)
    at org.apache.spark.rdd.MapPartitionsRDD.getPartitions(MapPartitionsRDD.scala:49)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:253)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:251)
    at scala.Option.getOrElse(Option.scala:121)
    at org.apache.spark.rdd.RDD.partitions(RDD.scala:251)
    at org.apache.spark.api.java.JavaRDDLike$class.partitions(JavaRDDLike.scala:61)
    at org.apache.spark.api.java.AbstractJavaRDDLike.partitions(JavaRDDLike.scala:45)
    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:483)
    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:745)
Caused by: java.net.URISyntaxException: Relative path in absolute URI: 2018:19
    at java.net.URI.checkPath(URI.java:1823)
    at java.net.URI.<init>(URI.java:745)
    at org.apache.hadoop.fs.Path.initialize(Path.java:202)
    ... 30 more

这是我的代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 20 13:33:45 2019

@author: Carlos
"""
from pyspark import SparkConf, SparkContext
import collections

conf = SparkConf().setMaster("local").setAppName("personaje_mas_popular")
sc=SparkContext(conf=conf)

def numerocoapariciones(linea):
    elementos = linea.split()
    return (int(elementos[0]), len(elementos)-1)

def codificarnombres(linea):
    fields = linea.split('\"')
    return (int(fields[0]), fields[1].encode("utf8"))

nombres = sc.textFile("./Marvel/Marvel-names.txt")
nombresrdd=nombres.map(codificarnombres)

lines = sc.textFile("./Marvel/Marvel-graph.txt")

emparejar = lines.map(numerocoapariciones)
totalapariciones = emparejar.reduceByKey(lambda x,y :x + y)

flipped = totalapariciones.map(lambda xy: (xy[1], xy[0]))

maspopular = flipped.max()

nombremaspopular = nombresrdd.lookup(maspopular[1])[0]
print("Héroe más popular: \n" + str(nombremaspopular))

我缺少图书馆吗?是版本问题吗?我正在运行Python 3.7.0,Pyspark 2.4.0并在Spyder(Anaconda环境)上编写代码。

1 个答案:

答案 0 :(得分:1)

尝试了不同的事情之后,这是最简单的事情:文件路径问题。

我的.py文件和.txt文件的原始路径都包含一个“:”,我在多个站点中读取的内容在读取文件路径时可能会导致错误。我只是将.py和.txt都移到了其他路径(Desktop,使事情变得100%简单),并且运行良好。

在解决此问题的过程中,我还遇到一个问题,即从终端更新pyspark之后,Anaconda Environment无法启动,因此我使用以下命令更新了python并再次使用了魅力:

conda update python -yn root

(我知道最后一个问题是题外话,但是,希望有一天能对某人有所帮助)