java - Apache Spark 让 SQL 查询更快?

标签 java apache-spark apache-spark-sql

来自apache-spark-makes-slow-mysql-queries-10x-faster

For long running (i.e., reporting or BI) queries, it can be much faster as Spark is a massively parallel system. MySQL can only use one CPU core per query, whereas Spark can use all cores on all cluster nodes. In my examples below, MySQL queries are executed inside Spark and run 5-10 times faster (on top of the same MySQL data).

它看起来很棒,但我无法想到查询的实际示例,其中查询可以划分为子查询,并且多核货车使其速度更快 在一个核心上运行它?

最佳答案

假设我们有两个表“客户”和“订单”,每个表都有 1 亿条记录。

现在我们必须在 Customer 和 Order 表中的 customer_id 列上连接这两个表来生成报告,MySQL 几乎不可能做到这一点,因为单个系统必须执行此操作加入大量数据。

在 Spark 集群上,我们可以根据连接列重新分区这些表。现在通过散列 customer_id 来分发两个数据帧的数据。因此,这意味着订单和客户表都具有 Spark 同一工作节点中单个客户的所有数据,并且可以执行本地联接,如下面的代码片段所示。

val customerDf = //
val orderDf = //
val df1 = customerDf.repartition($"customer_id")
val df2 = orderDf.repartition($"customer_id")
val result df1.join(df2).on(df1("customer_id") == df2("customer_id")) 

因此,这 1 亿条记录连接现在是在数十或数百个工作节点上并行执行的,而不是像 MySQL 那样在单个节点中完成。

关于java - Apache Spark 让 SQL 查询更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459346/

相关文章:

java - 错误 : 'Please fix the version conflict either by updating the version....'

java - 程序运行时创建对象

python - 分组 PySpark DataFrame 后如何应用描述函数?

jvm - 由于内存不足,Spark Join 失败

scala - Spark 输出到 kafka 恰好一次

java - 衡量花费时间和内存的框架

java - 使用 SSL 时 REST 响应超过 8KB 错误

pyspark - 使用 Pyspark 的 HIVE JDBC 连接将列名称返回为行值

apache-spark - Apache Sqoop 和 Spark

apache-spark - 使用 Spark SQL 查询 Hive 分区中子目录中的数据