数据质量校验简单设计思路

参考文档:https://www.jianshu.com/p/2697687407a8

一些数据计算完毕后,我们需要知道计算结果是否符合预期,比如是否有脏数据,是否数据量符合预期。这里就有两个问题,一个是校验什么,另一个是怎么校验。

校验什么

- 单个字段校验,例如字段的唯一性、非空、数值范围、枚举类型、异常值
- 表级行数,例如表的行数在一个合理范围,可以是跟历史行数对比,也可以跟预设的常量做对比
- 业务逻辑校验,包括了表内、表间的比较

怎么执行校验

Hive表的校验自然是跑SQL最方便,每当Hive表更新完要执行SQL校验,需要考虑两个问题

- 资源消耗。最基本的采集表行数,再加上额外的校验规则,不应该占用太多计算资源,以免因为校验而拖慢集群整体性能
- 校验延迟。如果是大表,或者是复杂规则,马上获取到校验结果是比较困难的。当校验与调度打通,应当认为关键规则必须校验通过,这种关键校验就不能等待太久。还有一种交互式的校验场景,用户配置完规则后,需要立即测试查看执行情况。

基于上述考虑,就要选择既省资源,又快的计算引擎,MapReduce可直接排除,备选方案是Spark SQL和Presto。
回过头看校验要执行的SQL,大部分是简单SQL,例如count(), min/max等,Presto在这种简单查询展示出了非常优异的性能,无论消耗资源,还是执行时间都很好。

presto安装部署

https://blog.csdn.net/weixin_41008393/article/details/90269228
http://prestodb.jd.com/docs/current/installation/deployment.html

jdbc访问presto

1、maven依赖
Driver为com.facebook.presto.jdbc.PrestoDriver

<dependency>
    <groupId>com.facebook.presto</groupId>
    <artifactId>presto-jdbc</artifactId>
    <version>0.191</version>
</dependency>

2、jdbc工具类

package com.zhbr.dataImport.qualities;

import java.sql.*;

/**
 * @ClassName PrestoUtil
 * @Description TODO
 * @Autor yanni
 * @Date 2020/4/16 14:12
 * @Version 1.0
 **/
public class PrestoUtil {

    //获得连接
    private String url;
    private String username;
    private String password;

    public PrestoUtil(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    {
        //注册驱动
        try {
            Class.forName("com.facebook.presto.jdbc.PrestoDriver");
        } catch (ClassNotFoundException e) {
            throw  new RuntimeException(e);
        }

    }
    public Connection getConnection(){
        Connection con = null;
        try {
            con = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    public void close(ResultSet rs, Statement sta, Connection con){
        try {
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (sta!=null){
                sta.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (con!=null){
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

3、校验方法

package com.zhbr.dataImport.qualities;

import com.zhbr.dataImport.utils.ConfigsUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @ClassName CheckDataUtil
 * @Description TODO
 * @Autor yanni
 * @Date 2020/4/16 14:26
 * @Version 1.0
 **/
public class CheckDataUtil {

    public static String run(String dataBaseAndTable,String userName,String password,String Sql){
        String str = "";
        PrestoUtil root = new PrestoUtil("jdbc:presto://node01:8083/" +dataBaseAndTable, userName, password);
        Connection connection = root.getConnection();
        Statement statement = null;
        try {
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(Sql);
            while(resultSet.next()) {
                str = resultSet.getString(1);
                return str;
            }
            resultSet.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return str;
    }
}

4、测试

package com.zhbr.dataImport.qualities;

import java.sql.*;

/**
 * @ClassName Test
 * @Description TODO
 * @Autor yanni
 * @Date 2020/4/16 14:00
 * @Version 1.0
 **/
public class Test {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        String s = CheckDataUtil.run("hive/default", "root", null, "select count(*) from lsb where P0 is null");
        System.out.println(s);
        System.out.println("---------------------------");

        String root = CheckDataUtil.run("mysql/test11", "root", null, "select max(P0) from lsb");
        System.out.println(root);
    }
}

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章