简体   繁体   English

使用将数据加载到CSV文件加载到mysql表中。 如何将以下查询转换为JOOQ?

[英]Load CSV file to a mysql table using Load data into. How do I convert the following query into JOOQ?

LOAD DATA INFILE '/testing.csv'
IGNORE INTO TABLE Test_table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
IGNORE 1 ROWS (@col1)
SET test_ID="100",
    test_reg_ID ="26003",
    VALUE =@col1;

testing.csv: testing.csv:

在此处输入图片说明

TABLE Test_table 表格Test_table

在此处输入图片说明

Question No.1: Help me convert this into JOOQ 3.6 Question No.2: I want to avoid the empty data which falls on row 1. 问题1:帮我将其转换为JOOQ 3.6问题2:我要避免第1行出现空数据。

jOOQ has a CSV import API for that purpose. jOOQ为此提供了CSV导入API Here's how you'd translate that MySQL command to jOOQ: 这是将MySQL命令转换为jOOQ的方法:

DSL.using(configuration)
   .loadInto(TEST_TABLE)
   .loadCSV(new File("/testing.csv"))
   .fields(TEST_TABLE.VALUE)
   .separator(',')
// Available in jOOQ 3.10 only: https://github.com/jOOQ/jOOQ/issues/5737
// .lineSeparator("\n")
   .ignoreRows(1)
   .execute();

Note that jOOQ's Loader API doesn't support those default expressions as MySQL does (see #5740 ): 请注意,jOOQ的Loader API不像MySQL那样支持那些默认表达式(请参阅#5740 ):

SET test_ID="100",
    test_reg_ID ="26003"

There are a few workarounds: 有一些解决方法:

  1. You could patch the CSV data and prepend those columns before loading them. 您可以修补CSV数据并在加载这些列之前添加它们。
  2. You could use DSLContext.fetchFromCSV() and then use stream().map() to prepend the missing data, before using the alternative Record import API rather than the suggested CSV import API 您可以使用DSLContext.fetchFromCSV() ,然后使用stream().map()来添加丢失的数据,然后再使用替代的Record导入API,而不是建议的CSV导入API
  3. You could run a simple UPDATE statement right after the import for this data. 您可以在导入此数据后立即运行一个简单的UPDATE语句。

A note on performance 性能说明

Do note that jOOQ's loader API can be fine-tuned by specifying bulk, batch, and commit sizes . 请注意,可以通过指定批量,批处理和提交大小来微调jOOQ的加载器API。 Nevertheless, the database's out-of-the-box import implementation is very likely to still be much faster than any client side import that has to go through JDBC. 尽管如此,数据库的现成导入实现仍然很可能比必须通过JDBC进行的任何客户端导入都快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM