{"name": "UEProcedures", "type": "record", "fields": [ {"name": "imsi", "type": "string"}, {"name": "time_at", "type": "string"}, {"name": "procedures", "type": {"type": "array", "items": { "type": "record", "name": "SignalProcedure", "fields" : [ {"name": "timestamp", "type": "string"}, {"name": "procedure_tag", "type": "string"} ] }} } ] }
ue_procedure.avsc数据格式说明,python3 下的示例代码:
import avro.schema from avro.datafile import DataFileReader, DataFileWriter from avro.io import DatumReader, DatumWriter schema = avro.schema.Parse(open(‘ue_procedure.avsc‘, "r").read()) writer = DataFileWriter(open("ue_procedures.avro", "wb"), DatumWriter(), schema) writer.append({"imsi": "UE001", "time_at": "2018-04-09 12:15", "procedures": [{"timestamp": "2019-04-09 12:01", "procedure_tag": "A"}, {"timestamp": "2019-04-09 12:02", "procedure_tag": "B"}]}) writer.append({"imsi": "UE002", "time_at": "2018-04-09 12:15", "procedures": [{"timestamp": "2019-04-09 12:01", "procedure_tag": "A"}, {"timestamp": "2019-04-09 12:02", "procedure_tag": "B"}]}) writer.close() reader = DataFileReader(open("ue_procedures.avro", "rb"), DatumReader()) for ue in reader: print(ue) reader.close()
输出:
{‘imsi‘: ‘UE001‘, ‘time_at‘: ‘2018-04-09 12:15‘, ‘procedures‘: [{‘timestamp‘: ‘2019-04-09 12:01‘, ‘procedure_tag‘: ‘A‘}, {‘timestamp‘: ‘2019-04-09 12:02‘, ‘procedure_tag‘: ‘B‘}]}
{‘imsi‘: ‘UE002‘, ‘time_at‘: ‘2018-04-09 12:15‘, ‘procedures‘: [{‘timestamp‘: ‘2019-04-09 12:01‘, ‘procedure_tag‘: ‘A‘}, {‘timestamp‘: ‘2019-04-09 12:02‘, ‘procedure_tag‘: ‘B‘}]}
参考:https://avro.apache.org/docs/1.8.2/gettingstartedpython.html
原文:https://www.cnblogs.com/bonelee/p/10678193.html