Python 从Spark RDD中移除元件
我正在从文本文件构建RDD。有些行不符合我期望的格式,在这种情况下,我使用标记-1Python 从Spark RDD中移除元件,python,apache-spark,pyspark,Python,Apache Spark,Pyspark,我正在从文本文件构建RDD。有些行不符合我期望的格式,在这种情况下,我使用标记-1 def myParser(line): try: # do something except: return (-1, -1), -1 lines = sc.textFile('path_to_file') pairs = lines.map(myParser) 是否可以删除带有-1标记的行?如果没有,解决方法是什么?我能想到的最干净的解决方案是使用平面图丢弃格
def myParser(line):
try:
# do something
except:
return (-1, -1), -1
lines = sc.textFile('path_to_file')
pairs = lines.map(myParser)
是否可以删除带有
-1
标记的行?如果没有,解决方法是什么?我能想到的最干净的解决方案是使用平面图丢弃格式错误的行:
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)
pairs = lines.map(myParser).filter(lambda x: x != ((-1, -1), -1))
另见
您还可以在映射之后进行过滤
:
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)
pairs = lines.map(myParser).filter(lambda x: x != ((-1, -1), -1))
只需使用过滤器
。。。否?在Java中,我使用一个筛选器:.filter(新函数(){public Boolean call(CassandraRow行)抛出异常{return row.getString(“value”).equals(whatIWant);})。lambda函数的参数是什么。关键、价值还是两者兼而有之?