PHP code example of survos / jsonl-bundle

1. Go to this page and download the library: Download survos/jsonl-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

survos / jsonl-bundle example snippets


use Survos\JsonlBundle\IO\JsonlWriter;

$writer = JsonlWriter::open('data/products.jsonl'); // mode: 'w'

$writer->write([
    'id' => 1,
    'name' => 'Widget',
    'price' => 9.99,
]);

$writer->close();

use Survos\JsonlBundle\IO\JsonlWriter;

$writer = JsonlWriter::open(
    'data/products.jsonl',
    'a', // append
);

foreach ($products as $product) {
    $writer->write($product);
}

$writer->close();

use Survos\JsonlBundle\IO\JsonlWriter;

$writer = JsonlWriter::open(
    'data/products.jsonl',
    'w', // overwrite
);

$writer->write(['id' => 1, 'name' => 'Widget']);
$writer->close();

use Survos\JsonlBundle\IO\JsonlWriter;

$writer = JsonlWriter::open(
    'data/products.jsonl',
    'a', // resume
);

foreach ($remainingProducts as $product) {
    $writer->write($product);
}

$writer->close();

use Survos\JsonlBundle\IO\JsonlWriter;

$writer = JsonlWriter::open(
    'data/products.jsonl.gz',
    'w', // overwrite (default)
);

$writer->write([
    'id' => 1,
    'name' => 'Compressed Widget',
]);

use Survos\JsonlBundle\IO\JsonlWriter;
use Survos\JsonlBundle\IO\JsonlWriterOptions;

$writer = JsonlWriter::open(
    'var/data/products.jsonl',
    'w',
    new JsonlWriterOptions(
        ensureDir: true,
    ),
);

$writer->write($row);
$writer->close();

use Survos\JsonlBundle\IO\JsonlReader;

$reader = new JsonlReader('data/products.jsonl');

foreach ($reader as $row) {
    echo $row['name'] . PHP_EOL;
}

$reader = new JsonlReader('data/products.jsonl');
foreach ($reader as $row) {
    // Continue processing after record 5000
}

use Survos\JsonlBundle\Model\JsonlSidecar;

$repo  = new JsonlStateRepository();
$state = $repo->load('data/products.jsonl');

$stats = $state->getStats();

$rows      = $stats->getRows();
$bytes     = $stats->getBytes();
$completed = $stats->isCompleted();

if (!$state->isFresh()) {
    // JSONL changed outside of JsonlWriter
    // decide whether to trust, refresh, or regenerate
}

$writer = JsonlWriter::open('data/products.jsonl');

// write rows...

$writer->markComplete();
$writer->close();


declare(strict_types=1);

namespace App\Command;

use Survos\JsonlBundle\IO\JsonlReader;
use Survos\JsonlBundle\IO\JsonlWriter;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpClient\HttpClient;

#[AsCommand('products:list', 'List products using a JSONL cache')]
final class ProductsListCommand
{
    public function __invoke(SymfonyStyle $io): int
    {
        $file = 'var/products.jsonl';

        if (!file_exists($file)) {
            $client = HttpClient::create();
            $data   = $client->request('GET', 'https://dummyjson.com/products')->toArray();

            $writer = JsonlWriter::open($file);
            foreach ($data['products'] as $product) {
                $writer->write($product);
            }
            $writer->close();
        }

        $reader = JsonlReader::open($file);
        foreach ($reader as $product) {
            $io->title($product['title']);
            $io->listing([
                'Price: ' . $product['price'],
                'Category: ' . $product['category'],
            ]);
            break; // first product only
        }

        return Command::SUCCESS;
    }
}

$file = 'var/products.jsonl.gz';

$reader = JsonlReader::openArchive('/cache/raw.bagit.tar', 'data/raw/obj.jsonl.gz');
foreach ($reader as $row) {
    // Same decoded rows as an ordinary local JSONL file.
}

$reader = JsonlReader::openArchive($archive, 'data/raw/obj.jsonl.gz', $cacheDir);
(new JsonlIndexer())->index($reader->path(), sidecarPath: $reader->sidecarPath());
$row = $reader->get('record-id'); // Uses cached SQLite row bodies when present.
$state = $reader->state();