-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature][Transform] data quality for null data rate
- Loading branch information
Showing
7 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
seatunnel-transforms/seatunnel-transforms-spark/seatunnel-transform-spark-null-rate/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
18 changes: 18 additions & 0 deletions
18
...l-rate/src/main/resources/META-INF/services/org.apache.seatunnel.spark.BaseSparkTransform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
95 changes: 95 additions & 0 deletions
95
...nsform-spark-null-rate/src/main/scala/org/apache/seatunnel/spark/transform/NullRate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
26 changes: 26 additions & 0 deletions
26
...-spark-null-rate/src/main/scala/org/apache/seatunnel/spark/transform/NullRateConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |