Giter VIP home page Giter VIP logo

avro-php's Introduction

nealio82/avro-php

This is a package based on the Apache AVRO PHP library (v 1.8.1).

The core logic remains the same but I've added Composer support, PSR-4 autoloading, method type-hinting, fixed property accessors with encapsulation where I've found errors (private/public member access), and some other tidy-ups.

Usage

Installation

$ composer require nealio82/avro-php

Usage

Based on the official library's example:

<?php

use Avro\DataIO\DataIO;
use Avro\DataIO\DataIOReader;
use Avro\DataIO\DataIOWriter;
use Avro\Datum\IODatumReader;
use Avro\Datum\IODatumWriter;
use Avro\IO\StringIO;
use Avro\Schema\Schema;

require_once('vendor/autoload.php');

$writers_schema_json = <<<_JSON
{
 "name":"member",
 "type":"record",
 "fields":
    [
        {"name":"member_id", "type":"int"},
        {"name":"member_name", "type":"string"}
    ]
}
_JSON;

$jose = array('member_id' => 1392, 'member_name' => 'Jose');
$maria = array('member_id' => 1642, 'member_name' => 'Maria');
$data = array($jose, $maria);


$file_name = 'data.avr';

// Open $file_name for writing, using the given writer's schema
$data_writer = DataIO::open_file($file_name, 'w', $writers_schema_json);
// Write each datum to the file
foreach ($data as $datum) {
    $data_writer->append($datum);
}
$data_writer->close();


// Open $file_name (by default for reading) using the writer's schema
// included in the file
$data_reader = DataIO::open_file($file_name);
echo "from file:\n";
// Read each datum
foreach ($data_reader->data() as $datum) {
    echo var_export($datum, true) . "\n";
}
$data_reader->close();


$io = new StringIO();

$writers_schema = Schema::parse($writers_schema_json);
$data_writer = new DataIOWriter($io, new IODatumWriter($writers_schema), $writers_schema);

foreach ($data as $datum) {
    $data_writer->append($datum);
}
$data_writer->close();


$binary_string = $io->string();

// Load the string data string
$read_io = new StringIO($binary_string);
$data_reader = new DataIOReader($read_io, new IODatumReader());
echo "from binary string:\n";
foreach ($data_reader->data() as $datum) {
    echo var_export($datum, true) . "\n";
}

avro-php's People

Contributors

iangely avatar nealio82 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.