python avro 数据格式使用demo
{"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 下的示例代码:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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'}]}
另外使用map的示例:
{"name": "UEStat",
"type": "record",
"fields": [
{"name": "imsi", "type": "string"},
{"name": "time_at", "type": "string"},
{"name": "procedures_total_cnt", "type": "long"},
{"name": "is_over15_time_detach_minus_attach", "type": "boolean"},
{"name": "detail_procedures_cnt", "type": {"type": "map", "values": "long"}}
]
}
import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
schema = avro.schema.Parse(open('chr_ue_stat.avsc', "r").read())
writer = DataFileWriter(open("chr_ue_stat.avro", "wb"), DatumWriter(), schema)
writer.append({"imsi": "UE001", "time_at": "2018-04-09 12:15", "is_over15_time_detach_minus_attach": True, "procedures_total_cnt":789, "detail_procedures_cnt": {"A": 123, "B": 342}})
writer.append({"imsi": "UE002", "time_at": "2018-04-09 12:15", "is_over15_time_detach_minus_attach": False, "procedures_total_cnt": 876, "detail_procedures_cnt": {"C":1123, "D": 313}})
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