| 29 | #include <string> |
| 30 | |
| 31 | int main(int argc, char* argv[]) try { |
| 32 | if (argc != 4) { |
| 33 | std::cerr << "Usage: " << argv[0] |
| 34 | << " <project-id> <subscription-id> <avro-file>\n"; |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | std::string const project_id = argv[1]; |
| 39 | std::string const subscription_id = argv[2]; |
| 40 | std::string const avro_file = argv[3]; |
| 41 | |
| 42 | auto constexpr kWaitTimeout = std::chrono::seconds(30); |
| 43 | |
| 44 | // Create a namespace alias to make the code easier to read. |
| 45 | namespace pubsub = ::google::cloud::pubsub; |
| 46 | |
| 47 | //! [START pubsub_subscribe_avro_records_with_revisions] |
| 48 | auto subscriber = pubsub::Subscriber(pubsub::MakeSubscriberConnection( |
| 49 | pubsub::Subscription(project_id, subscription_id))); |
| 50 | |
| 51 | // Create a schema client. |
| 52 | auto schema_client = |
| 53 | pubsub::SchemaServiceClient(pubsub::MakeSchemaServiceConnection()); |
| 54 | |
| 55 | // Read the reader schema. This is the schema you want the messages to be |
| 56 | // evaluated using. |
| 57 | std::ifstream ifs(avro_file); |
| 58 | avro::ValidSchema reader_schema; |
| 59 | avro::compileJsonSchema(ifs, reader_schema); |
| 60 | |
| 61 | std::unordered_map<std::string, avro::ValidSchema> revisions_to_schemas; |
| 62 | auto session = subscriber.Subscribe( |
| 63 | [&](pubsub::Message const& message, pubsub::AckHandler h) { |
| 64 | // Get the reader schema revision for the message. |
| 65 | auto schema_name = message.attributes()["googclient_schemaname"]; |
| 66 | auto schema_revision_id = |
| 67 | message.attributes()["googclient_schemarevisionid"]; |
| 68 | // If we haven't received a message with this schema, look it up. |
| 69 | if (revisions_to_schemas.find(schema_revision_id) == |
| 70 | revisions_to_schemas.end()) { |
| 71 | auto schema_path = schema_name + "@" + schema_revision_id; |
| 72 | // Use the schema client to get the path. |
| 73 | auto schema = schema_client.GetSchema(schema_path); |
| 74 | if (!schema) { |
| 75 | std::cout << "Schema not found:" << schema_path << "\n"; |
| 76 | return; |
| 77 | } |
| 78 | avro::ValidSchema writer_schema; |
| 79 | std::stringstream in; |
| 80 | in << schema.value().definition(); |
| 81 | avro::compileJsonSchema(in, writer_schema); |
| 82 | revisions_to_schemas[schema_revision_id] = writer_schema; |
| 83 | } |
| 84 | auto writer_schema = revisions_to_schemas[schema_revision_id]; |
| 85 | |
| 86 | auto encoding = message.attributes()["googclient_schemaencoding"]; |
| 87 | if (encoding == "JSON") { |
| 88 | std::stringstream in; |
nothing calls this directly
no outgoing calls
no test coverage detected