【问题标题】:Invalid operation: relation "information_schema.sequences" does not exist (SpringBoot + RedShift )无效操作:关系“information_schema.sequences”不存在(SpringBoot + RedShift)
【发布时间】:2019-11-19 08:59:41
【问题描述】:

我正在尝试在我的 Spring Boot 应用程序中建立与 Redshift 数据库的连接。我的属性文件中有以下条目。

spring.datasource.driver-class-name=com.amazon.redshift.jdbc41.Driver
spring.datasource.url=jdbc:redshift://redshift_url/db_name
spring.datasource.username=username
spring.datasource.password=password

spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect =  org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation = true 
spring.jpa.properties.hibernate.current_session_context_class =org.springframework.orm.hibernate5.SpringSessionContext

在 pom.xml 我有以下条目。

<dependency>
  <groupId>com.amazon.redshift</groupId>
  <artifactId>redshift-jdbc41</artifactId>
  <version>1.2.10.1009</version>
</dependency>

我已经创建了一个如下所示的 POJO 文件。

@Entity
@Table(name = "some_data_table")
public class SomeDataTable {

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private long id;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false)
private Date createdAt;

@Temporal(TemporalType.TIMESTAMP)
@Column(name="date",nullable = false)
private Date date;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
private Date updatedAt;

@Column(name = "data_source", nullable = false)
private String dataSource;

 Getters and Setters...
}

但是每当我启动应用程序时,我都会遇到错误。

Caused by: java.sql.SQLException: [Amazon](500310) Invalid operation: relation "information_schema.sequences" does not exist;
    at com.amazon.redshift.client.messages.inbound.ErrorResponse.toErrorException(Unknown Source) ~[na:na]
at com.amazon.redshift.client.PGMessagingContext.handleErrorResponse(Unknown Source) ~[na:na]
at com.amazon.redshift.client.PGMessagingContext.handleMessage(Unknown Source) ~[na:na]
at com.amazon.jdbc.communications.InboundMessagesPipeline.getNextMessageOfClass(Unknown Source) ~[na:na]
at com.amazon.redshift.client.PGMessagingContext.doMoveToNextClass(Unknown Source) ~[na:na]

请指导我解决此错误。

【问题讨论】:

    标签: hibernate spring-boot spring-data-jpa amazon-redshift persistence


    【解决方案1】:

    不要将 GenerationType.TABLE 用于您的 Redshift DWH,因为它会根据 PostgreSQL9Dialect 查询 information_schema.sequences 表,由于缺少对序列的支持,AWS Redshift 不支持该表。如您所见,使用GenerationType.TABLE 时执行的第一个查询将是:

    SELECT tbl.next_val
    FROM hibernate_sequences tbl
    ...
    

    您可以尝试使用 GenerationType.IDENTITY see Redshift CREATE TABLE doc 代替您的 id(见下文),或者干脆不使用 GeneratedValues,因为 Redshift 不会强制执行主键...

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    

    【讨论】:

      【解决方案2】:

      就像@aleksandar 提到的,表 information_schema.sequences 在 Redshift 中不存在。

      要跳过 information_schema.sequences,请创建覆盖现有 PostgreSqlDialect 的自定义方言

      import org.hibernate.dialect.PostgreSQL9Dialect;
      
      public class MyDialect extends PostgreSQL9Dialect {
      
          @Override
          public String getQuerySequencesString() {
              // Takes care of ERROR: relation “information_schema.sequences” does not exist
              return null;
          }
      }
      

      然后将hibernate.dialect设置为这个自定义的方言类

      spring.jpa.properties.hibernate.dialect =  <path to myDialect class>
      

      【讨论】:

        【解决方案3】:

        正如错误指出的那样,表 information_schema.sequences 在 Redshift 中不存在。 Redshift虽然是基于PostgreSQL的,但是还是有很多区别的(https://docs.aws.amazon.com/redshift/latest/dg/c_redshift-and-postgres-sql.html)

        但是,Redshift 专门设计为分析数据库,针对在大量数据上运行 SELECT 语句进行了优化,它不喜欢大量写入/更新,这使得它不适合 Spring Boot 应用程序。我建议您为您的应用程序选择另一种数据库类型,如果您使用 AWS,您可以使用例如RDS

        【讨论】:

        • 我没有更改数据库的选项。
        猜你喜欢
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 2019-02-23
        相关资源
        最近更新 更多