Skip to content

Commit

Permalink
update yml14
Browse files Browse the repository at this point in the history
Signed-off-by: yuanf <yuanf@dmetasoul.com>
  • Loading branch information
yuanf committed Aug 30, 2023
1 parent ed1c118 commit 4294466
Showing 1 changed file with 95 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

package com.facebook.presto.benchmark;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

public class Benchmark {
static String hostname = "mysql";
Expand Down Expand Up @@ -73,6 +72,7 @@ public static void main(String[] args) throws Exception {
mysqlCon.setSchema(lakeSoulDBName);
prestoCon.setSchema(lakeSoulDBName);
System.out.println(splitLine);
System.out.println("verifing single tableName" + lakeSoulDBName + "." + lakeSoulTableName);
verifyQuery(lakeSoulDBName, lakeSoulTableName);
System.out.println(splitLine);
}
Expand All @@ -85,7 +85,7 @@ public static void main(String[] args) throws Exception {
while (tablesResults.next()){
String tableName = tablesResults.getString(1);
System.out.println(splitLine);
System.out.println("verifing tableName = " + tableName);
System.out.println("verifing tableName = " + dbName + "." + tableName);
verifyQuery(dbName, tableName);
}
}
Expand Down Expand Up @@ -129,14 +129,21 @@ public static void verifyQuery (String schema, String table) throws SQLException
String sql2 = String.format("select count(*) from mysql.%s", schemaTableName) ;
String sql3 = String.format("select count(*) from (select * from lakesoul.%s except select * from mysql.%s)", schemaTableName, schemaTableName);
String sql4 = String.format("select count(*) from (select * from mysql.%s except select * from lakesoul.%s)", schemaTableName, schemaTableName);
int count1 = getCount(prestoCon.prepareStatement(sql1).executeQuery());
int count2 = getCount(prestoCon.prepareStatement(sql2).executeQuery());
int count3 = getCount(prestoCon.prepareStatement(sql3).executeQuery());
int count4 = getCount(prestoCon.prepareStatement(sql4).executeQuery());
if(count1 == 0 || count2 == 0 || count3 != 0 || count4 != 0){
throw new RuntimeException("table " + table + " is not matched");
String sql5 = String.format("select * from lakesoul.%s", schemaTableName) ;
String sql6 = String.format("select * from mysql.%s", schemaTableName) ;
try {
int count1 = getCount(prestoCon.prepareStatement(sql1).executeQuery());
int count2 = getCount(prestoCon.prepareStatement(sql2).executeQuery());
int count3 = getCount(prestoCon.prepareStatement(sql3).executeQuery());
int count4 = getCount(prestoCon.prepareStatement(sql4).executeQuery());
if(count1 == 0 || count2 == 0 || count3 != 0 || count4 != 0){
throw new RuntimeException("table " + table + " is not matched");
}
System.out.println("table " + table + " matched");
}catch (Exception e){
ResultSetPrinter.printResultSet(prestoCon.prepareStatement(sql5).executeQuery());
ResultSetPrinter.printResultSet(prestoCon.prepareStatement(sql6).executeQuery());
}
System.out.println("table " + table + " matched");
}

static int getCount(ResultSet res) throws SQLException {
Expand All @@ -159,4 +166,80 @@ static int getCount(ResultSet res) throws SQLException {
}


/**
* 结果集打印机.将结果集中的数据打印成表格.
*/
class ResultSetPrinter {
public static void printResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
// 获取列数
int ColumnCount = resultSetMetaData.getColumnCount();
// 保存当前列最大长度的数组
int[] columnMaxLengths = new int[ColumnCount];
// 缓存结果集,结果集可能有序,所以用ArrayList保存变得打乱顺序.
ArrayList<String[]> results = new ArrayList<>();
// 按行遍历
while (rs.next()) {
// 保存当前行所有列
String[] columnStr = new String[ColumnCount];
// 获取属性值.
for (int i = 0; i < ColumnCount; i++) {
// 获取一列
columnStr[i] = rs.getString(i + 1);
// 计算当前列的最大长度
columnMaxLengths[i] = Math.max(columnMaxLengths[i], (columnStr[i] == null) ? 0 : columnStr[i].length());
}
// 缓存这一行.
results.add(columnStr);
}
printSeparator(columnMaxLengths);
printColumnName(resultSetMetaData, columnMaxLengths);
printSeparator(columnMaxLengths);
// 遍历集合输出结果
Iterator<String[]> iterator = results.iterator();
String[] columnStr;
while (iterator.hasNext()) {
columnStr = iterator.next();
for (int i = 0; i < ColumnCount; i++) {
// System.out.printf("|%" + (columnMaxLengths[i] + 1) + "s", columnStr[i]);
System.out.printf("|%" + columnMaxLengths[i] + "s", columnStr[i]);
}
System.out.println("|");
}
printSeparator(columnMaxLengths);
}

/**
* 输出列名.
*
* @param resultSetMetaData 结果集的元数据对象.
* @param columnMaxLengths 每一列最大长度的字符串的长度.
* @throws SQLException
*/
private static void printColumnName(ResultSetMetaData resultSetMetaData, int[] columnMaxLengths) throws SQLException {
int columnCount = resultSetMetaData.getColumnCount();
for (int i = 0; i < columnCount; i++) {
// System.out.printf("|%" + (columnMaxLengths[i] + 1) + "s", resultSetMetaData.getColumnName(i + 1));
System.out.printf("|%" + columnMaxLengths[i] + "s", resultSetMetaData.getColumnName(i + 1));
}
System.out.println("|");
}

/**
* 输出分隔符.
*
* @param columnMaxLengths 保存结果集中每一列的最长的字符串的长度.
*/
private static void printSeparator(int[] columnMaxLengths) {
for (int i = 0; i < columnMaxLengths.length; i++) {
System.out.print("+");
// for (int j = 0; j < columnMaxLengths[i] + 1; j++) {
for (int j = 0; j < columnMaxLengths[i]; j++) {
System.out.print("-");
}
}
System.out.println("+");
}

}

0 comments on commit 4294466

Please sign in to comment.