LongAccumulator源码分析

博客提及查看accumulate方法,与信息技术相关,涉及代码操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们查看下这里的accumulate方法

 

 

 

 

解释这段代码 def dropNullAndDropDuplicates(spark: SparkSession, df: DataFrame, schema: StructType, dropKeys: Seq[String], duplicateKeys: Array[String]): (LongAccumulator, LongAccumulator, LongAccumulator, DataFrame) = { val schemaFieldNames: Array[String] = schema.fieldNames if (dropKeys.exists(!schemaFieldNames.contains(_)) || duplicateKeys.exists(!schemaFieldNames.contains(_))) { return (null, null, null, null) } val lineCount: LongAccumulator = spark.sparkContext.longAccumulator("lineCount") val trash: LongAccumulator = spark.sparkContext.longAccumulator("trash") val duplicate: LongAccumulator = spark.sparkContext.longAccumulator("duplicate") val df1: DataFrame = df.select( df.columns.map(name => col(name).as(name.trim.toLowerCase)): _* ) val df1FieldNames: Array[String] = df1.schema.fieldNames val df2: DataFrame = { var tmp: DataFrame = df1 schema.fieldNames.filterNot(df1FieldNames.contains).foreach( fieldName => tmp = tmp.withColumn(fieldName, lit(literal = null)) ) tmp.select( schema.fields .map(structField => tmp.col(structField.name).cast(structField.dataType)): _* ) }.withColumn(colName = "index", monotonically_increasing_id()) val df3: DataFrame = df2.filter(row => { lineCount.add(1) if (dropKeys.exists(key => row.get(row.fieldIndex(key)) == null)) { trash.add(1) false } else { true } }) val df4: DataFrame = df3.groupByKey(row => duplicateKeys.map(key => row.get(row.fieldIndex(key)).toString).mkString("-") )(Encoders.STRING).reduceGroups((row1, row2) => { duplicate.add(1) val defect1 = row1.toSeq.count(_ == null) val defect2 = row2.toSeq.count(_ == null) if (defect1 < defect2) row1 else if (defect1 > defect2) row2 else if (row1.getLong(row1.fieldIndex(name = "index")) > row2.getLong(row1.fieldIndex(name = "index"))) row1 else row2 }).map(_._2)(RowEncoder(df3.schema)) .toDF .drop("index") (lineCount, trash, duplicate, df4) }
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值