io.sitoolkit.csv:sit-csvloader-core

A application used to load CSV files to DB with JDBC connection.

License

License

Categories

Categories

CSV Data Data Formats
GroupId

GroupId

io.sitoolkit.csv
ArtifactId

ArtifactId

sit-csvloader-core
Last Version

Last Version

0.8
Release Date

Release Date

Type

Type

jar
Description

Description

A application used to load CSV files to DB with JDBC connection.

Download sit-csvloader-core

How to add to project

<!-- https://jarcasting.com/artifacts/io.sitoolkit.csv/sit-csvloader-core/ -->
<dependency>
    <groupId>io.sitoolkit.csv</groupId>
    <artifactId>sit-csvloader-core</artifactId>
    <version>0.8</version>
</dependency>
// https://jarcasting.com/artifacts/io.sitoolkit.csv/sit-csvloader-core/
implementation 'io.sitoolkit.csv:sit-csvloader-core:0.8'
// https://jarcasting.com/artifacts/io.sitoolkit.csv/sit-csvloader-core/
implementation ("io.sitoolkit.csv:sit-csvloader-core:0.8")
'io.sitoolkit.csv:sit-csvloader-core:jar:0.8'
<dependency org="io.sitoolkit.csv" name="sit-csvloader-core" rev="0.8">
  <artifact name="sit-csvloader-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.sitoolkit.csv', module='sit-csvloader-core', version='0.8')
)
libraryDependencies += "io.sitoolkit.csv" % "sit-csvloader-core" % "0.8"
[io.sitoolkit.csv/sit-csvloader-core "0.8"]

Dependencies

compile (1)

Group / Artifact Type Version
org.apache.commons : commons-csv jar 1.7

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.16

test (2)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.7.0
com.h2database : h2 jar 1.4.200

Project Modules

There are no modules declared in this project.

日本語

CSV Loader

The CSV Loader is a tool for loading CSV files into the DB.

As of v0.8, the CSV Loader will be used for "Flyway Java-based Migration" (specialized for https://flywaydb.org/documentation/concepts/migrations#java-based-migrations).

Required Software

  • Java 11+
  • Maven / Gradle

Usage (Flyway Java-based Migration)

This section describes how to use the CSV Loader within Flyway java-based Migration. First, configure the project with the Flyway Maven Plugin (with the https://flywaydb.org/documentation/usage/maven/) operational.

  • Directory configuration
project
  pom.xml
  src/main
    java
      db/migration
        V2__AddRecords.java
    resources
      db/migration
        V1__CreateTables.sql
        V2__AddRecords
          tabe-list.txt
          TABLE_1.csv
          TABLE_2.csv

Suppose you create a table in V1 _ _ CreateTables. sql and load it from a CSV file in V2 _ _ AddRecords. java.

  • V1__CreateTables.sql
CREATE TABLE TABLE_1 (
  COLUMN_1 INT,
  COLUMN_2 VARCHAR(10)
);

:

Next, add the CSV Loader dependency to pom. xml.

  • pom.xml
  <dependencies>
    <dependency>
      <groupId>io.sitoolkit.csv</groupId>
      <artifactId>sit-csvloader-core</artifactId>
      <version>0.8</version>
    </dependency>
  </dependences>

Next, create a Migration class to load the data-inherit BaseJavaMigration and run CsvLoader. load in migrate.

  • V2__AddRecords.java
package db.migration;

import org.flywaydb.core.api.logging.Log;
import org.flywaydb.core.api.logging.LogFactory;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import io.sitoolkit.csv.core.CsvLoader;

@SuppressWarnings("squid:S101")
public class V2__AddRecords extends BaseJavaMigration {

private final Log log = LogFactory.getLog(V2__AddRecords.class);

@Override
public void migrate(Context ctx) throws Exception {
CsvLoader.load(ctx.getConnection(), getClass(), log::info);
}
  
}

The table-list. txt contains the table names of the tables to be loaded in one version, one row at a time, from top to bottom.

  • table-list.txt
TABLE_1
TABLE_2

The CSV file contains the column names on the first line and the data to be loaded from the second line.

  • TABLE_1.csv
COLUMN_1,COLUMN_2
1,One
2,Two
3,Three

Once this is done, run Flyway Migration with the following command :

mvn flyway:migrate

If the migration is successful, CSV data is loaded into TABLE _ 1.

io.sitoolkit.csv

SI-Toolkit

Tools for smart system development

Versions

Version
0.8