hyvor/php-json-exporter
Composer 安装命令:
composer require hyvor/php-json-exporter
包简介
Export large datasets to a JSON file without memory exhaustion
README 文档
README
Why this library was created:
At Hyvor Talk, we have customers who have millions of comments on their websites, who want to export their data regularly to backup and analyze. Our first exporter was written to get all records from the database and create a JSON file from it, which took all data into memory. It worked for small websites, but larger websites could not use this as the server crashed due to memory exhaustion.So, we created this library to export data into a JSON file in the disk without taking it into the memory.
What you can do
The primary purpose of this library is to export large arrays of small objects (for example, table rows) into a JSON file in the disk. You can create a JSON file with multiple collections and direct values.
{
"collection-1": [
// an array of objects (rows)
{},
{},
{}
],
"collection-2": [
{},
{},
{}
],
"direct-value": "value"
}
Each object in a collection can be a representation of a row in a table of your database. The arrays are expected to be long (contains a lot of objects/rows).
Installation
composer require hyvor/php-json-exporter
Usage
use Hyvor\JsonExporter\File; $file = new File('export-file.json'); // you can use a relative or absolute path // add a collection named users $usersCollection = $file->collection('users'); $usersCollection->addItems(getUsers()); $usersCollection->addItems(getUsers(100)); // add a collection named $postsCollection = $file->collection('posts'); $postsCollection->addItems(getPosts()); $postsCollection->addItems(getPosts(100)); $postsCollection->addItems(getPosts(200)); // add a direct value // the value will be JSON encoded // you can use arrays, objects, strings, numbers, booleans, null $file->value('direct-value', 'value'); $file->value('direct-value-2', $myObject); $file->value('json-value', '{"name": "John"}', encode: false); // the value will not be JSON encoded // call this function finally $file->end();
In the above example, getUsers() and getPosts() are hypothetical functions that returns a limited number of records (100) as an array, and they support an offset parameter to skip already added records. Usually, you would call the addItems() method inside a loop or callback (See Laravel example below). The JSON output of the above example will look like this:
{
"users": [
// array of JSON-encoded user objects
],
"posts": [
// array of JSON-encoded post objects
],
"direct-value": "value",
"direct-value-2": {
// JSON-encoded object
},
"json-value": {"name": "John"}
}
Laravel Example for Collections
You can use Laravel Chunking to generate large collections.
use Hyvor\JsonExporter\File; $file = new File('export-file.json'); $usersCollection = $file->collection('users'); User::chunk(200, fn ($users) => $usersCollection->addItems($users->toArray())); $file->end();
hyvor/php-json-exporter 适用场景与选型建议
hyvor/php-json-exporter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 237.6k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hyvor/php-json-exporter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hyvor/php-json-exporter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 237.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-05