[TD-3173]<feature>: java taosdemo can execute one sql statement by commandline
This commit is contained in:
parent
bfeb758b3a
commit
1734b1d197
|
@ -3,15 +3,17 @@ package com.taosdata.taosdemo;
|
|||
import com.taosdata.taosdemo.components.DataSourceFactory;
|
||||
import com.taosdata.taosdemo.components.JdbcTaosdemoConfig;
|
||||
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||
import com.taosdata.taosdemo.service.DatabaseService;
|
||||
import com.taosdata.taosdemo.service.QueryService;
|
||||
import com.taosdata.taosdemo.service.SubTableService;
|
||||
import com.taosdata.taosdemo.service.SuperTableService;
|
||||
import com.taosdata.taosdemo.service.*;
|
||||
import com.taosdata.taosdemo.service.data.SuperTableMetaGenerator;
|
||||
import com.taosdata.taosdemo.utils.Printer;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
|
@ -32,6 +34,17 @@ public class TaosDemoApplication {
|
|||
}
|
||||
// 初始化
|
||||
final DataSource dataSource = DataSourceFactory.getInstance(config.host, config.port, config.user, config.password);
|
||||
if (config.executeSql != null && !config.executeSql.isEmpty() && !config.executeSql.replaceAll("\\s", "").isEmpty()) {
|
||||
Thread task = new Thread(new SqlExecuteor(dataSource, config.executeSql));
|
||||
task.start();
|
||||
try {
|
||||
task.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final DatabaseService databaseService = new DatabaseService(dataSource);
|
||||
final SuperTableService superTableService = new SuperTableService(dataSource);
|
||||
final SubTableService subTableService = new SubTableService(dataSource);
|
||||
|
@ -96,7 +109,6 @@ public class TaosDemoApplication {
|
|||
// 查询
|
||||
|
||||
|
||||
|
||||
/**********************************************************************************/
|
||||
// 删除表
|
||||
if (config.dropTable) {
|
||||
|
|
|
@ -42,7 +42,7 @@ public final class JdbcTaosdemoConfig {
|
|||
public int rate = 10;
|
||||
public long range = 1000l;
|
||||
// select task
|
||||
|
||||
public String executeSql;
|
||||
// drop task
|
||||
public boolean dropTable = false;
|
||||
|
||||
|
@ -89,7 +89,7 @@ public final class JdbcTaosdemoConfig {
|
|||
System.out.println("-rate The proportion of data out of order. effective only if order is 1. min 0, max 100, default is 10");
|
||||
System.out.println("-range The range of data out of order. effective only if order is 1. default is 1000 ms");
|
||||
// query task
|
||||
// System.out.println("-sqlFile The select sql file");
|
||||
System.out.println("-executeSql execute a specific sql.");
|
||||
// drop task
|
||||
System.out.println("-dropTable Drop data before quit. Default is false");
|
||||
System.out.println("--help Give this help list");
|
||||
|
@ -207,6 +207,9 @@ public final class JdbcTaosdemoConfig {
|
|||
range = Integer.parseInt(args[++i]);
|
||||
}
|
||||
// select task
|
||||
if ("-executeSql".equals(args[i]) && i < args.length - 1) {
|
||||
executeSql = args[++i];
|
||||
}
|
||||
|
||||
// drop task
|
||||
if ("-dropTable".equals(args[i]) && i < args.length - 1) {
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package com.taosdata.taosdemo.service;
|
||||
|
||||
import com.taosdata.taosdemo.utils.Printer;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class SqlExecuteor implements Runnable {
|
||||
private final DataSource dataSource;
|
||||
private final String sql;
|
||||
|
||||
public SqlExecuteor(DataSource dataSource, String sql) {
|
||||
this.dataSource = dataSource;
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
boolean execute = stmt.execute(sql);
|
||||
long end = System.currentTimeMillis();
|
||||
if (execute) {
|
||||
ResultSet rs = stmt.getResultSet();
|
||||
Printer.printResult(rs);
|
||||
} else {
|
||||
Printer.printSql(sql, true, (end - start));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.taosdata.taosdemo.utils;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Printer {
|
||||
|
||||
public static void printResult(ResultSet resultSet) throws SQLException {
|
||||
ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
while (resultSet.next()) {
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
String columnLabel = metaData.getColumnLabel(i);
|
||||
String value = resultSet.getString(i);
|
||||
System.out.printf("%s: %s\t", columnLabel, value);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void printSql(String sql, boolean succeed, long cost) {
|
||||
System.out.println("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
|
||||
}
|
||||
|
||||
private Printer() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue