python avro 数据格式使用demo

枫铃3年前 (2021-07-09)Python269
{"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

相关文章

python 的dict的append()?

看我下面这个表达...

Python十大常用文件操作

日常对于批量处理文件的需...

python递归函数错误使用方式

1.python递归函数...

排序算法的python实现

排序算法的python实现

冒泡排序 冒泡排序是比较简单的排序方法,它的思路是重复的走过要排序的序列,一次比较两个元素,如果顺序错误&#x...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。