JDBC客户端连接Starrocks 2.5

XML 复制代码
<?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>
java 复制代码
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"

相关推荐
sdaxue.com1 小时前
帝国CMS:如何去掉帝国CMS登录界面的认证码登录
数据库·github·网站·帝国cms·认证码
o(╥﹏╥)2 小时前
linux(ubuntu )卡死怎么强制重启
linux·数据库·ubuntu·系统安全
阿里嘎多学长2 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
Yuan_o_2 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端
Sunyanhui12 小时前
牛客网 SQL36查找后排序
数据库·sql·mysql
老王笔记2 小时前
MHA binlog server
数据库·mysql
lovelin+v175030409663 小时前
安全性升级:API接口在零信任架构下的安全防护策略
大数据·数据库·人工智能·爬虫·数据分析
DT辰白4 小时前
基于Redis的网关鉴权方案与性能优化
数据库·redis·缓存
2401_871213304 小时前
mysql高阶语句
数据库·mysql