Hinamarth

From Avro Schema to avro (default values)

Oct 31st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. # Define function that will extract the default dict elements from the avroSchema
  2. def explodeAvroSchemaFlat(avsc_dict):
  3.     list_of_element = avsc_dict['fields']
  4.     output = {}
  5.     for element in list_of_element:
  6.         if type(element['type']) is str:
  7.             output[element['name']] = element['default']
  8.         elif type(element['type']) is dict:
  9.             output[element['name']] = explodeAvroSchemaFlat(element['type'])
  10.     return output
  11.  
  12. # HERE: Insert parameters
  13. avro_schema_path = 'DiagnosticPacket_v2.avsc'
  14. avro_name = 'test_diagnostic_2.avro'
  15.  
  16. # Load Avro Schema as dict
  17. with open(avro_schema_path) as json_file:
  18.     json_avsc = json.load(json_file)
  19.  
  20. # Compute the dictionary with default value based on AvroSchema        
  21. default_avsc_dict = explodeAvroSchemaFlat(json_avsc)
  22.  
  23. # Parse the avro schema that
  24. schema = avro.schema.Parse(open(avro_schema_path, "rb").read())
  25.  
  26. writer = DataFileWriter(open(avro_name, "wb"), DatumWriter(), schema)
  27. writer.append(default_avsc_dict)
  28. writer.close()
  29.  
  30. reader = DataFileReader(open(avro_name, "rb"), DatumReader())
  31. for element in reader:
  32.     print(element)
  33. reader.close()
Advertisement
Add Comment
Please, Sign In to add comment