15

我正在尝试使用 spring boot jpa datasource 来使用 AWS athena。我尝试设置具有给定属性的数据源。

    spring.datasource.driver-class-name=com.amazonaws.athena.jdbc.AthenaDriver
    spring.datasource.url=jdbc:awsathena://athena.us-east-1.amazonaws.com:443/default
    spring.datasource.username=*****
    spring.datasource.password=***
    spring.datasource.tomcat.connectionProperties=s3_staging_dir=*****

我低于异常

    Caused by:org.springframework.beans.BeanInstantiationException:    Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(Constructo`enter code here`rResolver.java:588) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 51 common frames omitted
    Caused by: java.lang.IllegalArgumentException: URL must start with 'jdbc'
at org.springframework.util.Assert.isTrue(Assert.java:92) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.jdbc.DatabaseDriver.fromJdbcUrl(DatabaseDriver.java:268) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup.getDatabase(DatabaseLookup.java:73) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties.java:139) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter(JpaBaseConfiguration.java:105) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$720f8624.CGLIB$jpaVendorAdapter$4(<generated>) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$720f8624$$FastClassBySpringCGLIB$$9766cf.invoke(<generated>) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
4

3 回答 3

1

作为对此处提供的答案的回应(由于声誉问题,我无法发表评论)并且还因为没有在线示例,我将提供我如何设法配置spring boot(2.2.5)数据 JPA 以使用 Athena JDBC 驱动程序。

Athena jdbc 连接 url 有格式
jdbc:awsathena://athena.[Region].amazonaws.com:443;User= [AccessKey];Password=[SecretKey];S3OutputLocation=[Output];

我的 applications.properties 文件
spring.datasource.url=specifiedformat
spring.datasource.driver-class-name=com.simba.athena.jdbc42.Driver
spring.datasource.hikari.connection-test-query=SELECT 1
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=validate

可以在此处找到 Athena 的 JDBC 驱动程序
https://docs.aws.amazon.com/athena/latest/ug/connect-with-jdbc.html
或者如果您希望使用 maven 存储库
https://mvnrepository.com/artifact /com.syncron.amazonaws/simba-athena-jdbc-driver/2.0.2

我使用 MySQLDialect 是因为它类似于 Athena 使用的 ANSI SQL(目前没有 Athena 的方言)。当然因为 MySQL 的方言和 Athena 不支持准备好的语句(?实际上只有那些有占位符的,你可以用它String.format()来绕过这个,参考:AWS Athena JDBC PreparedStatement),你将无法执行所有的 spring boot data jpa 默认实现的操作,但是您可以通过在存储库类中使用@Query注释并String.format()绕过不受支持的准备好的语句来绕过它。

Spring boot + JPA + Hibernate + Athena JDBC 驱动程序对我来说工作得很好(我目前只读取数据),唯一的问题是你必须测试你的每个查询,如果它在这个配置中受支持(因为它们可以为不受支持的操作抛出异常)。

于 2020-04-03T15:06:54.177 回答
1

默认情况下,Spring boot 将根据从 JDBC 驱动程序检索到的 Datasource 元数据自动检测要使用的Hibernate Dialect 。

此错误是由于 Athena JDBC 驱动程序无法获取相关元数据。不知道为什么会失败,但是您可以绕过此检测并通过声明JpaVendorAdapterbean 来明确声明要自己使用的 Hibernate Dialect:

@Bean
public JpaVendorAdapter jpaVendorAdapter(JpaProperties properties) {
    AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.setShowSql(properties.isShowSql());
    adapter.setGenerateDdl(properties.isGenerateDdl());
    return adapter;
}

我保留所有默认行为,但只是禁用自动检测方言

并定义方言application.properties

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.XXXXXX

但关键是我怀疑 Athena 是否可以与 Hibernate 一起使用,因为我找不到 Athena 的现有方言。所以我的建议是:

  1. 从另一个数据库尝试方言,其 SQL 语法类似于 Athena。在某些情况下它不起作用也就不足为奇了。

  2. 自己实现 Athena 的方言。

于 2019-08-23T03:46:01.707 回答
0

AWS Athena 文档声明确保端口 444 对出站流量开放。根据此处的连接文档https://docs.aws.amazon.com/athena/latest/ug/connect-with-jdbc.html

不确定这是否是正确的解决方案,所以我挖了一下,因为这个问题太老了。

于 2019-08-22T16:39:54.913 回答