如何使用JDBC连接CnosDB

问题描述 投票:0回答:0

我按照官网文档使用JDBC连接CnosDB。

看来我无法编译该项目。

这是我的项目结构:

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── org
    │   │       └── example
    │   │           └── Main.java
    │   └── resources
    └── test
        └── java

pom.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>arrow-flight-jdbc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!-- https://mvnrepository.com/artifact/org.apache.arrow/arrow-jdbc -->
    <dependencies>
        <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.arrow</groupId>
            <artifactId>arrow-jdbc</artifactId>
            <version>11.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.arrow/flight-sql-jdbc-driver -->
        <dependency>
            <groupId>org.apache.arrow</groupId>
            <artifactId>flight-sql-jdbc-driver</artifactId>
            <version>11.0.0</version>
        </dependency>
    </dependencies>
</project>

Main.java

package org.example;

import java.sql.*;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        final Properties properties = new Properties();
        properties.put("user", "root");
        properties.put("password", "");
        properties.put("tenant", "cnosdb");
        properties.put("useEncryption", false);
        properties.put("database", "public");
        try (
                Connection connection = DriverManager.getConnection(
                "jdbc:arrow-flight-sql://127.0.0.1:8904", properties
                );
                Statement statement = connection.createStatement())
        {
            statement.execute("CREATE TABLE IF NOT EXISTS air\n" +
                    "(\n" +
                    "    visibility  DOUBLE,\n" +
                    "    temperature DOUBLE,\n" +
                    "    pressure    DOUBLE,\n" +
                    "    TAGS(station)\n" +
                    ");");
            statement.executeUpdate("INSERT INTO air (time, station, visibility, temperature, pressure) VALUES\n" +
                    "    (1666165200290401000, 'XiaoMaiDao', 56, 69, 77);");
            ResultSet resultSet = statement.executeQuery("select * from air limit 1;");

            while (resultSet.next()) {
                Timestamp column1 = resultSet.getTimestamp(1);
                String column2 = resultSet.getString(2);
                Double column3 = resultSet.getDouble(3);
                Double column4 = resultSet.getDouble(4);
                Double column5 = resultSet.getDouble(5);
                System.out.printf("%s %s %f %f %f\n", column1, column2, column3, column4, column5);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

我尝试运行以下命令

_JAVA_OPTIONS="--add-opens=java.base/java.nio=ALL-UNNAMED" mvn exec:java -Dexec.mainClass="org.example.Main"

但是抛出以下错误

[警告] java.lang.ClassNotFoundException: org.example.Main 在 org.codehaus.mojo.exec.URLClassLoaderBuilder$ExecJavaClassLoader.loadClass (URLClassLoaderBuilder.java:198) 在 java.lang.ClassLoader.loadClass (ClassLoader.java:521) 在 org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:271) 在 java.lang.Thread.run (Thread.java:1623) [信息] ---------------------------------------------------------- ------------------------ [信息] 构建失败 [信息] ---------------------------------------------------------- ------------------------ [信息] 总时间:0.557 秒 [信息] 完成于:2023-07-07T10:08:51+08:00 [信息] ---------------------------------------------------------- ------------------------ [错误] 无法在项目 arrow-flight-jdbc 上执行目标 org.codehaus.mojo:exec-maven-plugin:3.1.0:java (default-cli):执行 Java 类时发生异常。 org.example.Main -> [帮助 1] [错误] [错误] 要查看错误的完整堆栈跟踪,请使用 -e 开关重新运行 Maven。 [错误] 使用 -X 开关重新运行 Maven 以启用完整的调试日志记录。 [错误] [错误] 有关错误和可能的解决方案的更多信息,请阅读以下文章: [错误] [帮助 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

如何连接到CnosDB?

java jdbc cnosdb
© www.soinside.com 2019 - 2024. All rights reserved.