Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

load_data_infile2

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

load_data_infile2

  • 0.2.4
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

LoadDataInfile2

Gem Version Build Status Code Climate Test Coverage Dependency Status

Import the data at a high speed to the table from a text file, using the MySQL LOAD DATA INFILE statement.

This gem is dependent on mysql2.

By using mysql2, as well as plugin of ActiveRecord, it is possible to use in pure Ruby script.

Installation

Add to your Gemfile:

gem 'load_data_infile2'

And bundle.

Examples

Basic Usage

Database configuration:

db_config = {
  host: 'localhost'
  database: 'ldi_test'
  username: 'root'
}

Create client:

ldi_client = LoadDataInfile2::Client.new(db_config)

Import from CSV file:

ldi_client.import('/path/to/data.csv')

Default options are CSV format:

module LoadDataInfile2
  class << self
    def default_import_options
      @default_import_options ||= {
        fields_terminated_by: ',',          # CSV
        fields_optionally_enclosed_by: '"', # standard format of CSV
        fields_escaped_by: '"',             # standard format of CSV
        lines_terminated_by: "\\n",
        ignore_lines: 0
      }
    end
  end
end

TSV format

If you are using TSV format:

opts = {
  fileds_terminated_by: "\\t",
  fields_optionally_enclosed_by: "",
  fields_escaped_by: "\\"
}
ldi_client = LoadDataInfile2::Client.new(db_config, opts)
ldi_client.import('/path/to/data.tsv')

LOAD DATA LOCAL INFILE

If you use LOCAL option:

opts = { local_infile: true }
ldi_client = LoadDataInfile2::Client.new(db_config, opts)
ldi_client.import('/path/to/data.csv')
# => Execute "LOAD DATA LOCAL INFILE '/path/to/data.csv' INTO TABLE `ldi_test`.`data`;"

SQL Options

Support all options of LOAD DATA INFILE statement on MySQL 5.7 .

see: http://dev.mysql.com/doc/refman/5.7/en/load-data.html

For examples:

opts = { local_infile: true }
sql_opts = { table: 'special_users', ignore_lines: 1 }
ldi_client = LoadDataInfile2::Client.new(db_config, opts)
ldi_client.import('/path/to/users.csv', sql_opts)
Mappings
MySQLLoadDataInfile2
LOW_PRIORITYlow_priority_or_concurrent: :low_priority
CONCURRENTlow_priority_or_concurrent: :concurrent
LOCALlocal_infile: true
REPLACEreplace_or_ignore: :replace
IGNOREreplace_or_ignore: :ignore
tbl_nametable: 'special_table_name'
PARTITIONpartition: 'p0' / ['p0', 'p1', ...]
CHARCTER SETcharset: 'utf8'
FIELDS TERMINATED BYfields_terminated_by: ','
FIELDS ENCLOSED BYfields_enclosed_by: '"'
FIELDS OPTIONALLY ENCLOSED BYfields_optionally_enclosed_by: '"'
FIELDS ESCAPED BYfields_escaped_by: '"'
LINES STARTING BYlines_starting_by: '***'
LINES TERMINATED BYlines_terminated_by: '\n'
IGNORE LINESignore_lines: 1
col_name_or_user_varcolumns: ['col1', 'col2', '@var3', ...]
SET col_name = exprset: { col1: "'specific value'", col2: '@var', col3: 'NOW()' }

In Rails

Subclass of ActiveRecord is added .load_data_infile.

For example, in the case of User model, you can call the class method named load_data_infile from the User model.

User.load_data_infile('/path/to/data.csv')

If you want to pass options to the initialization of LoadDataInfile2::ActiveRecord, you can use the accessor of class variable named .default_load_data_infile_options.

User.default_load_data_infile_options = { ignore_lines: 1 }
User.load_data_infile('/path/to/data.csv')

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nalabjp/load_data_infile2.

License

MIT License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 30 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc