【问题标题】:How to debug the function passed to mapPartitions如何调试传递给 mapPartitions 的函数
【发布时间】:2017-12-01 05:53:11
【问题描述】:

我如何处理我无法在传递给 pyspark 中的 mapPartitions() 的函数中使用 print 语句调试代码的问题?

考虑这个例子:

def func(kv_iterator):
    for key, value in iterator:
        #do fancy stuff
        print('This print statement does not reach the driver program')
    return [result]

result = someRdd.mapPartitions(func)

在 func 内部,我想在可迭代和索引方面做很多工作,但我可以测试我的代码,而不会过多地使用 func 内部的变量。

是否有可能以某种方式将打印语句从一个分区重定向到我的驱动程序/输出通道?

【问题讨论】:

    标签: apache-spark mapreduce pyspark partitioning


    【解决方案1】:

    您可以使用以下方法之一:

    • 使用local 模式。所有输出都应该在控制台中可见。如果不是,您的代码可能永远不会执行 - 尝试 result.count()result.foreach(lambda _: None) 或其他操作 - 这可能就是问题所在。
    • 重定向标准输出(和标准错误,如果你想)到文件。对于基本的prints 使用file 参数:

      print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
      
    • 使用远程调试器 - How can pyspark be called in debug mode?

    但最重要的是 - 在 Spark 之外测试功能。与mapPartitions一起使用的函数应该接受Iterable(具体实现通常是itertools.chain)并返回Iterable

    【讨论】:

    • 只是一些小事,因为它不会使您的答案无效:映射函数的返回类型不需要是可迭代的;这不是强制性的。例如,你可以在你的函数中处理一个 numpy-array 并返回整个东西,它甚至不必有行。唯一需要注意的是如何处理映射后的返回值:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 2013-02-07
    • 2018-07-13
    • 2020-10-20
    相关资源
    最近更新 更多