參考文檔: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);
}
}