Home

Awesome

<p align="center"> <img src="https://raw.githubusercontent.com/Arp-G/csv2sql/master/.github/images/csv2sql.png" alt="Csv2Sql image"/> </p> <h3 align="center"> <i>CSV2SQL - Blazing fast csv to database loader! </i> </h3>

Table of Contents

  1. What is Csv2sql ?
  2. Why Csv2sql ?
  3. Using from Command Line
    1. Installation and usage
    2. Available command line arguments
    3. Examples of usage
  4. Using the browser based interface
    1. Installation and usage
  5. Running from source
  6. Supported data types
  7. Handling custom date/datetime formats
  8. Known issues, caveats and troubleshooting
  9. Future plans

Please have a quick look over the Known issues, caveats and troubleshooting section before using the app.

<a name="what"></a>

What is Csv2sql?

Csv2Sql is a blazing fast fully automated tool to load huge CSV files into a RDBMS.

Csv2Sql can automatically...

<a name="why"></a>

Why Csv2sql ?

<a name="cmd"></a>

Using from command line

Csv2sql can be easily used as a command line tool, with lots of customizable options passing by different command line arguments.

<p align="center"> <img src="https://raw.githubusercontent.com/Arp-G/csv2sql/master/.github/images/cmd.gif" alt="command line app demo"/> </p>

<a name="cmdinstall"></a>

Installation and usage:

You must have erlang installed to use the command line tool on any linux distribution.

Add the erlang repository using the following commands
wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
sudo dpkg -i erlang-solutions_1.0_all.deb
Install erlang
sudo apt-get update
sudo apt-get install esl-erlang

Download the executable binary from the latest release in this repository and run the executable using: ./csv2sql --<argument>

The next section describes all the available command line arguments.

<a name="cmdargs"></a>

Using command line args

You can pass various command line arguments to Csv2Sql to configure how to process csvs and specify other important information.

A description of all the available command line arguments that can be used are given below:

FlagDescriptionDefault value
--schema-file-pathThe location were the generated schema file will be storedIf no value is supplied it saves the generated schema file in the same directory as the source csv files specified by "--source-csv-directory" flag
--source-csv-directoryThe source directory where the csvs are locatedDefaults to the current directory from which the program is run
--db-connection-stringA connection string to connect ot the database, in the format: "<database_type>:<database_username>:<database_password>@<database_host>/<database_name>"This is a compulsory argument if database access is required
--imported-csv-directoryThe directory were the csvs will be moved after importing to database, make sure it is present and is empty(source-csv-directory)/imported
--validated-csv-directoryThe directory were the csvs will be moved after they are validated, make sure it is present and is empty(source-csv-directory)/validated
--skip-make-schemaSkip inferring schema and making a schema filefalse
--skip-insert-schemaSkip inserting the inferred schema in the database. Useful if the table structures are already present and you only wish to insert data from the csv files.(This will be true automatically if skip-make-schema is used)false
--skip-insert-dataSkip inserting data from the csvsfalse
--skip-validate-importSkip validating the imported datafalse
--connection-socketThe mysql socket file path/var/run/mysqld/mysqld.sock
--varchar-limitThe value of varchar type, and the limit after which a string is considered a text and not a varchar100
--schema-infer-chunk-sizeThe chunk size to use when the schema fora CSV will be inferred parallelly. For example, a chunk size 100 means the CSV will be read 100 rows at a time and separate processes will be used to infer the schema for each 100-row chunk100
--worker-countThe number of workers, directly related to how many CSVs will be processed parallelly10
--db-worker-countThe number of database workers, lowering the value will lead to slow performance but lesser load on database, a higher value can lead to too many database connection errors.15
--insertion-chunk-sizeNumber of records to insert into the database at once, increasing this may result in mysql error for too many placeholders100
--job-count-limitNumber of chunks to keep in memory (Memory required=insertion_chunk_size * job_count_limit)10
--logEnable ecto logs, to log the queries being executed, possible values are :debug, :info, :warnfalse
--timeoutThe time in milliseconds to wait for the query call to finish60000
--connect-timeoutThe number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake60000
--pool-sizeThe pool_size controls how many connections you want to the database.20
--queue-targetThe time to wait for a database connection5000
--queue-intervalIf all connections checked out during a :queue_interval takes more than :queue_target, then we double the :queue_target.1000

<a name="cmdexamples"></a>

Examples:

Load csvs to database, this will infer the schema, insert the inferred schemas to the database, insert the data and then validate data for all the csvs

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --db-connection-string "mysql:root:pass@localhost/test_csv"

Here "mysql" is the database type, "root" is the mysql username, "pass" is the mysql password, "localhost" is the database host and "test_csv" is the database name where the data will be imported.


Import schema only:

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --skip-insert-schema --skip-insert-data --skip-validate-import


Skip validation:

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --db-connection-string "postgres:root:pass@localhost/test_csv" --skip-validate-import

Here "postgres" is the database type.


Only validate imported csv:

./csv2sql --skip-make-schema --skip-insert-data --imported-csv-directory "/home/user/Desktop/imported-csvs" --db-connection-string "mysql:root:pass@localhost/test_csv"

Here we are running simple validation check over a previously imported csvs, this check will NOT compare the actual data but will only compare the row count in the csv and in the database.


Custom path for imported and validated csv files:

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --imported-csv-directory "/home/user/Desktop/imported_csvs" --validated-csv-directory "/home/user/Desktop/validated_csvs" --db-connection-string "postgres:root:pass@localhost/test_csv"


Only infer and create schema but don't insert data:

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --skip-insert-data --db-connection-string "postgres:root:pass@localhost/test_csv"

This will create empty table in the database after analyzing the csvs.


Change the worker count, setting this to one will lead to processing a single csv at a time, this will be slower but will lead to lower cpu usage and Database load:

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --worker-count 1 --db-connection-string "mysql:root:pass@localhost/test_csv"


Enable logs, to log the queries being executed:

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --log debug --db-connection-string "mysql:root:pass@localhost/test_csv"


Set the number of workers inserting data into the database, lowering the value will lead to slow performance but lesser load on database, a higher value can lead to too many database connection errors:

./csv2sql --source-csv-directory "/home/user/Desktop/csvs" --db-worker-count 2 --db-connection-string "mysql:root:pass@localhost/test_csv"

<a name="dashboard"></a>

Using csv2sql from your browser

For ease of use csv2sql also has a browser interface which can be used to easily configure the tool and also provides an interface that shows what is the progress of the various running tasks, which files are currently being processed, the current cpu and memory usage, etc.

<p align="center"> <img src="https://raw.githubusercontent.com/Arp-G/csv2sql/master/.github/images/dashboard.gif" alt="browser interface demo"/> </p>

Installation and usage: <a name="dashboardinstall"></a>

There are no dependencies needed to use the app via your browser, however you must have mysql or postgres installed.

Download the latest release of the app from the releases section in this repository.

You can now easily run the executable on your linux system, by:

This will run a local server which your access at localhost:4000 in your browser.

Thats all!

Please create an issue with details of your OS distribution, architecture(for example, x86_64 or ARM) and ABI (for example, musl or gnu) if the app does not run on your system

Using the app via the browser is super easy, once the app is running, go to localhost:4000 in your browser.

Now go to the Change configuration tab, and enter the relevant configuration details, hover over any configuration option to see what it does.

Whenever your are done, click on the Start tab and click on Start button below to start the import process.

<a name="sourceinstall"></a>

Running the app from source code

You must have elixir and mysql/postgresql installed in your system to run Csv2Sql.

To use the app just clone this repository and then install dependencies by mix deps.get

Finally, start the application by mix phx.server

This runs the phoenix server at localhost:4000 which provides a browser based interface to use the app.

Thats all !

<a name="support"></a>

Supported data types

Csv2sql currently supports MySql and PostgreSQL database.

Csv2Sql will map data in CSVs into one of the following data types:

Typemysqlpostgres
dateFor values matching pattern like YYYY-MM-DD or custom patternsNOT SUPPORTED, will map to VARCHAR
datetimeFor values matching pattern like YYYY-MM-DD hh:mm:ss or custom patterns , (WARNING: fractional seconds or timezone information will be lost if present)NOT SUPPORTED, will map to VARCHAR
booleanMaps values 0/1 or true/false to BIT typeMaps values 0/1 or true/false to BOOLEAN type
integerINTINT
floatDOUBLENUMERIC(1000, 100)
varcharVARCHARVARCHAR
textTEXTTEXT

All other types of data, will map to either VARCHAR or TEXT.

<a name="datetime"></a>

Handling custom date/datetime formats

By default csv2sql will identify date or datetime of the following patterns YYYY-MM-DD and YYYY-MM-DD hh:mm:ss respectively. If a csv file contains date or datetime in some other format then they will be imported as varchar by default however by specifying custom patterns we can import such data of arbitrary formats as date or datetime.

csv2sql uses the Timex library to parse date/datetime. You can specify multiple custom patterns for date or datetime as a string having one or more patterns separated by ;

When using the Web UI for csv2sql enter these pattern strings in the config page under "Custom date patterns" or "Custom datetime patterns".

The patterns should be compatible with Timex directives specified here.

(Custom patterns are only supported when using the web ui and are not available in the cli version of the application)

Good to know/Caveats

Examples

To parse datetime like 11/14/2021 3:43:28 PM a pattern like {0M}/{0D}/{YYYY} {h12}:{m}:{s} {AM} can be specified

The custom pattern needed is like...

{0M}/{0D}/{YYYY} {h12}:{m}:{s} {AM}

Consider a CSV with date or datetime having multiple formats like...

Example DateDate PatternExample DatetimeDatetime Pattern
2021-11-14{YYYY}-{0M}-{0D}2021-11-14T15:43:28{YYYY}-{0M}-{0D}T{0h24}:{m}:{s}
11-14-2021{0M}-{0D}-{YYYY}11-14-2021 15:43:28{0M}-{0D}-{YYYY} {0h24}:{m}:{s}
11/14/2021{0M}/{0D}/{YYYY}11/14/2021 3:43:28 PM{0M}/{0D}/{YYYY} {h12}:{m}:{s} {AM}

The pattern strings to parse the above csv would look like...

For date {YYYY}-{0M}-{0D};{0M}-{0D}-{YYYY}

For datetime {YYYY}-{0M}-{0D}T{0h24}:{m}:{s};{0M}-{0D}-{YYYY} {0h24}:{m}:{s};{0M}/{0D}/{YYYY} {h12}:{m}:{s} {AM}

<a name="issues"></a>

Known issues, caveats and troubleshooting:

%MyXQL.Error{connection_id: 9, message: "(1067) (ER_INVALID_DEFAULT) Invalid default value...

In this case, please try running the app again.

<a name="future"></a>

Future