博客
关于我
【Spark】Spark 优化操作之自定义 distinct
阅读量:372 次
发布时间:2019-03-05

本文共 801 字,大约阅读时间需要 2 分钟。

由于Spark的distinct算子默认实现效率较低,需要自行优化以提升性能。

具体实现方式非常简单,主要基于集合的特性。

def mydistinct(iter: Iterator[(String, Int)]: Iterator[String] = {     iter.foldLeft(Set[String]())((curS, item) => curS + item._1).toIterator}

使用mydistinct的方式如下:

val rdd2 = rdd1.map(x => (x._1 + SPLIT + x._2 + SPLIT + x._3 + SPLIT + x._4, 1)).partitionBy(new org.apache.spark.HashPartitioner(100)).mapPartitions(SetProcess.mydistinct).map(key => {       val strs = key.split(SPLIT)       (strs(0), strs(1), strs(2), strs(3))

说明:

  • mydistinct通过Set的特性实现去重,在每个partition内完成后再进行reduce,这样可以显著提升去重效率。
  • 在进行mydistinct之前,需要先对数据进行partitionBy操作。因为数据的key值发生了变化,原有的RDD分区可能不适用于新的RDD。如果不做partitionBy,可能会导致不同的partition之间存在重复数据,从而影响最终的去重效果。
  • 通过partitionBy操作,可以将相同key值的数据刷新到同一个partition中。在每个partition内使用Set去重,大大提高了整体性能。
  • 这种方法充分利用了Spark的高效分区机制和集合的去重特性,实现了高效的去重操作。

    转载地址:http://xdig.baihongyu.com/

    你可能感兴趣的文章
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>