2024-04-29 14:55:30 发布
网友
我正在从一个文本文件构建一个RDD。有些行不符合我期望的格式,在这种情况下,我使用标记-1。
def myParser(line): try: # do something except: return (-1, -1), -1 lines = sc.textFile('path_to_file') pairs = lines.map(myParser)
是否可以删除带有-1标记的行?如果没有,有什么解决办法?
-1
我能想到的最干净的解决方案是使用flatMap丢弃格式错误的行:
flatMap
def myParser(line): try: # do something return [result] # where result is the value you want to return except: return [] sc.textFile('path_to_file').flatMap(myParser)
另见What is the equivalent to scala.util.Try in pyspark?
也可以在map之后进行筛选:
map
pairs = lines.map(myParser).filter(lambda x: x != ((-1, -1), -1))
我能想到的最干净的解决方案是使用
flatMap
丢弃格式错误的行:另见What is the equivalent to scala.util.Try in pyspark?
也可以在
map
之后进行筛选:相关问题 更多 >
编程相关推荐