python avro 数据格式使用demo

枫铃4年前 (2021-07-09)Python276
{"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同步windows和linux文件

写python脚本的初衷,每次在windows编辑完文件后,想同步到linux上去,只能够登录服务器,...

爬虫基本原理

爬虫基本原理 一、爬虫是什么? 百度百科和维基百科对网络爬虫的定义:简单来说爬虫就是抓取目标网站内容的工具,一般是根据定义的行...

Django 函数和方法的区别

函数和方法的区别 1、函数要手动传self,方法不用传 2、如果是一个函数,用类名去调用,如果是一个方法...

Django 知识补漏单例模式

单例模式:(说白了就是)创建一个类的实例。在 Python 中,我们可以用多种方法来实现单例模式&#x...

Django基础知识MTV

Django简介 Django是使用Python编写的一个开源Web框架。可以用它来快速搭建一个高性能的网站。 Django也是一个MVC框架。但是在Dj...

Python mysql 索引原理与慢查询优化

一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,...

发表评论

访客

看不清,换一张

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