如果输入和输出数据库位于两个不同的数据库服务器中,是否可以使用渲染映射
Is it possible to use Render Mapping if the input and output db is in two different database servers
我正在做一个迁移项目,其中读取 table 并将某些字段移动到另一个 table。它的实现方式是 Java 客户端应用程序自己读取源 table,然后将结果提交到 Pojo
并使用 POST
发出请求Pojo
作为请求主体,发送到 REST 服务器,然后处理写入另一个 table 的任务。目前,我在我的客户端应用程序中使用 JOOQ
从源 table 读取数据,而没有任何代码生成。我正在使用 String
文字映射到 table 列和标识符构建 API,就像这样(select 示例):
return ctx.select(field(name(ID)),
field(name(CUSTOMER_ID)),
field(name(SIZE)),
field(name(NAME)),
field(name(CONTENT)))
.from(table(TABLE))
.where((field(name(UPLOAD)).eq((byte) 0)));
情况是这样的,我需要从多个数据库中读取相同的 table,并且我还可以访问一个 artifactory
,其中包含所有数据库的 JOOQ 生成的 classes tables,包括我需要阅读的那个。现在,问题是,JOOQ 生成的 classes 来自特定的数据库服务器 A
而我需要读取的 table 在数据库服务器 B
中的数据库中.当我尝试使用 artifactory
中生成的 class 读取 table 时,我收到一条错误消息 "Table doesn't exist"。 "Table",在此上下文中,指的是来自另一个数据库服务器的用于生成 JOOQ 的数据库 table。有什么方法可以配置 RenderMapping
,将 input
设置为服务器 A
上的数据库,将 output
设置为我当前正在读取的服务器?我已经在同一台服务器上的两个数据库之间配置了 RenderMapping
,但不知道它是否适用于两个不同的数据库服务器。我在这里要做的主要事情是使用来自 artifactory
的预生成的 JOOQ classes 从单个 table 中读取而不生成我自己的
根据我的主管的以下建议设法解决了这个问题。除了Settings
,我还得用Configuration
。像这样:
Configuration jooqConfig = new DefaultConfiguration();
然后使用渲染映射创建一个新的 Settings
。
Settings jooqSettings = new Settings()
.withRenderMapping(new RenderMapping().withSchemata(
new MappedSchema().withInput(sourceDb).withOutput(outputDb)))
.withRenderFormatted(true);
将设置和 SQLDialect
分配给 jooqConfig
jooqConfig.set(jooqSettings);
jooqConfig.set(SQLDialect.MARIADB);
最后,在 try-with-resources 中,
try (DSLContext ctx = DSL.using(jooqConfig.derive(getDataSource()))) {
/* getDataSource() returns a HikariDataSource with output db credentials */
do something, for ex., ctx.select()
}
显然,我只需要告诉 jOOQ 对所有数据库使用 sourceDB
模式,因为它是 JOOQ 生成的源数据库,也不需要在 pom.xml
中添加任何 jooq 插件.我唯一需要的是 JOOQ 和 artifact
相关的依赖项。
我正在做一个迁移项目,其中读取 table 并将某些字段移动到另一个 table。它的实现方式是 Java 客户端应用程序自己读取源 table,然后将结果提交到 Pojo
并使用 POST
发出请求Pojo
作为请求主体,发送到 REST 服务器,然后处理写入另一个 table 的任务。目前,我在我的客户端应用程序中使用 JOOQ
从源 table 读取数据,而没有任何代码生成。我正在使用 String
文字映射到 table 列和标识符构建 API,就像这样(select 示例):
return ctx.select(field(name(ID)),
field(name(CUSTOMER_ID)),
field(name(SIZE)),
field(name(NAME)),
field(name(CONTENT)))
.from(table(TABLE))
.where((field(name(UPLOAD)).eq((byte) 0)));
情况是这样的,我需要从多个数据库中读取相同的 table,并且我还可以访问一个 artifactory
,其中包含所有数据库的 JOOQ 生成的 classes tables,包括我需要阅读的那个。现在,问题是,JOOQ 生成的 classes 来自特定的数据库服务器 A
而我需要读取的 table 在数据库服务器 B
中的数据库中.当我尝试使用 artifactory
中生成的 class 读取 table 时,我收到一条错误消息 "Table doesn't exist"。 "Table",在此上下文中,指的是来自另一个数据库服务器的用于生成 JOOQ 的数据库 table。有什么方法可以配置 RenderMapping
,将 input
设置为服务器 A
上的数据库,将 output
设置为我当前正在读取的服务器?我已经在同一台服务器上的两个数据库之间配置了 RenderMapping
,但不知道它是否适用于两个不同的数据库服务器。我在这里要做的主要事情是使用来自 artifactory
的预生成的 JOOQ classes 从单个 table 中读取而不生成我自己的
根据我的主管的以下建议设法解决了这个问题。除了Settings
,我还得用Configuration
。像这样:
Configuration jooqConfig = new DefaultConfiguration();
然后使用渲染映射创建一个新的 Settings
。
Settings jooqSettings = new Settings()
.withRenderMapping(new RenderMapping().withSchemata(
new MappedSchema().withInput(sourceDb).withOutput(outputDb)))
.withRenderFormatted(true);
将设置和 SQLDialect
分配给 jooqConfig
jooqConfig.set(jooqSettings);
jooqConfig.set(SQLDialect.MARIADB);
最后,在 try-with-resources 中,
try (DSLContext ctx = DSL.using(jooqConfig.derive(getDataSource()))) {
/* getDataSource() returns a HikariDataSource with output db credentials */
do something, for ex., ctx.select()
}
显然,我只需要告诉 jOOQ 对所有数据库使用 sourceDB
模式,因为它是 JOOQ 生成的源数据库,也不需要在 pom.xml
中添加任何 jooq 插件.我唯一需要的是 JOOQ 和 artifact
相关的依赖项。