mschuett/jmem
此包已被弃用,不再维护。未建议替代包。
将大JSON文件拆分成合理大小。
dev-master
2016-02-21 23:29 UTC
Requires
- php: >=5.5.0
This package is not auto-updated.
Last update: 2021-07-06 22:38:11 UTC
README
迭代大型JSON数组,不占用所有内存。
示例用法
要开始使用jmem,请将以下行添加到您的composer.json文件中。
{ "require": { "mschuett/jmem": "dev-master" } }
接下来设置您的自动加载,然后就可以出发了。
<?php require 'vendor/autoload.php'; $gen = new Jmem\JsonLoader("Hangouts.json", "conversation_state"); foreach($gen->parse()->start() as $obj) { $obj->stream; }
关于
Jmem是为了在尝试将谷歌Hangouts的JSON数据(200MB+文件)放入数据库时解析大型JSON文件而编写的。PHP可能不是这项工作的最佳工具,但是当它是唯一可用的工具时,它做得很好。目前将200MB文件拆分大约需要1.5分钟,因此在上传后将其在后台运行是最佳选择。使用生成器是为了在任何时候都不会占用大量内存。
文档
我已经非常详细地注释了代码,因此很容易查看源代码并进行修改。话虽如此,以下是主要的JsonLoader类。
/** * $file is the path to the file that you would like to parse though. * If the file does not exist an exception will be thrown. Element is * the array of objects you would like to have broken up and returned * to you. * * @param String $file * @param String $element * @param int $bytes (1024 default) */
生成器返回一个JSON对象,您可以通过它访问以下内容。Stream包含完整的JSON对象,number是对象在数组中的位置,从1开始。
class JsonObject { public $number = 0; public $stream = ""; public function __construct($stream, $num) { $this->stream = $stream; $this->number = $num; } }
贡献
请随时发布您可能发现的错误或提交pull request。我喜爱所有的反馈,无论是好是坏!