This is a Q&A, meaning I am sharing my solution/answer to a problem I faced:

The problem was that the getting started guide from the apache site was not entirely up- -to-date and after a bit of fiddling I manage to get the sample to work.

  1. First wget the latest release from here
  2. Go to the py3 subfolder under lang and build the project via your python 3 (read more here)
  3. Create user.avsc in the same folder as the python code is going to be located.

    The schema:

     {
    
      "namespace": "example.avro",
      "type": "record",
      "name": "User",
      "fields": [
          {"name": "name", "type": "string"},
          {"name": "favorite_number",  "type": ["int", "null"]},
          {"name": "favorite_color", "type": ["string", "null"]}
      ]
     }
    
  4. Create the .py from the code provided below (Mind that I had to make minor changes to the code in here to get this to work).

    The modified code is as follows.

    import avro.schema
    from avro.datafile import DataFileReader, DataFileWriter
    from avro.io import DatumReader, DatumWriter
    
    schema = avro.schema.Parse(open("user.avsc").read())
    
    writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema)
    writer.append({"name": "Alyssa", "favorite_number": 256})
    writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
    writer.close()
    
    reader = DataFileReader(open("users.avro", "rb"), DatumReader())
    for user in reader:
        print(user)
    
    reader.close()
    
有帮助吗?

解决方案

  1. First wget the latest release from here
  2. Go to the py3 subfolder under lang and build the project via your python 3 (read more here)
  3. Create user.avsc in the same folder as the python code is going to be located.

    The schema:

     {
    
      "namespace": "example.avro",
      "type": "record",
      "name": "User",
      "fields": [
          {"name": "name", "type": "string"},
          {"name": "favorite_number",  "type": ["int", "null"]},
          {"name": "favorite_color", "type": ["string", "null"]}
      ]
     }
    
  4. Create the .py from the code provided below (Mind that I had to make minor changes to the code in here to get this to work).

    The modified code is as follows.

    import avro.schema
    from avro.datafile import DataFileReader, DataFileWriter
    from avro.io import DatumReader, DatumWriter
    
    schema = avro.schema.Parse(open("user.avsc").read())
    
    writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema)
    writer.append({"name": "Alyssa", "favorite_number": 256})
    writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
    writer.close()
    
    reader = DataFileReader(open("users.avro", "rb"), DatumReader())
    for user in reader:
        print(user)
    
    reader.close()
    

其他提示

You can create a new project and then set python 2.7 as system interpreter and then import avro.

i got the output as in documentation https://avro.apache.org/docs/1.8.2/gettingstartedpython.html

{u'favorite_color': None, u'favorite_number': 256, u'name': u'Alyssa'}
{u'favorite_color': u'red', u'favorite_number': 7, u'name': u'Ben'}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top