spark优化之编程方式汇总

spark优化有两个方向,一是写好的代码,二是合理配置资源。本文讲述的是第一种思路,内容来源于Spark Performance Tuning & Best Practices,sparkbyexample是个很好的网站,除了是全英文,没有缺点。

以下为整理的spark程序优化思路:

  1. 使用DataFrame而不是RDD。这得益于钨丝计划项目和catalyst优化器的优化。钨丝计划能提高spark任务的内存和cpu效率,catalyst优化器是个整合的sql查询优化器。

  2. 使用coalesce()而不是repartition()。缩小分区是一般使用coalesce(),因为该方法默认没有shuffle操作,除非你想增加分区那只能用repartition,该方法有shuffle操作。

  3. 使用mapPartitions() 替代map()

  4. 使用序列化数据格式。比如Avro,kryo,Parquet格式而不是text,CSV,JSON格式。

    Most of the Spark jobs run as a pipeline where one Spark job writes data into a File and another Spark jobs read the data, process it, and writes to another file for another Spark job to pick up. When you have such use case, prefer writing an intermediate file in Serialized and optimized formats like Avro, Kryo, Parquet e.t.c, any transformations on these formats performs better than text, CSV, and JSON.

  5. 尽量避免使用UDF(user defined functions),UDF对于spark是个黑盒,spark没有办法对UDF做优化,所以函数在sparksql中有内置就不要用udf。

  6. 在内存中持久化(persisting)和缓存(cache)数据

    spark的cache(),persist(),unpersist()方法及需要注意的细节

  7. 减少昂贵的shuffle操作

    Spark Shuffle is an expensive operation since it involves the following

    • Disk I/O
    • Involves data serialization and deserialization
    • Network I/O
  8. Disable DEBUG & INFO Logging

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值