编程语言
首页 > 编程语言> > python – 遇到丢失的功能时,Apache Spark会抛出NullPointerException

python – 遇到丢失的功能时,Apache Spark会抛出NullPointerException

作者:互联网

在为要素中的字符串列编制索引时,我对PySpark有一个奇怪的问题.这是我的tmp.csv文件:

x0,x1,x2,x3 
asd2s,1e1e,1.1,0
asd2s,1e1e,0.1,0
,1e3e,1.2,0
bd34t,1e1e,5.1,1
asd2s,1e3e,0.2,0
bd34t,1e2e,4.3,1

我在’x0’中有一个缺失值.
首先,我正在使用pyspark_csv:https://github.com/seahboonsiew/pyspark-csv将csv文件中的功能读入DataFrame
然后使用StringIndexer索引x0:

import pyspark_csv as pycsv
from pyspark.ml.feature import StringIndexer

sc.addPyFile('pyspark_csv.py')

features = pycsv.csvToDataFrame(sqlCtx, sc.textFile('tmp.csv'))
indexer = StringIndexer(inputCol='x0', outputCol='x0_idx' )
ind = indexer.fit(features).transform(features)
print ind.collect()

当调用”ind.collect()”时,Spark会抛出java.lang.NullPointerException.一切都适用于完整的数据集,例如,对于’x1′.

有没有人知道造成这种情况的原因以及如何解决这个问题?

提前致谢!

谢尔盖

更新:

我使用Spark 1.5.1.确切的错误:

File "/spark/spark-1.4.1-bin-hadoop2.6/python/pyspark/sql/dataframe.py", line 258, in show
print(self._jdf.showString(n))

File "/spark/spark-1.4.1-bin-hadoop2.6/python/lib/py4j-0.8.2.1-src.zip/py4j/java_gateway.py", line 538, in __call__

File "/spark/spark-1.4.1-bin-hadoop2.6/python/lib/py4j-0.8.2.1-src.zip/py4j/protocol.py", line 300, in get_return_value

py4j.protocol.Py4JJavaError: An error occurred while calling o444.showString.
: java.lang.NullPointerException
at org.apache.spark.sql.types.Metadata$.org$apache$spark$sql$types$Metadata$$hash(Metadata.scala:208)
at org.apache.spark.sql.types.Metadata$$anonfun$org$apache$spark$sql$types$Metadata$$hash$2.apply(Metadata.scala:196)
at org.apache.spark.sql.types.Metadata$$anonfun$org$apache$spark$sql$types$Metadata$$hash$2.apply(Metadata.scala:196)
... etc

我试图在不读取csv文件的情况下创建相同的DataFrame,

df = sqlContext.createDataFrame(
  [('asd2s','1e1e',1.1,0), ('asd2s','1e1e',0.1,0), 
  (None,'1e3e',1.2,0), ('bd34t','1e1e',5.1,1), 
  ('asd2s','1e3e',0.2,0), ('bd34t','1e2e',4.3,1)],
  ['x0','x1','x2','x3'])

它给出了同样的错误.有点不同的例子很好,

df = sqlContext.createDataFrame(
  [(0, None, 1.2), (1, '06330986ed', 2.3), 
  (2, 'b7584c2d52', 2.5), (3, None, .8), 
  (4, 'bd17e19b3a', None), (5, '51b5c0f2af', 0.1)],
  ['id', 'x0', 'num'])

// after indexing x0

+---+----------+----+------+
| id|        x0| num|x0_idx|
+---+----------+----+------+
|  0|      null| 1.2|   0.0|
|  1|06330986ed| 2.3|   2.0|
|  2|b7584c2d52| 2.5|   4.0|
|  3|      null| 0.8|   0.0|
|  4|bd17e19b3a|null|   1.0|
|  5|51b5c0f2af| 0.1|   3.0|
+---+----------+----+------+

更新2:

我刚刚在Scala中发现了同样的问题,所以我猜这只是Spark bug而不是PySpark.特别是数据框架

val df = sqlContext.createDataFrame(
  Seq(("asd2s","1e1e",1.1,0), ("asd2s","1e1e",0.1,0), 
      (null,"1e3e",1.2,0), ("bd34t","1e1e",5.1,1), 
      ("asd2s","1e3e",0.2,0), ("bd34t","1e2e",4.3,1))
).toDF("x0","x1","x2","x3")

索引’x0’功能时抛出java.lang.NullPointerException.此外,当在下面的数据帧中索引’x0’时

val df = sqlContext.createDataFrame(
  Seq((0, null, 1.2), (1, "b", 2.3), 
      (2, "c", 2.5), (3, "a", 0.8), 
      (4, "a", null), (5, "c", 0.1))
).toDF("id", "x0", "num")

我有’java.lang.UnsupportedOperationException:不支持类型Any的模式’,这是由第5个向量中缺少’num’值引起的.如果用数字替换它,一切都很好,甚至在第一个向量中缺少值.

我也尝试过旧版本的Spark(1.4.1),结果是一样的.

解决方法:

它看起来像你正在使用的模块将空字符串转换为空值,并且它在某些时候与下游处理混乱.乍一看it looks like a PySpark bug.

怎么解决?一个简单的解决方法是在索引之前删除空值:

features.na.drop()

或者用一些占位符替换null:

from pyspark.sql.functions import col, when

features.withColumn(
    "x0", when(col("x0").isNull(), "__SOME_PLACEHOLDER__").otherwise(col("x0")))

此外,您可以使用spark-csv.它是高效的,经过测试,并且奖励不会将空字符串转换为空值.

features = (sqlContext.read
    .format('com.databricks.spark.csv')
    .option("inferSchema", "true")
    .option("header", "true")
    .load("tmp.csv"))

标签:apache-spark-ml,python,apache-spark,pyspark,apache-spark-sql
来源: https://codeday.me/bug/20190929/1832026.html