| 48 | } |
| 49 | |
| 50 | void ProcessRowsInAvroFormat( |
| 51 | avro::ValidSchema const& valid_schema, |
| 52 | ::google::cloud::bigquery::storage::v1::AvroRows const& rows, |
| 53 | std::int64_t row_count) { |
| 54 | // Get an avro decoder. |
| 55 | std::stringstream row_bytes(rows.serialized_binary_rows(), std::ios::binary); |
| 56 | std::unique_ptr<avro::InputStream> in = avro::istreamInputStream(row_bytes); |
| 57 | avro::DecoderPtr decoder = |
| 58 | avro::validatingDecoder(valid_schema, avro::binaryDecoder()); |
| 59 | decoder->init(*in); |
| 60 | |
| 61 | for (auto i = 0; i < row_count; ++i) { |
| 62 | std::cout << "Row " << i << " "; |
| 63 | avro::GenericDatum datum(valid_schema); |
| 64 | avro::decode(*decoder, datum); |
| 65 | if (datum.type() == avro::AVRO_RECORD) { |
| 66 | const avro::GenericRecord& record = datum.value<avro::GenericRecord>(); |
| 67 | std::cout << "(" << record.fieldCount() << "): "; |
| 68 | for (auto i = 0; i < record.fieldCount(); i++) { |
| 69 | const avro::GenericDatum& datum = record.fieldAt(i); |
| 70 | |
| 71 | switch (datum.type()) { |
| 72 | case avro::AVRO_STRING: |
| 73 | std::cout << std::left << std::setw(15) |
| 74 | << datum.value<std::string>(); |
| 75 | break; |
| 76 | case avro::AVRO_INT: |
| 77 | std::cout << std::left << std::setw(15) << datum.value<int>(); |
| 78 | break; |
| 79 | case avro::AVRO_LONG: |
| 80 | std::cout << std::left << std::setw(15) << datum.value<long>(); |
| 81 | break; |
| 82 | // Depending on the table you are reading, you might need to add |
| 83 | // cases for other datatypes here. The schema will tell you what |
| 84 | // datatypes need to be handled. |
| 85 | default: |
| 86 | std::cout << std::left << std::setw(15) << "UNDEFINED"; |
| 87 | } |
| 88 | std::cout << "\t"; |
| 89 | } |
| 90 | } |
| 91 | std::cout << "\n"; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } // namespace |
| 96 | |