Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 39 lines (32 sloc) 0.762 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/usr/bin/env python

#
# derived from example at http://www.harshj.com/2010/04/25/writing-and-reading-avro-data-files-using-python/
#
from avro import schema, datafile, io
import pprint
import sys
import json

field_id = None
# Optional key to print
if (len(sys.argv) > 2):
  field_id = sys.argv[2]

# Test reading avros
rec_reader = io.DatumReader()

# Create a 'data file' (avro file) reader
df_reader = datafile.DataFileReader(
  open(sys.argv[1]),
  rec_reader
)

# Read all records stored inside
pp = pprint.PrettyPrinter()
i = 0
for record in df_reader:
  if i > 20:
    break
  i += 1
  if field_id:
    pp.pprint(record[field_id])
  else:
    pp.pprint(record)

obj = json.loads(df_reader.meta['avro.schema'])
print "\nAvro Schema: " + json.dumps(obj)
Something went wrong with that request. Please try again.