【发布时间】: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