Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define function that will extract the default dict elements from the avroSchema
- def explodeAvroSchemaFlat(avsc_dict):
- list_of_element = avsc_dict['fields']
- output = {}
- for element in list_of_element:
- if type(element['type']) is str:
- output[element['name']] = element['default']
- elif type(element['type']) is dict:
- output[element['name']] = explodeAvroSchemaFlat(element['type'])
- return output
- # HERE: Insert parameters
- avro_schema_path = 'DiagnosticPacket_v2.avsc'
- avro_name = 'test_diagnostic_2.avro'
- # Load Avro Schema as dict
- with open(avro_schema_path) as json_file:
- json_avsc = json.load(json_file)
- # Compute the dictionary with default value based on AvroSchema
- default_avsc_dict = explodeAvroSchemaFlat(json_avsc)
- # Parse the avro schema that
- schema = avro.schema.Parse(open(avro_schema_path, "rb").read())
- writer = DataFileWriter(open(avro_name, "wb"), DatumWriter(), schema)
- writer.append(default_avsc_dict)
- writer.close()
- reader = DataFileReader(open(avro_name, "rb"), DatumReader())
- for element in reader:
- print(element)
- reader.close()
Advertisement
Add Comment
Please, Sign In to add comment