我有一个糟糕的HTTPD access_log,只想跳过“糟糕”的行.
在scala中,这很简单:
import scala.util.Try
val log = sc.textFile("access_log")
log.map(_.split(' ')).map(a => Try(a(8))).filter(_.isSuccess).map(_.get).map(code => (code,1)).reduceByKey(_ + _).collect()
对于python我通过使用“lambda”表示法明确定义函数来获得以下解决方案:
log = sc.textFile("access_log")
def wrapException(a):
try:
return a[8]
except:
return 'error'
log.map(lambda s : s.split(' ')).map(wrapException).filter(lambda s : s!='error').map(lambda code : (code,1)).reduceByKey(lambda acu,value : acu + value).collect()
在pyspark中有更好的方法(例如在Scala中)吗?
非常感谢!
解决方法:
更好是一个主观的术语,但你可以尝试一些方法.
>在这种特殊情况下,您可以做的最简单的事情是避免任何例外情况.您只需要一个flatMap和一些切片:
log.flatMap(lambda s : s.split(' ')[8:9])
如您所见,它意味着不需要异常处理或后续过滤器.
>可以使用简单的包装器扩展以前的想法
def seq_try(f, *args, **kwargs):
try:
return [f(*args, **kwargs)]
except:
return []
和示例用法
from operator import div # FYI operator provides getitem as well.
rdd = sc.parallelize([1, 2, 0, 3, 0, 5, "foo"])
rdd.flatMap(lambda x: seq_try(div, 1., x)).collect()
## [1.0, 0.5, 0.3333333333333333, 0.2]
>最后更多OO方法:
import inspect as _inspect
class _Try(object): pass
class Failure(_Try):
def __init__(self, e):
if Exception not in _inspect.getmro(e.__class__):
msg = "Invalid type for Failure: {0}"
raise TypeError(msg.format(e.__class__))
self._e = e
self.isSuccess = False
self.isFailure = True
def get(self): raise self._e
def __repr__(self):
return "Failure({0})".format(repr(self._e))
class Success(_Try):
def __init__(self, v):
self._v = v
self.isSuccess = True
self.isFailure = False
def get(self): return self._v
def __repr__(self):
return "Success({0})".format(repr(self._v))
def Try(f, *args, **kwargs):
try:
return Success(f(*args, **kwargs))
except Exception as e:
return Failure(e)
和示例用法:
tries = rdd.map(lambda x: Try(div, 1.0, x))
tries.collect()
## [Success(1.0),
## Success(0.5),
## Failure(ZeroDivisionError('float division by zero',)),
## Success(0.3333333333333333),
## Failure(ZeroDivisionError('float division by zero',)),
## Success(0.2),
## Failure(TypeError("unsupported operand type(s) for /: 'float' and 'str'",))]
tries.filter(lambda x: x.isSuccess).map(lambda x: x.get()).collect()
## [1.0, 0.5, 0.3333333333333333, 0.2]
您甚至可以使用multipledispatch
的模式匹配
from multipledispatch import dispatch
from operator import getitem
@dispatch(Success)
def check(x): return "Another great success"
@dispatch(Failure)
def check(x): return "What a failure"
a_list = [1, 2, 3]
check(Try(getitem, a_list, 1))
## 'Another great success'
check(Try(getitem, a_list, 10))
## 'What a failure'
如果你喜欢这种方法,我已经将更完整的实现推到了GitHub和pypi.