You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Zezeng Wang (Jira)" <ji...@apache.org> on 2019/09/25 12:47:00 UTC

[jira] [Created] (AVRO-2570) Code line doesn't match the code in Avro C++ Document

Zezeng Wang created AVRO-2570:
---------------------------------

             Summary: Code line doesn't match the code in Avro C++ Document
                 Key: AVRO-2570
                 URL: https://issues.apache.org/jira/browse/AVRO-2570
             Project: Apache Avro
          Issue Type: Improvement
          Components: c, doc
            Reporter: Zezeng Wang
            Assignee: Zezeng Wang


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 {
 {color:#FF0000}27{color}  std::unique_ptr<avro::OutputStream> out = avro::memoryOutputStream();
 {color:#FF0000}28{color}  avro::EncoderPtr e = avro::binaryEncoder();
 {color:#FF0000}29{color}  e->init(*out);
 30  c::cpx c1;
 {color:#FF0000}31{color}  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 {color:#ff0000}line 9{color}, we construct a memory output stream. By this we indicate that we want to send the encoded Avro data into memory. In {color:#ff0000}line 10{color}, we construct a binary encoder, whereby we mean the output should be encoded using the Avro binary standard. In {color:#ff0000}line 11{color}, we attach the output stream to the encoder. At any given time an encoder can write to only one output stream.

In {color:#ff0000}line 14,{color} 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 {color:#ff0000}line 17{color}, 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 {color:#ff0000}line 1{color}8 and 19, we construct a binary decoder and attach the input stream to it. {color:#ff0000}Line 22{color} 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()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#a0847ef62f42f6f0d0af28da9f7c7cf15] instead of [avro::binaryEncoder()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#ad0158bd2fc76615b68db68d7e4f7c4f6] in {color:#ff0000}line 10{color} and [avro::jsonDecoder()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#a7ac8a9c93b0621227de3dcca383e36ff] instead of [avro::binaryDecoder()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#a04f78e53cb84a0189b626e6a5726b148] in {color:#ff0000}line 18{color}.

On the other hand, if you want to write the contents to a file instead of memory, you should use [avro::fileOutputStream()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#a164e34d709ba33d2464c295e7874f784] instead of [avro::memoryOutputStream()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#a0e961740e43d3f800220f03de11654e8] in {color:#ff0000}line 9{color} and [avro::fileInputStream()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#a1e5251d2ad9581e353399c1bac90c9fb] instead of [avro::memoryInputStream()|http://avro.apache.org/docs/current/api/cpp/html/namespaceavro.html#a98b7a8baaef11e477db89699ab27359b] in {color:#ff0000}line 17{color}.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)