php 解析大json

<?php
/*
 * @Description: 
 * @Version: 2.0
 * @Autor: ABing
 * @Date: 2024-06-05 17:59:30
 * @LastEditors: lhl
 * @LastEditTime: 2024-06-21 15:49:20
 */

namespace App\Console\Commands;

use App\Models\SxTask;
use App\Services\HttpClient;
use App\Services\ShaXiang;
use App\Support\ElasticsearchSupport;
use Illuminate\Console\Command;
use Log;

class LoadVul extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'LoadVul';

    /**
     * The console command description.
     *
     * @var string
     * https://github.com/Live-Hack-CVE/full_database
     */
    protected $description = '漏洞数据入ES';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $this->info('开始执行');

        ini_set('memory_limit', '2256M');

        $handle = fopen(storage_path('hyuna_vulnclass_202406181036.json'), "rb");


        $jsonstr = "";

        $batchInsert = [];

        if ($handle) {
            while (!feof($handle)) {
                $v = fgets($handle); // 读取一行

                // echo $v;
                if (strlen($v) < 10) {
                    var_dump($v);
                }

                if (strlen($v)==3) {
                    // dd($v);
                    $jsonstr = "";
                }

                $jsonstr .= $v;

                // echo $jsonstr;
                if (strlen($v)==4) {

                    $jsonstr = rtrim($jsonstr, ",\n");

                    $jsonstr = ltrim($jsonstr, "\t");
                    
                    $arr = json_decode($jsonstr, 1);


                    if (!isset($arr['id'])) {

                        // $jsonstr = str_replace("\t", '', $jsonstr); 

                        // $newArr=json_decode($jsonstr,1);
                        dd($jsonstr, $arr);
                        Log::info('没有解析正确啊', ['str' => $jsonstr, 'arr' => $arr]);
                        continue;
                    }
                    // dd($jsonstr,$arr);
                    $batchInsert[] = $arr;
                    $jsonstr = "";
                }


                if (count($batchInsert) >= 10) {
                    $this->save($batchInsert);
                    $batchInsert=[];
                }
            }

            fclose($handle);
        }

        $this->info('执行结束');
    }


    protected function  save($data)
    {

        $index = 'vul';

        $esInit = (new ElasticsearchSupport())->initFun($index, [config('es.wxqb')]);

        $batchInsert = [];

        foreach ($data as $one) {

            $batchInsert[]['create'] = [
                '_index' => $index,
                '_id' => $one['id'],
            ];

            $one['createtime'] = time();

            $batchInsert[] = $one;
        }

        $res = $esInit->getClient()->bulk([
            'index' => $esInit->getIndex(),
            'type' => 'doc',
            'body' => $batchInsert
        ]);

        var_dump($res);
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值