spark sql - 是否使用行转换或 UDF [英] spark sql - whether to use row transformation or UDF

查看:24
本文介绍了spark sql - 是否使用行转换或 UDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 100 列和 1000 万条记录的输入表 (I).我想得到一个有 50 列的输出表 (O),这些列来自 I 的列,即有 50 个函数将 I 的列映射到 O 的 50 列,即 o1 = f(i1) , o2 = f(i2, i3) ..., o50 = f(i50, i60, i70).

I am having an input table (I) with 100 columns and 10 million records. I want to get an output table (O) that has 50 columns and these columns are derived from columns of I i.e. there will be 50 functions that map column(s) of I to 50 columns of O i.e. o1 = f(i1) , o2 = f(i2, i3) ..., o50 = f(i50, i60, i70).

在 spark sql 中,我可以通过两种方式做到这一点:

In spark sql I can do this in two ways:

  1. 逐行解析 I 的整行(例如:map 函数)以生成一行 O.
  2. 使用我认为在列级别工作的 UDF,即将 I 的现有列作为输入并生成 O 的相应列之一,即使用 50 个 UDF 函数.

我想知道以上 2 个中哪一个更有效(更高的分布式和并行处理)以及为什么或它们是否同样快速/高性能,因为我正在处理整个输入表我并生成全新的输出表 O,即批量数据处理.

I want to know which one of the above 2 is more efficient (higher distributed and parallel processing) and why or if they are equally fast/performant, given that I am processing entire input table I and producing entirely new output table O i.e. its a bulk data processing.

推荐答案

我打算写关于 Catalyst 优化器,但只需注意 Jacek Laskowski 在他的书 Mastering Apache Spark 2 中所说的内容就更简单了em>:

I was going to write this whole thing about the Catalyst optimizer, but it is simpler just to note what Jacek Laskowski says in his book Mastering Apache Spark 2:

"在恢复使用您自己的自定义 UDF 函数之前,尽可能将更高级别的标准基于列的函数与数据集运算符一起使用,因为 UDF 是 Spark 的黑盒,因此它甚至不会尝试优化它们."

Jacek 还注意到来自 Spark 开发团队的某个人的评论:

Jacek also notes a comment from someone on the Spark development team:

"在一些简单的情况下,我们可以分析 UDF 字节码并推断它在做什么,但一般来说很难做到."

这就是为什么 Spark UDF 永远不应成为您的首选.

This is why Spark UDFs should never be your first option.

同样的情绪在这个 Cloudera 中得到回应 post,其中作者指出...使用 Apache Spark 的内置 SQL 查询函数通常会带来最佳性能,并且应该是可以避免引入 UDF 时考虑的第一种方法."

That same sentiment is echoed in this Cloudera post, where the author states "...using Apache Spark’s built-in SQL query functions will often lead to the best performance and should be the first approach considered whenever introducing a UDF can be avoided."

然而,作者也正确地指出,随着 Spark 变得更智能,这可能会在未来发生变化,与此同时,您可以使用 Expression.genCode,如 Chris Fregly 的 谈谈,如果你不介意的话耦合到 Catalyst 优化器.

However, the author correctly notes also that this may change in the future as Spark gets smarter, and in the meantime, you can use Expression.genCode, as described in Chris Fregly’s talk, if you don't mind tightly coupling to the Catalyst optimizer.

这篇关于spark sql - 是否使用行转换或 UDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆