具有一些空值的数组上的 Spark Stats

Spark Stats on a array that has some nulls

我有以下代码:

            myData3 = myData.map(lambda line: line.split(',')).map(lambda fields: ("Column", float(fields[0]))).map(lambda (column, value) : (value)).persist(StorageLevel.MEMORY_AND_DISK)

我将 if 语句放在那里是因为现在我有一些数据集包含整列。 float(fields[0]) 映射在遇到任何 null 时会导致错误。如何编写 spark 代码以允许我获取示例数组:[1,2,3,4,5,19] 并处理它?

地图前 运行 个过滤器:

.map(...split...)
.filter(lambda fields: fields[0] != null)
.map(...process...)

如果确实需要,您也可以使用 accumulator 来跟踪过滤掉的数据。

使用累加器它看起来更像(python 不是我常用的语言,所以它可能会有点偏差:

accum = sc.accumulator(0)

def filterWithAccum(fields):
  accum.add(1)
  return fields[0] != null

.map(...split...)
.filter(filterWithAccum)
.map(...process...)