【问题标题】:Pyspark RDD - both filtered and unfiltered dataPyspark RDD - 过滤和未过滤的数据
【发布时间】:2021-02-13 09:19:46
【问题描述】:

我从 spark 会话创建了一个 RDD。我的数据是系统日志格式。我想将前缀为 1 的行过滤为有效行,将不满足条件的行过滤为无效行。

rdd2=rdd1.filter(lambda rec: rec.startswith('<128>1') or rec.startswith('<134>1'))

如何在同一个函数中获取无效行? 参考:https://sparkbyexamples.com/pyspark/pyspark-where-filter/ spark filter with higher order function

【问题讨论】:

    标签: apache-spark pyspark rdd


    【解决方案1】:

    您可以只使用有效条件的否定(not):

    valid = rdd1.filter(lambda rec: rec.startswith('<128>1') or rec.startswith('<134>1'))
    invalid = rdd1.filter(lambda rec: not(rec.startswith('<128>1') or rec.startswith('<134>1')))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多