isaacdew/load-data
Composer 安装命令:
composer require isaacdew/load-data
包简介
Fluent syntax for loading large CSV files into MySQL using LOAD DATA INFILE in Laravel.
README 文档
README
MySQL and MariaDB come equipped with the LOAD DATA INFILE statement which allows for loading large datasets from a CSV or similar file into a table very quickly. This package provides an API for constructing and executing a LOAD DATA INFILE statement in Laravel.
Installation
Install this package using composer:
composer require isaacdew/load-data
Examples
Basic Example
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->fieldsTerminatedBy(',') ->fieldsEnclosedBy('"', optionally: true) ->load();
Ignoring the CSV Header
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->ignoreHeader() ->load();
Defining Columns
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->fieldsTerminatedBy(',') ->fieldsEnclosedBy('"', optionally: true) ->columns([ 'column_one', 'column_two', 'column_three' ]) ->load();
Using the Headers from the CSV to Define Your Columns
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->fieldsTerminatedBy(',') ->fieldsEnclosedBy('"', optionally: true) ->useFileHeaderForColumns() ->load();
By default, the CSV headers are converted to snake case since the columns need to match your database table column names. If you need to do any custom modification, you can pass a callback to the useFileHeaderForColumns method.
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->fieldsTerminatedBy(',') ->fieldsEnclosedBy('"', optionally: true) // Remove parenthesis from column names ->useFileHeaderForColumns(fn($column) => preg_replace('/(\(.*)$/', '', $column)) ->load();
Only Loading Specific Columns from the CSV
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->fieldsTerminatedBy(',') ->fieldsEnclosedBy('"', optionally: true) ->useFileHeaderForColumns() ->onlyColumns([ 'column_one', 'column_two' ]) ->load();
Truncating the Table Before Load
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->truncateBeforeLoad() ->load();
Using Set Statements
To use this feature, you must define the columns first either with the columns method or by using the file header with the useFileHeaderForColumns method. Then you can modify the value from your CSV using a MySQL expression. A good use case is a date column where the CSV isn't using a MySQL friendly format. Note that you must prefix the column name with @ to use it in your expression.
use Isaacdew\LoadData\LoadData; LoadData::from(storage_path('path/to/file.csv')) ->to('tablename') ->fieldsTerminatedBy(',') ->fieldsEnclosedBy('"', optionally: true) ->columns([ 'column_one', 'column_two', 'column_three', 'date_column' ]) ->setColumn('date_column', "STR_TO_DATE(@date_column, '%c/%d/%Y')") // Convert MM/DD/YYYY to a MySQL date ->load();
Dedicated Database Servers
If your Laravel application is not on the same server as your database, you will have to make sure the LOAD DATA LOCAL INFILE statement is enabled on your database server and in PDO. This package will automatically use the LOCAL keyword if your DB_HOST is not set to 127.0.0.1 or localhost.
To enable LOAD DATA LOCAL INFILE in PDO, go to your config/database.php file and add PDO::MYSQL_ATTR_LOCAL_INFILE => true to the array of options for the mysql connection like so:
return [ 'connections' => [ //... 'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), PDO::MYSQL_ATTR_LOCAL_INFILE => true // Add this line! ]) : [], ], //... ] ];
Note On Security
Prepared statements are not supported for LOAD DATA INFILE statements. With that being the case, do not use user input for constructing a LOAD DATA INFILE statement. I took the precaution of using PDO::quote() to escape the filename, however, I would still recommend against the use of a user provided filename in this statement.
isaacdew/load-data 适用场景与选型建议
isaacdew/load-data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 181 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 isaacdew/load-data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 isaacdew/load-data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 181
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-03