优化方式
以下是亲测的一些Spark调优方法并附上优化时间:
1.使用broadcast广播变量
使用broadcast广播变量,将HBase中的静态表数据做广播,可以大大提高效率,在我们的应用场景中,因为HBase中的静态数据较大,大概是6万多条静态路段信息,若不使用broadcast,会引发频繁GC。
1.1原理说明
在算子函数中使用到外部变量或两表join时,默认情况下,Spark会将该变量或小维表复制多个副本,通过网络传输到task中,此时每个task都有一个变量副本。如果变量本身比较大的话(比如100M,甚至1G),那么大量的变量副本在网络中传输的性能开销,以及在各个节点的Executor中占用过多内存导致的频繁GC,都会极大地影响性能。因为每个 task 是一个线程,而且同在一个进程运行的 tasks 都属于同一个 application。所以我们使用broadcast广播变量,使每个节点(executor)上放一份就可以被所有 task 共享。
贴上Spark官网上的一段话:
Broadcast variables allow the programmer to keep a read-only variable cached on each machine rather than shipping a copy of it with tasks. They can be used, for example, to give every node a copy of a large input dataset in an efficient manner. Spark also attempts to distribute broadcast variables using efficient broadcast algorithms to reduce communication cost.
为了避免维护数据一致性问题,Spark 目前只支持 br