使用 Redshift 设置的 QueryDSL

Posted

技术标签:

【中文标题】使用 Redshift 设置的 QueryDSL【英文标题】:QueryDSL with Redshift setup 【发布时间】:2016-05-09 05:37:16 【问题描述】:

我正在尝试将 QueryDSL 添加到使用 Amazon Redshift 的项目中。我不确定 QueryDSL 是否支持 Redshift,我可能在这里做错了很多。我在 Q 类中遇到编译错误,请参阅下面的相关摘录:

TABLE DDL (REDSHIFT)(注意 users_follow 表缺少 id 列和二进制主键)

CREATE TABLE users_follow (
    user_id1 INT8 NOT NULL,
    user_id2 INT8 NOT NULL,
    active INT2 NOT NULL,
    created_at TIMESTAMP NOT NULL,
    modified_at TIMESTAMP NOT NULL,
    PRIMARY KEY (user_id1, user_id2),
    FOREIGN KEY (user_id1) REFERENCES users (id),
    FOREIGN KEY (user_id2) REFERENCES users (id)
)

CREATE TABLE users (
    id INT8 NOT NULL,
    username VARCHAR(255),
    PRIMARY KEY (id)
)

POM 依赖部分

    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-sql</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-sql-codegen</artifactId>
        <version>4.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-sql-spring</artifactId>
        <version>4.1.0</version>
    </dependency>

POM 插件部分

        <plugin>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-maven-plugin</artifactId>
            <version>4.1.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>export</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jdbcDriver>com.amazon.redshift.core.jdbc41.PGJDBC41Driver</jdbcDriver>
                <jdbcUrl>jdbc:redshift://aUrl.com:5439/aSchema</jdbcUrl>
                <jdbcPassword>aPassword</jdbcPassword>
                <jdbcUser>aUser</jdbcUser>
                <packageName>com.myproject.domain</packageName>
                <targetFolder>$project.basedir/target/generated-sources/java</targetFolder>
                <sourceFolder>$project.basedir/target/generated-sources/java</sourceFolder>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.amazonaws</groupId>
                    <artifactId>redshift</artifactId>
                    <version>$redshift.version</version>
                </dependency>
            </dependencies>
        </plugin>

从 mvn clean install -Dmaven.test.skip=true -e -X 输出

...

[INFO] Exported users_follow successfully

...

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] path/target/generated-sources/java/com/myproject/domain/QUsersFollow.java:[43,109] cannot find symbol
  symbol:   variable id
  location: class com.myproject.domain.QUsersFollow
[ERROR] path/target/generated-sources/java/com/myproject/domain/QUsersFollow.java:[45,109] cannot find symbol
  symbol:   variable id
  location: class com.myproject.domain.QUsersFollow
[INFO] 18 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.210 s
[INFO] Finished at: 2016-05-09T04:00:53+00:00
[INFO] Final Memory: 40M/373M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project analytics-service: Compilation failure: Compilation failure:
[ERROR] path/target/generated-sources/java/com/myproject/domain/QUsersFollow.java:[43,109] cannot find symbol
[ERROR] symbol:   variable id
[ERROR] location: class com.myproject.domain.QUsersFollow
[ERROR] path/target/generated-sources/java/com/myproject/domain/QUsersFollow.java:[45,109] cannot find symbol
[ERROR] symbol:   variable id
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project analytics-service: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:972)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 20 more

生成的 QCLASS(注意无法解析符号 'id')

package com.myproject.domain;

import static com.querydsl.core.types.PathMetadataFactory.*;

import com.querydsl.core.types.dsl.*;

import com.querydsl.core.types.PathMetadata;
import javax.annotation.Generated;
import com.querydsl.core.types.Path;

import com.querydsl.sql.ColumnMetadata;
import java.sql.Types;

/**
 * QUsersFollow is a Querydsl query type for QUsersFollow
 */
@Generated("com.querydsl.sql.codegen.MetaDataSerializer")
public class QUsersFollow extends com.querydsl.sql.RelationalPathBase<QUsersFollow> 

    private static final long serialVersionUID = 463786116;

    public static final QUsersFollow usersFollow = new QUsersFollow("users_follow");

    public final NumberPath<Short> active = createNumber("active", Short.class);

    public final DateTimePath<java.sql.Timestamp> createdAt = createDateTime("createdAt", java.sql.Timestamp.class);

    public final DateTimePath<java.sql.Timestamp> modifiedAt = createDateTime("modifiedAt", java.sql.Timestamp.class);

    public final NumberPath<Long> userId1 = createNumber("userId1", Long.class);

    public final NumberPath<Long> userId2 = createNumber("userId2", Long.class);

    public final com.querydsl.sql.PrimaryKey<QUsersFollow> usersFollowNewPkey = createPrimaryKey(userId1, userId2);

    public final com.querydsl.sql.ForeignKey<QUsers> usersFollowNewUserId1Fkey = createForeignKey(userId1, "id");

    public final com.querydsl.sql.ForeignKey<QUsers> usersFollowNewUserId2Fkey = createForeignKey(userId2, "id");

    public final com.querydsl.sql.ForeignKey<QUsersFollow> _usersFollowNewUserId1Fkey = createInvForeignKey(id, "user_id1"); //cannot resolve symbol 'id'

    public final com.querydsl.sql.ForeignKey<QUsersFollow> _usersFollowNewUserId2Fkey = createInvForeignKey(id, "user_id2"); //cannot resolve symbol 'id'

    public QUsersFollow(String variable) 
        super(QUsersFollow.class, forVariable(variable), "public", "users_follow");
        addMetadata();
    

    public QUsersFollow(String variable, String schema, String table) 
        super(QUsersFollow.class, forVariable(variable), schema, table);
        addMetadata();
    

    public QUsersFollow(Path<? extends QUsersFollow> path) 
        super(path.getType(), path.getMetadata(), "public", "users_follow");
        addMetadata();
    

    public QUsersFollow(PathMetadata metadata) 
        super(QUsersFollow.class, metadata, "public", "users_follow");
        addMetadata();
    

    public void addMetadata() 
        addMetadata(active, ColumnMetadata.named("active").withIndex(3).ofType(Types.SMALLINT).withSize(5).notNull());
        addMetadata(createdAt, ColumnMetadata.named("created_at").withIndex(4).ofType(Types.TIMESTAMP).withSize(29).withDigits(6).notNull());
        addMetadata(modifiedAt, ColumnMetadata.named("modified_at").withIndex(5).ofType(Types.TIMESTAMP).withSize(29).withDigits(6).notNull());
        addMetadata(userId1, ColumnMetadata.named("user_id1").withIndex(1).ofType(Types.BIGINT).withSize(19).notNull());
        addMetadata(userId2, ColumnMetadata.named("user_id2").withIndex(2).ofType(Types.BIGINT).withSize(19).notNull());
    


【问题讨论】:

使用 QueryDSL 4.1.0 和 Postgres 对我来说可以正常工作 - 但它不会生成“反向”外键,这是问题的根本原因。但我找不到任何启用它的选项。您是否尝试过当前版本?或者至少 4.0.8? @a_horse_with_no_name 用 4.1.0 试过,问题依然存在。 【参考方案1】:

QueryDSL 开发人员:Redshift 不是受支持的数据库,我想说 JDBC 驱动程序未正确提供数据库元数据。

https://github.com/querydsl/querydsl/issues/1880

【讨论】:

以上是关于使用 Redshift 设置的 QueryDSL的主要内容,如果未能解决你的问题,请参考以下文章

AWS Redshift - 在卸载到 s3 时设置零件大小

Redshift 数据 API:以编程方式为 getStatementResult 设置 MaxResults

AWS Glue to Redshift:是否可以替换,更新或删除数据?

com.zaxxer.hikari.pool.HikariPool$PoolInitializationException:尝试使用 Redshift 设置 HikraCP 时无法初始化池

如何在 Redshift 中批量更新设置值?

从 S3 加载时,Redshift 使用了错误的时区