简体   繁体   中英

Spring boot JPA: Use library to Bulk load data for MySQL

I'm fairly new to Spring boot.

I'm currently writing a small microservice which will bulk load data from a CSV file into a MySQL database table. I understand that there are many ways to achieve this, but one of the ways I was thinking was to use the MySQL LOAD DATA FILE command to parse the csv file and load the data into the database table.

I was wondering if anyone knows of a way that I can invoke the MySQL LOAD DATA command using the Spring boot JPA library ?

Any help/ advice is appreciated.

Thanks

Found a solution to the problem:

Add the @Modifying and @Transactional annotations to your method in the Repository. Here is an example:

@Repository
public interface ExpenseRepository extends CrudRepository<Expense, Integer>{

    @Modifying
    @Transactional
    @Query (value="LOAD DATA LOCAL INFILE 'C:/Users/Tester/Documents/transaction_data.txt' INTO TABLE tbl_fin FIELDS TERMINATED BY ',' IGNORE 1 LINES", nativeQuery = true)
    public void bulkLoadData();

}

If you have the following error

java.sql.SQLSyntaxErrorException: The used command is not allowed with this MySQL version

Do not forget to allow load local file because it is disabled by default. Add to spring.datasource.url: &allowLoadLocalInfile=true and also enable it in your local MySQL Configuration with the command: SET GLOBAL local_infile = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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