JDBC客户端连接Starrocks 2.5
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>TestStarrocksJdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <version>5.1.47</version>-->
<version>8.0.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.Test</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
private static Connection connection;
public Test() {
}
public static List<Map<String, Object>> starRocksRead(String sql) {
Long start = System.currentTimeMillis();
System.out.println("sql is: " + sql);
try {
List<Map<String, Object>> list = new ArrayList();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
ResultSetMetaData rsmd = resultSet.getMetaData();
int count = rsmd.getColumnCount();
while(resultSet.next()) {
Map<String, Object> map = new HashMap();
for(int i = 1; i <= count; ++i) {
String columnName = rsmd.getColumnName(i);
Object value = resultSet.getObject(i);
map.put(columnName, value);
}
list.add(map);
}
System.out.println("result is: " + list);
return list;
} catch (Exception var11) {
var11.printStackTrace();
Long end = System.currentTimeMillis();
System.out.println((end - start) / 60L);
return null;
}
}
public static void main(String[] args) {
try {
if (args.length != 5) {
System.out.println("未指定连接地址和sql,程序自动退出");
System.exit(1);
}
String ip = args[0];
Integer port = Integer.valueOf(args[1]);
String account = args[2];
String password = args[3];
String sql = args[4];
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException var7) {
throw new SQLException(var7);
}
String jdbcUrl = String.format("jdbc:mysql://%s:%s?connectTimeout=5000&socketTimeout=30000", ip, port);
connection = DriverManager.getConnection(jdbcUrl, account, password);
starRocksRead(sql);
if (connection != null) {
connection.close();
}
} catch (Exception var8) {
var8.printStackTrace();
}
}
}
sql=$(cat test.sql )
java -jar ~/TestStarrocksJdbc-1.0-SNAPSHOT-jar-with-dependencies.jar 127.0.0.1 9030 username password "$sql"