/** * @dataProvider file_name_provider */ public function test_read($file_name) { $dr = AvroDataIO::open_file($file_name, AvroFile::READ_MODE, $this->projection_json); $data = $dr->data(); $this->assertNotEquals(0, count($data), sprintf("no data read from %s", $file_name)); foreach ($data as $idx => $datum) { $this->assertNotNull($datum, sprintf("null datum from %s", $file_name)); } }
_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 = AvroDataIO::open_file($file_name, 'w', $writers_schema_json); // Write each datum to the file foreach ($data as $datum) { $data_writer->append($datum); } // Tidy up $data_writer->close(); // Open $file_name (by default for reading) using the writer's schema // included in the file $data_reader = AvroDataIO::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(); // Create a data string // Create a string io object. $io = new AvroStringIO(); // Create a datum writer object $writers_schema = AvroSchema::parse($writers_schema_json); $writer = new AvroIODatumWriter($writers_schema); $data_writer = new AvroDataIOWriter($io, $writer, $writers_schema); foreach ($data as $datum) { $data_writer->append($datum);
/** * Writes the header of the AvroIO object container */ private function write_header() { $this->write(AvroDataIO::magic()); $this->datum_writer->write_data(AvroDataIO::metadata_schema(), $this->metadata, $this->encoder); $this->write($this->sync_marker); }
public function test_differing_schemas_with_complex_objects() { $data_file = $this->add_data_file('data-complex.avr'); $writers_schema = <<<JSON { "type": "record", "name": "something", "fields": [ {"name": "something_fixed", "type": {"name": "inner_fixed", "type": "fixed", "size": 3}}, {"name": "something_enum", "type": {"name": "inner_enum", "type": "enum", "symbols": ["hello", "goodbye"]}}, {"name": "something_array", "type": {"type": "array", "items": "int"}}, {"name": "something_map", "type": {"type": "map", "values": "int"}}, {"name": "something_record", "type": {"name": "inner_record", "type": "record", "fields": [ {"name": "inner", "type": "int"} ]}}, {"name": "username", "type": "string"} ]} JSON; $data = array(array("username" => "john", "something_fixed" => "foo", "something_enum" => "hello", "something_array" => array(1, 2, 3), "something_map" => array("a" => 1, "b" => 2), "something_record" => array("inner" => 2), "something_error" => array("code" => 403)), array("username" => "ryan", "something_fixed" => "bar", "something_enum" => "goodbye", "something_array" => array(1, 2, 3), "something_map" => array("a" => 2, "b" => 6), "something_record" => array("inner" => 1), "something_error" => array("code" => 401))); $dw = AvroDataIO::open_file($data_file, 'w', $writers_schema); foreach ($data as $datum) { $dw->append($datum); } $dw->close(); foreach (array('fixed', 'enum', 'record', 'error', 'array', 'map', 'union') as $s) { $readers_schema = json_decode($writers_schema, true); $dr = AvroDataIO::open_file($data_file, 'r', json_encode($readers_schema)); foreach ($dr->data() as $idx => $obj) { foreach ($readers_schema['fields'] as $field) { $field_name = $field['name']; $this->assertEquals($data[$idx][$field_name], $obj[$field_name]); } } $dr->close(); } }