【问题标题】:Getting Pyspark error of 'split' is not in list while calling split() function调用 split() 函数时出现“split”的 Pyspark 错误不在列表中
【发布时间】:2019-09-30 21:33:21
【问题描述】:

我创建了一个如下的数据框

spark= SparkSession.builder.appName("test").getOrCreate()
categories=spark.read.text("resources/textFile/categories")
categories.show(n=2)
+------------+
|       value|
+------------+
|1,2,Football|
|  2,2,Soccer|
+------------+
only showing top 2 rows

现在当我将此数据帧转换为RDD并尝试根据“,”(逗号)分割RDD的每一行

crdd=categories.rdd.map(lambda line: line.split(',')[1])
crdd.foreach(lambda lin : print(lin))

将位置 1 的元素添加到 crdd RDD 时,出现以下错误

Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 13.0 failed 1 times, most recent failure: Lost task 0.0 in stage 13.0 (TID 13, localhost, executor driver): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "C:\Users\Downloads\bigdataSetup\spark-2.2.1-bin-hadoop2.7\python\lib\pyspark.zip\pyspark\sql\types.py", line 1504, in __getattr__
    idx = self.__fields__.index(item)
ValueError: 'split' is not in list

注意:此处为CSV格式的数据只是为了方便复制。

【问题讨论】:

  • 这意味着 map 正在接收行列表然后你需要这样做 >>> categories.rdd.map(lambda x:x[0].split(",")[1] ).take(3)

标签: apache-spark pyspark apache-spark-sql pyspark-sql


【解决方案1】:

由于您的数据是 CSV 格式,您可以使用 read.csv API:

categories=spark.read.csv("resources/textFile/categories")

修改你的代码如下:

crdd = categories.rdd.map(lambda line: line.value.split(',')[1])

for i in crdd.take(10): print (i)

【讨论】:

  • 我知道有一个CSV模块,这里的CSV只是为了方便复制。我更感兴趣的是解决问题。
  • 查看添加的代码来解决您的问题。值是数据框的列名。
猜你喜欢
  • 2020-05-11
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多