Is it possible to use Render Mapping if the input and output db is in two different database servers

青春壹個敷衍的年華 提交于 2019-12-24 14:27:46

问题


I'm working on a migration project where a table is read from and certain fields are moved to another table. It's implemented in such a manner that a Java client app reads the source table on its own, the results are then lodged into a Pojo and a POST request is made using that Pojo as a request body, to a REST server which then handles the task of writing to another table. Currently, I'm using JOOQ in my client app to read from the source table, without any code generation. I'm using String literals to map to the table columns and identifier building API, like this (select example):

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)));

The situation is so that I need to read the same table from multiple databases and I also have access to an artifactory that contains the JOOQ generated classes of all the tables, including the one that I need to read from. Now, the problem is, the JOOQ generated classes come from a specific database server A and the table I need to read from is inside a database in database server B. When I tried to read the table using the generated class from the artifactory, I get an error message that said "Table doesn't exist". "Table", in this context, refers to the database table which was used for JOOQ generation, from another database server. Is there any way I can configure a RenderMapping that sets the input to the database on server A and output to the server I'm currently reading from? I have configured RenderMapping between two databases on the same server, but no idea if it works for two different database servers. The main thing I'm trying to do here is to use pre-generated JOOQ classes from an artifactory to read from a single table without generating my own.


回答1:


Managed to fix the issue after the following suggestion from my supervisor. Apart from Settings, I also had to use Configuration. Something like this:

Configuration jooqConfig = new DefaultConfiguration();

Then Create a new Settings with render mapping.

Settings jooqSettings = new Settings()
    .withRenderMapping(new RenderMapping().withSchemata(
        new MappedSchema().withInput(sourceDb).withOutput(outputDb)))
    .withRenderFormatted(true);

Assign the settings and SQLDialect to jooqConfig

jooqConfig.set(jooqSettings);
jooqConfig.set(SQLDialect.MARIADB);

and finally, inside 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()
}

Apparently, I only needed to tell jOOQ to use the sourceDB schema for all databases as it's the source database for JOOQ generation and also didn't need to add any jooq plugin inside pom.xml. The only things I needed were JOOQ and artifact related dependencies.



来源:https://stackoverflow.com/questions/54865716/is-it-possible-to-use-render-mapping-if-the-input-and-output-db-is-in-two-differ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!