【问题标题】:No suitable driver found for jdbc:redshift:没有找到适合 jdbc:redshift 的驱动程序:
【发布时间】:2019-01-10 21:55:05
【问题描述】:

我正在尝试使用com.amazon.redshift.jdbc.Driver 连接到我的redshift 数据库,我编写了一个简单的类来连接到数据库并执行选择查询。我创建了一个简单的 gradle 项目,它创建了 fat jar。

当我在本地机器上运行它时,一切正常。但是当我将它部署到 AWS Lambda 中时,它给了我以下错误

{
"errorMessage": "No suitable driver found for jdbc:redshift://xyz/xyz",
"errorType": "java.sql.SQLException",
"stackTrace": [
    "java.sql.DriverManager.getConnection(DriverManager.java:689)",
    "java.sql.DriverManager.getConnection(DriverManager.java:208)",
    "test.DatabaseConnector.grab(DatabaseConnector.kt:204)",
"test.DatabaseConnector.generateHTMLReport(DatabaseConnector.kt:271)",
"test.DatabaseConnector.sendReport(DatabaseConnector.kt:606)",
"test.HelloWorldLambda.runReport(HelloWorldLambda.kt:15)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
   ]
   }

我不确定为什么同一个 jar 在本地而不是 AWS lambda 中有效。下面是代码

    Class.forName(driverName)
    val connection = DriverManager.getConnection(url, 
    Properties().apply {
        setProperty("user", username)
        setProperty("password", password)
    })
    val stmt = connection.createStatement()
    val rs = stmt.executeQuery(query)

    while (rs.next()) {
       println(rs.getString("name"))
    }

    rs.close()
    stmt.close()
    connection.close()

有人可以帮忙解决这个问题吗?

【问题讨论】:

    标签: jdbc aws-lambda amazon-redshift


    【解决方案1】:

    如果您使用 Maven,请确保使用 redshift-jdbc42-no-awssdk。另外,显然有一个错误,特别是 1.2.10.1009 版本,请看here

    唯一的工作版本似乎是“1.2.8.1005”,它是 Maven 存储库中最旧的可用版本。

    您需要做的另一件事是确保您的 jar 文件需要是一个所谓的“胖 jar”,其中包含应用程序的所有依赖项。

    你可以用maven-assembly-plugin作为包装生命周期来做一个胖罐子:

        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>Handler</mainClass>
                        </manifest>
                    </archive>
    
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

      【解决方案2】:

      我在 ECS 任务中收到此错误,驱动程序已捆绑在 jar 中,它是一个 spring boot 应用程序。该容器在开发机器上工作正常,但在 ECS 中失败。 @Ali Niaki 上面的回答证明很有帮助,我尝试更改版本号,但我没有降级,而是升级到了新版本。

      AWS 似乎不会使用发布版本更新文档。因此,请使用他们的存储库:http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release 从他们的文档中找到最新版本并改用它。在撰写本文时,在 ECS 中运行的最新版本是“redshift-jdbc42-1.2.18.1036”。 AWS redshift doc 仍然提到:'1.2.10.1009',这会导致问题。

      【讨论】:

      猜你喜欢
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 2011-10-21
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多