Skip to content

Commit

Permalink
[Feature][Transform] data quality for null data rate
Browse files Browse the repository at this point in the history
  • Loading branch information
wsyhj committed May 31, 2022
1 parent 76c2cb3 commit 93d73bc
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 0 deletions.
67 changes: 67 additions & 0 deletions docs/en/transform/nullRate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# EncryptedPhone

## Description

When there is a large amount of data, the final result will always be greatly affected by the problem of data null value. Therefore, early null value detection is particularly important. For this reason, this function came into being

:::tip

This transform **ONLY** supported by Spark.

:::

## Options

| name | type | required | default value |
| -------------------------| ------ | -------- | ------------- |
| fields | string | yes | - |
| rates | double | yes | - |
| throwException_enable | boolean| no | - |
| result_table_name | string | no | - |



### field [string]

Which fields do you want to monitor .

### rates [double]

It is consistent with the number of fields. Double type indicates the set null rate value .

### throwException_enable [boolean]

Whether to throw an exception when it is greater than the set value. The default value is false .

### result_table_name [string]

Whether the current verification value is output to the table. It is not output by defaul .

### common options [string]

Transform plugin common parameters, please refer to [Transform Plugin](common-options.mdx) for details

## Examples

```bash
nullRate {
fields = ["msg", "name"]
rates = [10.0,3.45]
result_table_name = "tmp"
throwException_enable = true
}
}
```

Use `NullRate` in transform's Dataset.

```bash
transform {
NullRate {
fields = ["msg", "name"]
rates = [10.0,3.45]
result_table_name = "tmp"
throwException_enable = true
}
}
```
6 changes: 6 additions & 0 deletions seatunnel-core/seatunnel-core-spark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
<artifactId>seatunnel-transform-spark-uuid</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.seatunnel</groupId>
<artifactId>seatunnel-transform-spark-null-rate</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions seatunnel-transforms/seatunnel-transforms-spark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<module>seatunnel-transform-spark-replace</module>
<module>seatunnel-transform-spark-uuid</module>
<module>seatunnel-transform-spark-sql</module>
<module>seatunnel-transform-spark-null-rate</module>
</modules>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.seatunnel</groupId>
<artifactId>seatunnel-transforms-spark</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>seatunnel-transform-spark-null-rate</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.seatunnel</groupId>
<artifactId>seatunnel-api-spark</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
</dependency>

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.binary.version}</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

org.apache.seatunnel.spark.transform.NullRate
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.spark.transform

import org.apache.seatunnel.apis.base.plugin.Plugin
import org.apache.seatunnel.common.config.CheckConfigUtil._
import org.apache.seatunnel.common.config.CheckResult
import org.apache.seatunnel.spark.{BaseSparkTransform, SparkEnvironment}
import org.apache.spark.sql.catalyst.encoders.RowEncoder
import org.apache.spark.sql.types.{DataTypes, StructType}
import org.apache.spark.sql.{Dataset, Row}

import scala.collection.JavaConversions._

class NullRate extends BaseSparkTransform {

override def process(df: Dataset[Row], env: SparkEnvironment): Dataset[Row] = {

val allCount = env.getSparkSession.sparkContext.longAccumulator("allCount")
val fieldsAndRates = config.getStringList(NullRateConfig.FIELDS).zip(config.getDoubleList(NullRateConfig.RATES)).filter(fl => df.schema.names.contains(fl._1)).toMap
val fieldsAndRatesAccumulator = fieldsAndRates.map(fl => {
fl._1 -> env.getSparkSession.sparkContext.longAccumulator(fl._1)
})

df.foreachPartition(iter => {
while (iter.hasNext) {
allCount.add(1L)
val row = iter.next()
fieldsAndRates.map(fl => fl._1).foreach(field => {
val accumulator = fieldsAndRatesAccumulator.get(field).get
if (row.get(row.fieldIndex(field)) == null) {
accumulator.add(1L)
} else {
accumulator.add(0L)
}
})
}
})

val allCountValue = allCount.value * 1.00d
val nullRateValue = fieldsAndRatesAccumulator.map(fl => {
(fl._1, fieldsAndRates.getOrDefault(fl._1, 100.00d), fl._2.value, (fl._2.value / allCountValue) * 100d)
})

if (config.hasPath(NullRateConfig.IS_THROWEXCEPTION) && config.getBoolean(NullRateConfig.IS_THROWEXCEPTION)) {
nullRateValue.foreach(fv => {
if (fv._4 > fv._2) {
throw new RuntimeException(s"the field(${fv._1}) null rate(${fv._4}) is lager then the setting(${fv._2})")
}
})
}

if (config.hasPath(Plugin.RESULT_TABLE_NAME)) {
val nullRateRows = nullRateValue.map(fv => {
Row(fv._1, fv._2, fv._3, fv._4)
}).toSeq

val schema = new StructType()
.add("field_name", DataTypes.StringType)
.add("setting_rate", DataTypes.LongType)
.add("null_rate", DataTypes.LongType)
.add("rate_percent", DataTypes.LongType)
env.getSparkSession.createDataset(nullRateRows)(RowEncoder(schema)).createOrReplaceTempView(config.getString(Plugin.RESULT_TABLE_NAME))
}

df
}

override def checkConfig(): CheckResult = {
val exists = checkAllExists(config, NullRateConfig.FIELDS, NullRateConfig.RATES)
val equal = if (config.getStringList(NullRateConfig.FIELDS).size() == config.getIntList(NullRateConfig.RATES).size()) CheckResult.success()
else CheckResult.error(s"the ${NullRateConfig.FIELDS} length is not equal ${NullRateConfig.RATES} length")
val unique = if (config.getStringList(NullRateConfig.FIELDS).toList.distinct.size() == config.getStringList(NullRateConfig.FIELDS).size()) CheckResult.success()
else CheckResult.error(s"the ${NullRateConfig.FIELDS} is not unique")
mergeCheckResults(exists, equal, unique)
}


override def getPluginName: String = "NullRate"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.spark.transform

object NullRateConfig {
def FIELDS = "fields"
def RATES = "rates"
def IS_THROWEXCEPTION = "throwException_enable"
}

0 comments on commit 93d73bc

Please sign in to comment.