Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-2570

Code line doesn't match the code in Avro C++ Document

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Trivial
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.10.0
    • Component/s: c++, doc
    • Labels:
      None

      Description

      The description does not match the code in Avro C++ Document

      The following is from Avro C++ Documentation:

      File: generated.cc
       1
      19 #include "cpx.hh"
      20 #include "avro/Encoder.hh"
      21 #include "avro/Decoder.hh"
      22 
      23 
      24 int
      25 main()
      26 {
      27  std::unique_ptr<avro::OutputStream> out = avro::memoryOutputStream();
      28  avro::EncoderPtr e = avro::binaryEncoder();
      29  e->init(*out);
      30  c::cpx c1;
      31  c1.re = 1.0;
      32  c1.im = 2.13;
      33  avro::encode(*e, c1);
      34 
      35  std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
      36  avro::DecoderPtr d = avro::binaryDecoder();
      37  d->init(*in);
      38 
      39  c::cpx c2;
      40  avro::decode(*d, c2);
      41  std::cout << '(' << c2.re << ", " << c2.im << ')' << std::endl;
      42  return 0;
      43 }
      In line 9, we construct a memory output stream. By this we indicate that we want to send the encoded Avro data into memory. In line 10, we construct a binary encoder, whereby we mean the output should be encoded using the Avro binary standard. In line 11, we attach the output stream to the encoder. At any given time an encoder can write to only one output stream.

      In line 14, we write the contents of c1 into the output stream using the encoder. Now the output stream contains the binary representation of the object. The rest of the code verifies that the data is indeed in the stream.

      In line 17, we construct a memory input stream from the contents of the output stream. Thus the input stream has the binary representation of the object. In line 18 and 19, we construct a binary decoder and attach the input stream to it. Line 22 decodes the contents of the stream into another object c2. Now c1 and c2 should have identical contents, which one can readily verify from the output of the program, which should be:

      (1, 2.13)

      Now, if you want to encode the data using Avro JSON encoding, you should use avro::jsonEncoder() instead of avro::binaryEncoder() in line 10 and avro::jsonDecoder() instead of avro::binaryDecoder() in line 18.

      On the other hand, if you want to write the contents to a file instead of memory, you should use avro::fileOutputStream() instead of avro::memoryOutputStream() in line 9 and avro::fileInputStream() instead of avro::memoryInputStream() in line 17.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                zeshuai007 Zezeng Wang
                Reporter:
                zeshuai007 Zezeng Wang
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: