Home | About | Sematext search-lucene.com search-hadoop.com
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB
 Search Hadoop and all its subprojects:

Switch to Threaded View
Avro >> mail # user >> Avro cpp setting precision for the output stream


Copy link to this message
-
Avro cpp setting precision for the output stream
I want to take an avro object, with double fields, and json encode it into
a string.  However, when I do this the encoded doubles have lost precision.
 I suspect this is because the precision I set on the ostringstream and
resulting avro::ostreamOutputStream isn't working.

Here's an example inspired by the tutorial (
http://avro.apache.org/docs/1.7.2/api/cpp/html/index.html). Using their
cpx.json

00001 {
00002     "type": "record",
00003     "name": "cpx",
00004     "fields" : [
00005         {"name": "re", "type": "double"},
00006         {"name": "im", "type" : "double"}
00007     ]
00008 }

Generating cpx.hh by
avrogencpp -i cpx.json -o cpx.hh -n c

The test program is then:

 1#include "cpx.hh"
 2#include "avro/Compiler.hh"
 3#include "avro/Encoder.hh"
 4#include "avro/Decoder.hh"
 5#include "avro/Specific.hh"
 6#include "avro/Generic.hh"
 7#include <sstream>
 8#include <fstream>
 9
10using namespace std;
11int main()
12{
13  ifstream ifs("cpx.json");
14  avro::ValidSchema type_schema;
15  avro::compileJsonSchema(ifs, type_schema);
16
17  ostringstream oss (ostringstream::out);
18  oss.precision(30); // doesn't matter
19  std::auto_ptr<avro::OutputStream> out = avro::ostreamOutputStream(oss);
20  avro::EncoderPtr e = avro::jsonEncoder(type_schema);
21  e->init(*out);
22  c::cpx complex;
23  complex.re = 1.123456789;
24  complex.im = 2.01;
25  avro::encode(*e, complex);
26  e->flush();
27  cout.precision(30);
28  cout << oss.str() << endl;
29
30  return 0;
31}

If we then compile and run this we get:
{"re":1.12346,"im":2.01}
Where as I'd expect something like:
{"re":1.12346789,"im":2.01}
It doesn't seem to matter where/if we set the precision on the output
stream (line 18).

Is there a different way to handle this? How do I keep the precision from
getting stripped off double when doing json encoding? Thanks!

-Paul
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB