python-avro-json-serializer, 使用AVRO模式将数据序列化为JSON格式

分享于 

6分钟阅读

GitHub

  繁體 雙語
Serializes data into a JSON format using AVRO schema.
  • 源代码名称:python-avro-json-serializer
  • 源代码网址:http://www.github.com/linkedin/python-avro-json-serializer
  • python-avro-json-serializer源代码文档
  • python-avro-json-serializer源代码下载
  • Git URL:
    git://www.github.com/linkedin/python-avro-json-serializer.git
    Git Clone代码到本地:
    git clone http://www.github.com/linkedin/python-avro-json-serializer
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/linkedin/python-avro-json-serializer
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    python Avro JSON序列化程序

    PyPI version

    AvroJsonSerializer 使用AVRO模式将数据序列化为JSON格式。

    为什么我们需要序列化程序而不只是转到 JSON?

    • 验证数据与架构是否匹配
    • 联合的序列化( 请参见下面的SimpleExample )
    • 有些 Avro JSON反引用器期望JSON中的字段顺序与模式中的顺序相同
    • bytesfixed 字段的序列化

    二进制分布可以在 找到。

    简单示例:

    schema_dict = {
     "namespace": "example.avro",
     "type": "record",
     "name": "User",
     "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number", "type": ["int", "null"]},
     {"name": "favorite_color", "type": ["string", "null"]}
     ]
    }
    avro_schema = avro.schema.make_avsc_object(schema_dict, avro.schema.Names())
    serializer = AvroJsonSerializer(avro_schema)self.assertEquals(serializer.to_json({"name": "Alyssa", "favorite_number": 256}),
     """{"name":"Alyssa","favorite_number":{"int":256},"favorite_color":null}""")self.assertEquals(serializer.to_json({"name": "Ben", "favorite_number": 7, "favorite_color": "red"}),
     """{"name":"Ben","favorite_number":{"int":7},"favorite_color":{"string":"red"}}""")self.assertEquals(serializer.to_json({"name": "Lion"}),
     """{"name":"Lion","favorite_number":null,"favorite_color":null}""")

    另一个例子:

    # need to serialize this datadata = {
     "ffloat": 1.0,
     "funion_null": None,
     "flong": 1L,
     "fdouble": 2.0,
     "ffixed": "1234567890123456",
     "fint": 1,
     "fstring": "hi there",
     "frec": {
     "subfint": 2 }
    }# according to this schema:schema_dict = {
     "fields": [{"name": "fint", "type": "int"},
     {"name": "flong", "type": "long"},
     {"name": "fstring", "type": "string"},
     {"name": "ffixed",
     "size": 16,
     "type": {"name": "fixed_16", "size": 16, "type": "fixed"}},
     {"name": "frec",
     "type": {"fields": [{"name": "subfint", "type": "int"}],
     "name": "Rec",
     "type": "record"}},
     {"name": "funion_null", "type": ["int", "null"]},
     {"name": "ffloat", "type": "float"},
     {"name": "fdouble", "type": "double"}],
     "name": "all_field",
     "namespace": "com.some.thing",
     "type": "record"}
    avro_schema = avro.schema.make_avsc_object(schema_dict, avro.schema.Names())
    serializer = AvroJsonSerializer(avro_schema)
    json_str = serializer.to_json(data)print json_str> {"fint":1,"flong":1,"fstring":"hi there","ffixed":"1234567890123456","frec":{"subfint":2},"funion_null":null,"ffloat":1.0,"fdouble":2.0}

    有关更多示例,请参见测试

    如何运行测试

    python-avro-json-serializer$ virtualenv venv
    python-avro-json-serializer$ source venv/bin/activate
    (venv)python-avro-json-serializer$ pip install tox
    (venv)python-avro-json-serializer$ tox
    GLOB sdist-make:/Users/bngo/python-avro-json-serializer/setup.py
    py27 create:/Users/bngo/python-avro-json-serializer/.tox/py27
    py27 installdeps: nose, -rrequirements.txt
    py27 inst:/Users/bngo/python-avro-json-serializer/.tox/dist/avro_json_serializer-0.4.1.zip
    py27 installed: avro==1.7.6,avro-json-serializer==0.4.1,nose==1.3.7,simplejson==3.8.2,six==1.10.0
    py27 runtests: PYTHONHASHSEED='107331485'py27 runtests: commands[0] | nosetests
    .............
    ----------------------------------------------------------------------
    Ran 13 tests in 0.066s
    OK
    py35 create:/Users/bngo/python-avro-json-serializer/.tox/py35
    py35 installdeps: nose, -rrequirements.txt
    py35 inst:/Users/bngo/python-avro-json-serializer/.tox/dist/avro_json_serializer-0.4.1.zip
    py35 installed: avro-json-serializer==0.4.1,avro-python3==1.8.1,nose==1.3.7,simplejson==3.8.2,six==1.10.0
    py35 runtests: PYTHONHASHSEED='107331485'py35 runtests: commands[0] | nosetests
    .............
    ----------------------------------------------------------------------
    Ran 13 tests in 0.029s
    OK
    _____________________________________________________________________________________ summary _____________________________________________________________________________________
     py27: commands succeeded
     py35: commands succeeded
     congratulations :)

    许可证

    许可协议的条款下,python 是许可的。


    数据  for  form  格式  Schema  serialize  
    相关文章