mr4-lc/php-json-exporter

在不耗尽内存的情况下将大型数据集导出到JSON文件

0.0.1 2023-12-19 10:18 UTC

This package is auto-updated.

Last update: 2024-09-19 11:51:02 UTC


README

为什么创建这个库

Hyvor Talk 上,我们有客户在他们的网站上拥有数百万条评论,他们希望定期导出数据以进行备份和分析。我们的第一个导出器是编写来从数据库获取所有记录并创建一个JSON文件的,这会将所有数据都加载到内存中。它适用于小型网站,但大型网站不能使用这个方法,因为服务器会因为内存耗尽而崩溃。因此,我们创建了这个库,以将数据导出到磁盘上的JSON文件中,而不将其加载到内存中。

分支:支持PHP 7.4

您能做什么

这个库的主要目的是将大量的小对象数组(例如,表行)导出到磁盘上的JSON文件。您可以使用多个集合和直接值创建JSON文件。

{
    "collection-1": [
        // an array of objects (rows)
        {},
        {},
        {}
    ],
    "collection-2": [
        {},
        {},
        {}
    ],
    "direct-value": "value"
}

集合中的每个对象可以表示您的数据库表中的一行。期望数组很长(包含很多对象/行)。

安装

composer require mr4-lc/php-json-exporter

用法

use Mr4Lc\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();

在上面的示例中,getUsers()getPosts() 是假设的函数,它们以数组的形式返回有限数量的记录(100),并支持一个偏移量参数来跳过已添加的记录。通常,您会在循环或回调中调用 addItems() 方法(下面有Laravel示例)。上述示例的JSON输出将如下所示

{
    "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示例:集合

您可以使用 Laravel分批处理 来生成大型集合。

use Mr4Lc\JsonExporter\File;

$file = new File('export-file.json');

$usersCollection = $file->collection('users');
User::chunk(200, fn ($users) => $usersCollection->addItems($users->toArray()));

$file->end();